A C++ DAL / ORM code generation framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.9 KiB

  1. /*
  2. g++ -g worm_wt_example.cpp -o wormex -rdynamic -lworm -lmysqlclient -lsqlite3 -lctemplate_nothreads -lwt -lwtext -lwthttp -lwtdbo -lwtdbosqlite3 -lwtdbopostgres -lboost_date_time-mt -lboost_regex-mt -lboost_program_options-mt -lboost_signals-mt -lboost_system-mt -lboost_filesystem-mt -lboost_thread-mt
  3. */
  4. #include <Wt/WApplication>
  5. #include <Wt/WServer>
  6. #include <Wt/WTable>
  7. #include <Wt/WString>
  8. #include <Wt/WEnvironment>
  9. #include <Wt/WBoxLayout>
  10. #include <Wt/WText>
  11. #include <Wt/WContainerWidget>
  12. #include "../src/sql/wsqldatabase.h"
  13. using Wt::WApplication;
  14. using Wt::WEnvironment;
  15. using Wt::WString;
  16. using Wt::WBoxLayout;
  17. using Wt::WText;
  18. using std::string;
  19. Wt::WTable *getdbtables()
  20. {
  21. Wt::WTable *table = new Wt::WTable();
  22. table->setMargin ( "0.5em" );
  23. Wt::WCssDecorationStyle *border = new Wt::WCssDecorationStyle();
  24. border->setBorder ( Wt::WBorder ( Wt::WBorder::Dotted ) );
  25. table->setDecorationStyle ( *border );
  26. // border->setBorder(Wt::WBorder(Wt::WBorder::Dashed ));
  27. WSql::WSqlDatabase db ( WSql::WSQLITE );
  28. db.setDatabaseName ( string ( "blog.db" ) );
  29. if ( !db.open() )
  30. {
  31. std::cerr << "nope .." << db.error().text() << std::endl;
  32. return table;
  33. }
  34. std::vector<string>tables = db.tableNames();
  35. std::vector<string>::iterator it = tables.begin();
  36. WSql::WSqlTable stbl;
  37. int numflds = 0;
  38. int row = 0;
  39. while ( it != tables.end() )
  40. {
  41. stbl = db.tableMetaData ( *it++ );
  42. numflds = stbl.count();
  43. Wt::WTableCell *cell = table->elementAt ( row++, 0 );
  44. cell->addWidget ( new Wt::WText ( WString ( "Table Name: {1}" ).arg ( stbl.name() ) ) );
  45. cell->setColumnSpan ( 5 );
  46. cell->setPadding ( "0.5em" );
  47. cell->setContentAlignment ( Wt::AlignCenter );
  48. int i = 0;
  49. for ( ; i < numflds; ++i )
  50. {
  51. WSql::WSqlColumn clm = stbl.column ( i );
  52. table->elementAt ( row + i, 0 )->addWidget ( new Wt::WText ( "Field: " ) );
  53. table->elementAt ( row + i, 0 )->setPadding ( "0.5em" );
  54. table->elementAt ( row + i, 0 )->setDecorationStyle ( *border );
  55. table->elementAt ( row + i, 1 )->addWidget ( new Wt::WText ( clm.columnName() ) );
  56. table->elementAt ( row + i, 1 )->setPadding ( "0.5em" );
  57. table->elementAt ( row + i, 1 )->setDecorationStyle ( *border );
  58. table->elementAt ( row + i, 2 )->addWidget ( new Wt::WText ( WSql::WSqlDataType::toString ( clm.type() ) ) );
  59. table->elementAt ( row + i, 2 )->setPadding ( "0.5em" );
  60. table->elementAt ( row + i, 2 )->setDecorationStyle ( *border );
  61. table->elementAt ( row + i, 3 )->addWidget ( new Wt::WText ( clm.defaultValue<string > () ) );
  62. table->elementAt ( row + i, 3 )->setPadding ( "0.5em" );
  63. table->elementAt ( row + i, 3 )->setDecorationStyle ( *border );
  64. table->elementAt ( row + i, 4 )->addWidget ( new Wt::WText ( clm.isPrimaryKey() ? "Primary Key" : "Not primary" ) );
  65. table->elementAt ( row + i, 4 )->setPadding ( "0.5em" );
  66. table->elementAt ( row + i, 4 )->setDecorationStyle ( *border );
  67. table->elementAt ( row + i, 5 )->addWidget ( new Wt::WText ( clm.canBeNull() ? "Nullable" : "Not nullable" ) );
  68. table->elementAt ( row + i, 5 )->setPadding ( "0.5em" );
  69. table->elementAt ( row + i, 5 )->setDecorationStyle ( *border );
  70. }
  71. row += i;
  72. }
  73. db.close();
  74. return table;
  75. }
  76. class wormApp : public Wt::WApplication
  77. {
  78. public:
  79. wormApp ( const Wt::WEnvironment &env );
  80. };
  81. wormApp::wormApp ( const Wt::WEnvironment &env ) : Wt::WApplication ( env )
  82. {
  83. setTitle ( "worm" );
  84. Wt::WTable *tbl = getdbtables();
  85. root()->addWidget ( new WText ( "Tables" ) );
  86. root()->addWidget ( tbl );
  87. }
  88. WApplication *createwormApp ( const WEnvironment &env )
  89. {
  90. WApplication *app = new wormApp ( env );
  91. return app;
  92. }
  93. int main ( int argc, char **argv )
  94. {
  95. Wt::WServer server ( argv[0] );
  96. server.setServerConfiguration ( argc, argv, WTHTTP_CONFIGURATION );
  97. server.addEntryPoint ( Wt::Application, createwormApp );
  98. if ( server.start() )
  99. {
  100. int sig = Wt::WServer::waitForShutdown ( argv[0] );
  101. std::cerr << "Shutdown (signal = " << sig << ")" << std::endl;
  102. server.stop();
  103. }
  104. }