7 #include <TMySQLServer.h>
8 #include <TSQLiteServer.h>
9 #include <TSQLStatement.h>
10 #include <TSQLResult.h>
14 #define LogInfo(message) std::cout << "DEBUG: " << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << " ::: " << message << std::endl
20 std::ifstream infile(fileName.c_str());
28 if(wordexp(input.c_str(), &exp_result, 0) != 0)
30 std::cout <<
"ExpandEnvironmentals - ERROR - Your string '" << input <<
"' cannot be understood!" << endl;
34 const string output( exp_result.we_wordv[0]);
43 if (my_cnf.length() > 0) {
46 if (usr_id == Guest) {
50 LogInfo(
"DB Conf. "<< m_my_cnf <<
" doesn't exist");
53 else m_my_cnf =
"my.cnf";
62 if (m_svr_id != LITE) {
63 LogInfo(
"DbSvc::DbSvc(const SvrId_t, const std::string) is for sqlite db only.");
69 LogInfo(
"SQLITE DB file " << m_dbfile <<
" does not exist");
73 m_svr = Form(
"sqlite://%s", m_dbfile.c_str());
74 m_con =
new TSQLiteServer(m_svr.c_str());
79 if (m_con)
delete m_con;
85 TSQLResult* res = m_con->GetDataBases(name);
87 while ( (row = res->Next()) != 0 ) {
88 string val = row->GetField(0);
90 if (val == name) found =
true;
97 ret = m_con->DropDataBase(name);
98 if (ret == 0) ret = m_con->CreateDataBase(name);
103 if (do_create) ret = m_con->CreateDataBase(name);
105 if (ret == 0) ret = m_con->SelectDataBase(name);
107 cerr <<
"!!ERROR!! DbSvc::UseSchema: Failed to select '" << name <<
"'. Abort." << endl;
114 string query =
"drop table if exists ";
116 if (! m_con->Exec(query.c_str())) {
117 cerr <<
"!!ERROR!! DbSvc::DropTable(): Failed on " << name <<
". Abort." << endl;
124 if (m_con->HasTable(name))
return true;
126 cerr <<
"!!ERROR!! DbSvc: The table '" << name <<
"' does not exist. Abort." << endl;
132 void DbSvc::CreateTable(
const std::string name,
const std::vector<std::string> list_var,
const std::vector<std::string> list_type,
const std::vector<std::string> list_key)
134 if (HasTable(name)) {
135 cerr <<
"!!ERROR!! DbSvc::CreateTable(): Table '" << name <<
"' already exists. To avoid an unintended creation, please check and drop the table in your code. Abort." << endl;
138 if (list_var.size() != list_type.size()) {
139 cerr <<
"!!ERROR!! DbSvc::CreateTable(): The sizes of the var and type lists don't match. Abort." << endl;
144 oss <<
"create table " << name <<
"(";
145 for (
unsigned int ii = 0; ii < list_var.size(); ii++) {
146 oss << list_var[ii] <<
" " << list_type[ii] <<
", ";
148 if (list_key.size() == 0) {
149 oss <<
"primary_id INT not null auto_increment, PRIMARY KEY (primary_id)";
151 oss <<
"PRIMARY KEY (";
152 for (
unsigned int ii = 0; ii < list_key.size(); ii++) {
153 if (ii != 0) oss <<
", ";
159 if (! m_con->Exec(oss.str().c_str())) {
160 cerr <<
"!!ERROR!! DbSvc::CreateTable(): The creation query failed. Abort." << endl;
165 void DbSvc::CreateTable(
const std::string name,
const int n_var,
const char** list_var,
const char** list_type,
const int n_key,
const char** list_key)
167 vector<string> vec_var;
168 vector<string> vec_type;
169 for (
int ii = 0; ii < n_var; ii++) {
170 vec_var .push_back(list_var [ii]);
171 vec_type.push_back(list_type[ii]);
173 vector<string> vec_key;
174 for (
int ii = 0; ii < n_key; ii++) {
175 vec_key.push_back(list_key[ii]);
177 CreateTable(name, vec_var, vec_type, vec_key);
182 vector<string> vec_var;
183 vector<string> vec_type;
184 vector<string> vec_key;
185 for (
unsigned int ii = 0; ii < list.
Size(); ii++) {
188 list.
Get(ii, name, type, is_key);
189 vec_var .push_back(name);
190 vec_type.push_back(type);
191 if (is_key) vec_key.push_back(name);
193 CreateTable(name, vec_var, vec_type, vec_key);
208 TSQLStatement* stmt = m_con->Statement(query);
209 if ((! stmt->Process()) && m_svr_id != LITE) {
210 cerr <<
"!!ERROR!! DbSvc::Process(): Failed to execute a statement: " << stmt <<
".\n"
218 void DbSvc::SelectServer()
220 if (m_svr_id == DB1 ) m_svr =
"e906-db1.fnal.gov";
221 else if (m_svr_id == DB2 ) m_svr =
"e906-db2.fnal.gov";
222 else if (m_svr_id == DB3 ) m_svr =
"e906-db3.fnal.gov";
223 else if (m_svr_id == DB01) m_svr =
"seaquestdb01.fnal.gov:3310";
224 else if (m_svr_id == UIUC) m_svr =
"seaquel.physics.illinois.edu:3283";
225 else if (m_svr_id == LOCAL) m_svr =
"localhost";
227 cerr <<
"!!ERROR!! DbSvc(): Unsupported server ID. Abort.\n";
232 void DbSvc::ConnectServer()
235 oss <<
"mysql://" << m_svr <<
"/?reconnect=1&cnf_file=" << m_my_cnf;
238 m_con = TMySQLServer::Connect(oss.str().c_str(), 0, 0);
239 if (! (m_con && m_con->IsConnected())) {
240 cerr <<
"!!ERROR!! DbSvc::ConnectServer(): Failed. Abort." << endl;
247 m_name .push_back(name);
248 m_type .push_back(type);
249 m_is_key.push_back(is_key);
256 is_key = m_is_key[idx];
void Get(const int idx, std::string &name, std::string &type, bool &is_key) const
void UseSchema(const char *name, const bool do_create=false, const bool do_drop=false)
DbSvc(const SvrId_t svr_id=DB1, const UsrId_t usr_id=Guest, const std::string my_cnf="")
TSQLStatement * Process(const char *query)
std::string ExpandEnvironmentals(const std::string &input)
void Add(const std::string name, const std::string type, const bool is_key=false)
bool FileExist(const std::string fileName)
void CreateTable(const std::string name, const std::vector< std::string > list_var, const std::vector< std::string > list_type, const std::vector< std::string > list_key)
unsigned int Size() const
bool HasTable(const char *name, const bool exit_on_false=false)
void DropTable(const char *name)