WORM 0.2
A C++ DAL/ORM code generation framework

src/wormgen.cpp

Go to the documentation of this file.
00001 /*
00002  *    WORM - a DAL/ORM code generation framework
00003  *    Copyright (C) 2011  Erik Winn <erikwinnmail@yahoo.com>
00004  * 
00005  *    This program is free software: you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation, either version 3 of the License, or
00008  *    (at your option) any later version.
00009  * 
00010  *    This program is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *    GNU General Public License for more details.
00014  * 
00015  *    You should have received a copy of the GNU General Public License
00016  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 
00020 #include "sql/wsqldatabase.h"
00021 #include "orm/wormclassgenerator.h"
00022 #include <iostream>
00023 #include <getopt.h>
00024 #include <dirent.h>
00025 
00026 #define PACKAGE    "wormgen"
00027 #define VERSION    "0.2"
00028 
00029 #define DEFAULT_TPL_DIR "/usr/local/share/worm"
00030 
00031 void print_help( int exval )
00032 {
00033     std::cout << PACKAGE << " " << VERSION <<  "\n Usage:\n" << std::endl;
00034     std::cout << PACKAGE << " [-hVv] <-d database> \n [-t templates_dir ] [-o outputdir] [-u username] [-p password] [-s hostname] [-D driver] \n" << std::endl;
00035     std::cout << "  -h              print this help and exit" << std::endl;
00036     std::cout << "  -V              print version and exit" << std::endl;
00037     std::cout << "  -v              set verbose flag" << std::endl;
00038     std::cout << "  -d database            set database schema to use" << std::endl;
00039     std::cout << "  -u username            set username" << std::endl;
00040     std::cout << "  -p password            set password" << std::endl;
00041     std::cout << "  -s server              set server or hostname" << std::endl;
00042     std::cout << "  -o DIRECTORY           set output directory" << std::endl;
00043     std::cout << "  -t DIRECTORY          set templates directory" << std::endl;
00044     std::cout << "  -D driver              set database driver (default MYSQL)" << std::endl
00045     << "Note: driver may be one of: 'mysql' or 'sqlite'. " << std::endl;
00046     
00047     exit( exval );
00048 }
00049 
00050 WSql::DriverType getDriver(const std::string drivername)
00051 {
00052     if(drivername.compare("mysql")==0)
00053         return WSql::WMYSQL;
00054     if(drivername.compare("sqlite")==0)
00055         return WSql::WSQLITE;
00056     std::cout << PACKAGE << ": Error - driver not supported:" << drivername << " \n" << std::endl;
00057     print_help( 1 );    
00058 }
00059 
00060 int main( int argc, char ** argv )
00061 {
00062     if ( argc == 1 )
00063         print_help( 1 );
00064     int opt;
00065     bool verbose = false;
00066     std::string hostname = "localhost";
00067     std::string dbname;
00068     std::string username = "root";
00069     std::string password = "";
00070     std::string outputdir = ".";
00071     std::string templatesdir = "./src/orm/templates";
00072     DIR *dir = opendir (DEFAULT_TPL_DIR);
00073     if (dir != NULL){
00074         templatesdir = DEFAULT_TPL_DIR;
00075         closedir(dir);
00076     }
00077     WSql::DriverType drivertype = WSql::WMYSQL;
00078 
00079     while (( opt = getopt( argc, argv, "hVvf:o:u:d:D:p:s:t:" ) ) != -1 ) {
00080         switch ( opt ) {
00081             case 'h':
00082                 print_help( 0 );
00083                 break;
00084             case 'V':
00085                 std::cout <<  PACKAGE << " " << VERSION << std::endl;
00086                 exit( 0 );
00087                 break;
00088             case 'v':
00089                 verbose = true;
00090                 break;
00091             case 'u':
00092                 username = optarg;
00093                 break;
00094             case 'd':
00095                 dbname = optarg;
00096                 break;
00097             case 'D':
00098                 drivertype = getDriver(std::string(optarg));
00099                 break;
00100             case 'p':
00101                 password = optarg;
00102                 break;
00103             case 's':
00104                 hostname = optarg;
00105                 break;
00106             case 'o':
00107                 outputdir = optarg;
00108                 break;
00109             case 't':
00110                 templatesdir = optarg;
00111                 break;
00112             case ':':
00113                 std::cout << PACKAGE << ": Error - Option " << optopt << " requires a value\n" << std::endl;
00114                 print_help( 1 );
00115                 break;
00116             default:
00117                 std::cout << PACKAGE << ": Error - No such option:" << optopt << " \n" << std::endl;
00118                 print_help( 1 );
00119         }
00120     }
00121     
00122     if ( (dir = opendir(templatesdir.c_str())) == NULL)  {
00123         std::cout << PACKAGE << " - FATAL: Cannot find template directory " << templatesdir << "!" << std::endl;
00124         print_help(2);
00125     }
00126     closedir(dir);
00127     if ( dbname.empty() ) {
00128         std::cerr << PACKAGE << ": Error - no database specified! \n" << std::endl;
00129         print_help(3);
00130     }
00131 
00132 //OK, lets go!
00133     WSql::WSqlDatabase db( drivertype );
00134     db.setDatabaseName( dbname );
00135     db.setUserName( username );
00136     db.setHostName( hostname );
00137     db.setPassword( password );
00138 
00139     if ( !db.open() ) {
00140         std::cerr << "Failed to open: " << db.error().text() << std::endl;
00141         return 4;
00142     }
00143 
00144     WSql::WormClassGenerator gen(db);
00145     gen.setTemplateDirectory(templatesdir);
00146     gen.setOutputDirectory(outputdir);
00147     gen.init();
00148     gen.run();
00149     
00150     return 0;
00151 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Defines