14
14
#include " flex-lua-index.hpp"
15
15
#include " flex-table.hpp"
16
16
#include " lua-utils.hpp"
17
+ #include " output-flex.hpp"
17
18
#include " pgsql-capabilities.hpp"
19
+ #include " util.hpp"
18
20
19
21
#include < lua.hpp>
20
22
@@ -416,6 +418,12 @@ void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table,
416
418
lua_pop (lua_state, 1 ); // "indexes"
417
419
}
418
420
421
+ TRAMPOLINE_WRAPPED_OBJECT (table, __tostring)
422
+ TRAMPOLINE_WRAPPED_OBJECT (table, cluster)
423
+ TRAMPOLINE_WRAPPED_OBJECT (table, columns)
424
+ TRAMPOLINE_WRAPPED_OBJECT (table, name)
425
+ TRAMPOLINE_WRAPPED_OBJECT (table, schema)
426
+
419
427
} // anonymous namespace
420
428
421
429
int setup_flex_table (lua_State *lua_state, std::vector<flex_table_t > *tables,
@@ -442,3 +450,81 @@ int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
442
450
443
451
return 1 ;
444
452
}
453
+
454
+ /* *
455
+ * Define the osm2pgsql.Table class/metatable.
456
+ */
457
+ void lua_wrapper_table::init (lua_State *lua_state)
458
+ {
459
+ lua_getglobal (lua_state, " osm2pgsql" );
460
+ if (luaL_newmetatable (lua_state, osm2pgsql_table_name) != 1 ) {
461
+ throw std::runtime_error{" Internal error: Lua newmetatable failed." };
462
+ }
463
+ lua_pushvalue (lua_state, -1 ); // Copy of new metatable
464
+
465
+ // Add metatable as osm2pgsql.Table so we can access it from Lua
466
+ lua_setfield (lua_state, -3 , " Table" );
467
+
468
+ // Now add functions to metatable
469
+ lua_pushvalue (lua_state, -1 );
470
+ lua_setfield (lua_state, -2 , " __index" );
471
+ luaX_add_table_func (lua_state, " __tostring" ,
472
+ lua_trampoline_table___tostring);
473
+ luaX_add_table_func (lua_state, " insert" , lua_trampoline_table_insert);
474
+ luaX_add_table_func (lua_state, " name" , lua_trampoline_table_name);
475
+ luaX_add_table_func (lua_state, " schema" , lua_trampoline_table_schema);
476
+ luaX_add_table_func (lua_state, " cluster" , lua_trampoline_table_cluster);
477
+ luaX_add_table_func (lua_state, " columns" , lua_trampoline_table_columns);
478
+
479
+ lua_pop (lua_state, 2 );
480
+ }
481
+
482
+ int lua_wrapper_table::__tostring () const
483
+ {
484
+ std::string const str{fmt::format (" osm2pgsql.Table[{}]" , self ().name ())};
485
+ luaX_pushstring (lua_state (), str);
486
+
487
+ return 1 ;
488
+ }
489
+
490
+ int lua_wrapper_table::cluster () const
491
+ {
492
+ lua_pushboolean (lua_state (), self ().cluster_by_geom ());
493
+ return 1 ;
494
+ }
495
+
496
+ int lua_wrapper_table::columns () const
497
+ {
498
+ lua_createtable (lua_state (), (int )self ().num_columns (), 0 );
499
+
500
+ int n = 0 ;
501
+ for (auto const &column : self ().columns ()) {
502
+ lua_pushinteger (lua_state (), ++n);
503
+ lua_newtable (lua_state ());
504
+
505
+ luaX_add_table_str (lua_state (), " name" , column.name ().c_str ());
506
+ luaX_add_table_str (lua_state (), " type" , column.type_name ().c_str ());
507
+ luaX_add_table_str (lua_state (), " sql_type" ,
508
+ column.sql_type_name ().c_str ());
509
+ luaX_add_table_str (lua_state (), " sql_modifiers" ,
510
+ column.sql_modifiers ().c_str ());
511
+ luaX_add_table_bool (lua_state (), " not_null" , column.not_null ());
512
+ luaX_add_table_bool (lua_state (), " create_only" , column.create_only ());
513
+
514
+ lua_rawset (lua_state (), -3 );
515
+ }
516
+
517
+ return 1 ;
518
+ }
519
+
520
+ int lua_wrapper_table::name () const
521
+ {
522
+ luaX_pushstring (lua_state (), self ().name ());
523
+ return 1 ;
524
+ }
525
+
526
+ int lua_wrapper_table::schema () const
527
+ {
528
+ luaX_pushstring (lua_state (), self ().schema ());
529
+ return 1 ;
530
+ }
0 commit comments