createTables.cxx

00001 #include "config.h"
00002 #include "Options.h"
00003 #include "Sequence.h"
00004 #include "Config.h"
00005 #include "Priority.h"
00006 #include "Parameter.h"
00007 #include "SingleValue.h"
00008 #include "BlobValue.h"
00009 #include "Address.h"
00010 #include <rcudb/Server.h>
00011 #include <rcudb/Result.h>
00012 #include <rcudb/Sql.h>
00013 #include <iostream>
00014 #include <sstream>
00015 #include <stdexcept>
00016 
00017 //____________________________________________________________________
00018 template <typename T>
00019 void createTable(RcuDb::Server& s, const std::string& name) 
00020 {
00021   if (T::Create(s)) return;
00022   
00023   std::stringstream errstr;
00024   errstr << "Failed to create " << name << " table: " 
00025          << s.ErrorString();
00026   throw std::runtime_error(errstr.str());
00027 }
00028 
00029 
00030 //____________________________________________________________________
00031 int 
00032 main(int argc, char** argv)
00033 {
00034   using namespace RcuConf;
00035   Option<bool>        hOpt('h', "help",    "Show this help", false,false);
00036   Option<bool>        VOpt('V', "version", "Show version number", false,false);
00037   Option<std::string> cOpt('c', "connection", "Database connection url", 
00038                            "mysql://config@localhost/RCU");
00039   CommandLine cl("");
00040   cl.Add(hOpt);
00041   cl.Add(VOpt);
00042   cl.Add(cOpt);
00043 
00044   if (!cl.Process(argc, argv)) return 1;
00045   if (hOpt.IsSet()) {
00046     cl.Help();
00047     return 0;
00048   }
00049   if (VOpt.IsSet()) {
00050     std::cout << "raw2root version " << VERSION << std::endl;
00051     return 0;
00052   }
00053 
00054   
00055   std::string con = cOpt;
00056   try {
00057     RcuDb::Server* server = RcuDb::Server::Connect(con);
00058     if (!server) 
00059       throw std::runtime_error("Failed to open connection to server!");
00060   
00061     if (!Sequence::Create(*server)) {
00062       std::stringstream s;
00063       s << "Failed to create Sequence table: " << server->ErrorString();
00064       throw std::runtime_error(s.str());
00065     }
00066     createTable<Config>(*server,      "Config");
00067     createTable<Priority>(*server,       "Priority");
00068     createTable<Parameter>(*server,   "Parameter");
00069     createTable<SingleValue>(*server, "SingleValue");
00070     createTable<BlobValue>(*server,   "BlobValue");
00071     createTable<Address>(*server,     "Address");
00072 
00073     RcuDb::Result* res = server->Query("SHOW TABLES");
00074     if (!res) {
00075       std::stringstream s;
00076       s << "Failed to get priorities: " << server->ErrorString();
00077       throw std::runtime_error(s.str());
00078     }
00079     res->Print();
00080   }
00081   catch (std::exception& e) {
00082     std::cerr << e.what() << std::endl;
00083     return 1;
00084   }
00085   return 0;
00086 }
00087 
00088       
00089 
00090 
Top of page Last update Fri Apr 27 01:54:15 2007
Copyright © 2004 Christian Holm Created by DoxyGen 1.3.5