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.

151 lines
5.1 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*
  2. * WORM - a DAL/ORM code generation framework
  3. * Copyright (C) 2011 Erik Winn <sidewalksoftware@gmail.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "sql/wsqldatabase.h"
  19. #include "orm/wormclassgenerator.h"
  20. #include <iostream>
  21. #include <getopt.h>
  22. #include <dirent.h>
  23. #define PACKAGE "wormgen"
  24. #define VERSION "0.2"
  25. #define DEFAULT_TPL_DIR "/usr/local/share/worm"
  26. void print_help( int exval )
  27. {
  28. std::cout << PACKAGE << " " << VERSION << "\n Usage:\n" << std::endl;
  29. std::cout << PACKAGE << " [-hVv] <-d database> \n [-t templates_dir ] [-o outputdir] [-u username] [-p password] [-s hostname] [-D driver] \n" << std::endl;
  30. std::cout << " -h print this help and exit" << std::endl;
  31. std::cout << " -V print version and exit" << std::endl;
  32. std::cout << " -v set verbose flag" << std::endl;
  33. std::cout << " -d database set database schema to use" << std::endl;
  34. std::cout << " -u username set username" << std::endl;
  35. std::cout << " -p password set password" << std::endl;
  36. std::cout << " -s server set server or hostname" << std::endl;
  37. std::cout << " -o DIRECTORY set output directory" << std::endl;
  38. std::cout << " -t DIRECTORY set templates directory" << std::endl;
  39. std::cout << " -D driver set database driver (default MYSQL)" << std::endl
  40. << "Note: driver may be one of: 'mysql' or 'sqlite'. " << std::endl;
  41. exit( exval );
  42. }
  43. WSql::DriverType getDriver(const std::string drivername)
  44. {
  45. if(drivername.compare("mysql")==0)
  46. return WSql::WMYSQL;
  47. if(drivername.compare("sqlite")==0)
  48. return WSql::WSQLITE;
  49. std::cout << PACKAGE << ": Error - driver not supported:" << drivername << " \n" << std::endl;
  50. print_help( 1 );
  51. }
  52. int main( int argc, char ** argv )
  53. {
  54. if ( argc == 1 )
  55. print_help( 1 );
  56. int opt;
  57. bool verbose = false;
  58. std::string hostname = "localhost";
  59. std::string dbname;
  60. std::string username = "root";
  61. std::string password = "";
  62. std::string outputdir = ".";
  63. std::string templatesdir = "./src/orm/templates";
  64. DIR *dir = opendir (DEFAULT_TPL_DIR);
  65. if (dir != NULL){
  66. templatesdir = DEFAULT_TPL_DIR;
  67. closedir(dir);
  68. }
  69. WSql::DriverType drivertype = WSql::WMYSQL;
  70. while (( opt = getopt( argc, argv, "hVvf:o:u:d:D:p:s:t:" ) ) != -1 ) {
  71. switch ( opt ) {
  72. case 'h':
  73. print_help( 0 );
  74. break;
  75. case 'V':
  76. std::cout << PACKAGE << " " << VERSION << std::endl;
  77. exit( 0 );
  78. break;
  79. case 'v':
  80. verbose = true;
  81. break;
  82. case 'u':
  83. username = optarg;
  84. break;
  85. case 'd':
  86. dbname = optarg;
  87. break;
  88. case 'D':
  89. drivertype = getDriver(std::string(optarg));
  90. break;
  91. case 'p':
  92. password = optarg;
  93. break;
  94. case 's':
  95. hostname = optarg;
  96. break;
  97. case 'o':
  98. outputdir = optarg;
  99. break;
  100. case 't':
  101. templatesdir = optarg;
  102. break;
  103. case ':':
  104. std::cout << PACKAGE << ": Error - Option " << optopt << " requires a value\n" << std::endl;
  105. print_help( 1 );
  106. break;
  107. default:
  108. std::cout << PACKAGE << ": Error - No such option:" << optopt << " \n" << std::endl;
  109. print_help( 1 );
  110. }
  111. }
  112. if ( (dir = opendir(templatesdir.c_str())) == NULL) {
  113. std::cout << PACKAGE << " - FATAL: Cannot find template directory " << templatesdir << "!" << std::endl;
  114. print_help(2);
  115. }
  116. closedir(dir);
  117. if ( dbname.empty() ) {
  118. std::cerr << PACKAGE << "FATAL: Error - no database specified! \n" << std::endl;
  119. print_help(3);
  120. }
  121. //OK, lets go!
  122. WSql::WSqlDatabase db( drivertype );
  123. db.setDatabaseName( dbname );
  124. db.setUserName( username );
  125. db.setHostName( hostname );
  126. db.setPassword( password );
  127. if ( !db.open() ) {
  128. std::cerr << "FATAL: Failed to open database - error: " << db.error().text() << std::endl;
  129. return 4;
  130. }
  131. WSql::WormClassGenerator gen(db);
  132. gen.setTemplateDirectory(templatesdir);
  133. gen.setOutputDirectory(outputdir);
  134. gen.init();
  135. gen.run();
  136. return 0;
  137. }