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/Sql.h>
00012 #include <iostream>
00013 #include <sstream>
00014 #include <stdexcept>
00015
00016
00017 int
00018 main(int argc, char** argv)
00019 {
00020 using namespace RcuConf;
00021 Option<bool> hOpt('h', "help", "Show this help", false,false);
00022 Option<bool> VOpt('V', "version", "Show version number",false,false);
00023 Option<unsigned> tOpt('t', "tag", "Tag to use", 0);
00024 Option<unsigned> xOpt('x', "x", "X coordinate", 0);
00025 Option<unsigned> yOpt('y', "y", "Y coordinate", 0);
00026 Option<unsigned> zOpt('z', "z", "Z coordinate", 0);
00027 Option<std::string> dOpt('d', "description", "Description", "");
00028 Option<std::string> pOpt('p', "priority", "Priority to use", "default");
00029 Option<std::string> cOpt('c', "connection", "Database connection url",
00030 "mysql://config@localhost/RCU");
00031 CommandLine cl("");
00032 cl.Add(hOpt);
00033 cl.Add(VOpt);
00034 cl.Add(cOpt);
00035 cl.Add(tOpt);
00036 cl.Add(xOpt);
00037 cl.Add(yOpt);
00038 cl.Add(zOpt);
00039 cl.Add(dOpt);
00040
00041 if (!cl.Process(argc, argv)) return 1;
00042 if (hOpt.IsSet()) {
00043 cl.Help();
00044 return 0;
00045 }
00046 if (VOpt.IsSet()) {
00047 std::cout << "createConfigs version " << VERSION << std::endl;
00048 return 0;
00049 }
00050
00051 try {
00052 std::string con = cOpt;
00053 RcuDb::Server* server = RcuDb::Server::Connect(con);
00054 if (!server)
00055 throw std::runtime_error("Failed to open connection to server!");
00056
00057 Priority::List orders;
00058 RcuDb::Sql sel;
00059 sel << "description = '" << pOpt.Value() << "'";
00060 if (!Priority::Select(orders, *server, sel)) {
00061 std::stringstream s;
00062 s << "Failed to get priority '" << pOpt.Value() << "': "
00063 << server->ErrorString();
00064 throw std::runtime_error(s.str());
00065 }
00066 if (orders.size() < 0)
00067 throw std::runtime_error("Too few matches on default priority");
00068 if (orders.size() > 1)
00069 throw std::runtime_error("Too many matches on default priority");
00070
00071 Priority* priority = *(orders.begin());
00072
00073 RcuConf::Config c(tOpt, xOpt, yOpt, zOpt, priority->Id(), dOpt);
00074 if (!c.Insert(*server)) {
00075 std::stringstream errstr;
00076 errstr << "Failed to insert " << dOpt.Value() << " into Config table: "
00077 << server->ErrorString();
00078 throw std::runtime_error(errstr.str());
00079 }
00080
00081 Config::List confs;
00082 if (!Config::Select(confs, *server, "")) {
00083 std::stringstream s;
00084 s << "Failed to get configs: " << server->ErrorString();
00085 throw std::runtime_error(s.str());
00086 }
00087 for (Config::List::iterator i = confs.begin(); i != confs.end(); ++i)
00088 (*i)->Print();
00089 }
00090 catch (std::exception& e) {
00091 std::cerr << e.what() << std::endl;
00092 return 1;
00093 }
00094 return 0;
00095 }
00096
00097
00098
00099