34 #include "TSQLServer.h"
35 #include "TSQLResult.h"
51 TString
HarpoDB::dbUrl = TString(
"mysql://llrharpo.in2p3.fr/harpo");
57 return basename((
char *)dbUrl.Data());
68 cout <<
"List all tables in database \"" << GetDBName()
69 <<
"\" on server " << db->GetHost() << endl;
73 while ( (obj = next()) )
74 cout <<
"Table: " << obj->GetName() << endl;
80 printf(
"DB %s\n",__FUNCTION__);
82 if ( DbUrl != NULL ) {
86 const char *dburl = gSystem->Getenv(
"HARPO_DB_URL");
87 if ( dburl != NULL ) {
88 if ( strlen(dburl) > 0 )
89 dbUrl = TString(dburl);
94 cout <<
" Warn NO Database URL" << endl;
96 db = TSQLServer::Connect(dbUrl.Data(),dbuser.Data(), dbpw.Data());
99 cout <<
" Error acess a database" << endl;
104 printf(
"Server info: %s\n", db->ServerInfo());
106 fTables = db->GetTablesList();
114 printf(
"DB %s\n",__FUNCTION__);
130 printf(
"DB %s\n",__FUNCTION__);
132 const char *nm = name.Data();
133 if ( fTables->FindObject(nm) == 0 ) {
137 return table.
Open(GetDBName(), nm);
144 printf(
"%s\n",__FUNCTION__);
153 res = db->GetColumns(dbnm, tbl);
155 while ((row = res->Next())) {
157 if ( ftypes.find(row->GetField(0)) == ftypes.end() ) {
158 ftypes[row->GetField(0)] = row->GetField(1);
161 cout <<
"key " << row->GetField(0) <<
" exist" << endl;
173 printf(
"%s\n",__FUNCTION__);
175 if ( db == NULL )
return false;
178 TSQLResult *
res = NULL;
180 cout <<
"SQL : " << sql << endl;
181 res = db->Query(sql.Data());
186 int nrows = res->GetRowCount();
188 if ( (list != NULL ) && (nrows > 0)) {
189 int nfields = res->GetFieldCount();
190 while ((mrow = res->Next()) != NULL) {;
192 for (
int j = 0; j < nfields; j++) {
194 fields[res->GetFieldName(j)] =mrow->GetField(j);
198 list->push_back(fields);
209 printf(
"%s\n",__FUNCTION__);
215 if ( ftypes.empty() ) {
216 printf(
"No table info or DB read error\n");
225 TSQLResult *
res = NULL;
230 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
231 if ( it != ftypes.begin()) sql +=
", ";
232 sql +=
"`" + it->first +
"`";
234 sql +=
" FROM `" + name +
"` WHERE `run` = ";
238 cout <<
"SQL : " << sql << endl;
242 res = db->Query(sql.Data());
248 int nrows = res->GetRowCount();
251 if (nrows != 1)
return false;
253 int nfields = res->GetFieldCount();
260 for (
int j = 0; j < nfields; j++) {
261 if (mrow->GetFieldLength(j) > 0 ) {
264 fields[res->GetFieldName(j)] = mrow->GetField(j);
276 if ( fields.empty() ) {
277 printf(
"Empty Row.\n");
279 for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
280 cout << it->first <<
" = "<< it->second << endl;
288 cout <<
" Table : " << name <<
"======================" << endl;
289 if ( ftypes.empty() ) {
290 printf(
"No table info or DB read error\n");
292 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
293 cout << it->first <<
" ("<< it->second <<
")" << endl;
301 return fields.find(field) != fields.end();
307 return ftypes.find(field) != ftypes.end();
313 dbFields::iterator it = fields.find(field);
314 if ( it != fields.end() ) {
324 dbFields::iterator it = fields.find(field);
325 if ( it != fields.end() ) {
334 TString sval; sval += val;
335 return SetVal(field,sval);
340 TString sval; sval += val;
341 return SetVal(field,sval);
348 for (dbFields::iterator it=fields.begin(); it!=fields.end();++it) {
349 if ( it->second.Contains(
"date") || it->second.Contains(
"time")
350 || it->second.Contains(
"char") )
362 dbFields::iterator it = ftypes.find(field);
363 if ( it != ftypes.end() ) {
373 dbFields::iterator it = ftypes.find(field);
374 if ( it != ftypes.end() ) {
375 it->second =
"expression";
386 printf(
"%s\n",__FUNCTION__);
394 sql +=
"DELETE FROM `" + name +
"` WHERE `run` = " + run +
";";
397 TSQLResult *
res = NULL;
398 res = db->Query(sql.Data());
411 printf(
"%s\n",__FUNCTION__);
413 if ( ftypes.empty() ) {
414 printf(
"No table info or DB read error\n");
419 printf(
"Can't update empty Row\n");
423 Bool_t verbose = option.Contains(
"verbose");
428 dbFields::iterator itype;
429 for (dbFields::iterator it=rfields.begin(); it!=rfields.end();++it) {
431 if (ftypes.find(it->first) != ftypes.end()) {
432 fields[it->first] = it->second;
436 for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
438 if ((itype =ftypes.find(it->first)) != ftypes.end() ) {
439 if ( itype->second.Contains(
"date") || itype->second.Contains(
"time")
440 || itype->second.Contains(
"char") )
442 it->second.Prepend(
'"');
443 it->second.Append(
'"');
454 sql +=
"UPDATE `" + name +
"` SET ";
455 for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
456 if ( it->first.EqualTo(
"run"))
continue;
457 if ( it != fields.begin()) sql +=
", ";
458 sql +=
"`" + it->first +
"` = " + it->second;
460 sql +=
" WHERE `run` = ";
464 cout <<
"SQL : " << sql << endl;
467 res = db->Query(sql.Data());
480 printf(
"%s\n",__FUNCTION__);
482 if ( ftypes.empty() ) {
483 printf(
"No table info or DB read error\n");
488 printf(
"Can't update empty Row\n");
492 Bool_t verbose = option.Contains(
"verbose");
493 Bool_t update = option.Contains(
"update");
497 dbFields::iterator itype;
498 for (dbFields::iterator it=rfields.begin(); it!=rfields.end();++it) {
500 if (ftypes.find(it->first) != ftypes.end()) {
501 fields[it->first] = it->second;
505 for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
507 if ((itype =ftypes.find(it->first)) != ftypes.end() ) {
508 if ( itype->second.Contains(
"date") || itype->second.Contains(
"time")
509 || itype->second.Contains(
"char") )
511 it->second.Prepend(
'"');
512 it->second.Append(
'"');
525 sql +=
"INSERT INTO `" + name +
"` (";
526 for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
527 if ( it != fields.begin()) {
531 sql +=
"`" + it->first +
"`";
533 if (update && (! it->first.EqualTo(
"run"))) {
534 if (it != fields.begin() ) ondup +=
", ";
535 ondup +=
"`" + it->first +
"`=VALUES(`" + it->first +
"`)" ;
542 sql +=
" ON DUPLICATE KEY UPDATE " + ondup;
545 cout <<
"SQL : " << sql << endl;
547 res = db->Query(sql.Data());
558 fout << #line << endl;
561 fout << line << endl;
563 #define wlc(pre,class,post) \
564 fout << #pre + class + #post << endl;
566 #define wqc(pre,class,post) \
567 fout << pre + class + post << endl;
572 if (cname.IsNull()) {
573 clname +=
"HarpoDB" + name;
577 TString cluname = clname;
581 fname = clname +
"LinkDef.h";
582 cout <<
"Writing " << fname << endl;
584 fout.open(fname.Data());
586 wl(#pragma link off all globals;)
587 wl(#pragma link off all classes;)
588 wl(#pragma link off all functions;)
590 wqc(
"#pragma link C++ class ",clname,
"+;")
596 fname = clname +
".h";
597 cout <<
"Writing " << fname << endl;
598 fout.open(fname.Data());
599 wqc(
"#ifndef ",cluname,
"_H")
600 wqc(
"#define ",cluname,
"_H")
601 wq(
"//////////////////////////////////////////////////////////////////////////")
603 wqc(
"// ",clname,
" (Generated by HarpoDBTable::MakeClass())")
605 wq(
"//////////////////////////////////////////////////////////////////////////")
606 wl(#include
"TObject.h")
607 wl(#include
"TString.h")
608 wl(#include
"HarpoDB.h")
610 wqc(
"class ",clname,
" : public TObject {")
612 wqc(
" ",clname,
"();")
613 wqc(
" virtual ~",clname,
"();")
617 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
618 if (it->second.Contains(
"bool") ) {
621 }
else if (it->second.Contains(
"int") ) {
623 }
else if (it->second.Contains(
"dec") || it->second.Contains(
"float") ||
624 it->second.Contains(
"real") || it->second.Contains(
"double") ) {
625 vartype =
"Double_t";
626 }
else if ( it->second.Contains(
"date") || it->second.Contains(
"time")
627 || it->second.Contains(
"char") ) {
632 fout <<
" " << vartype <<
" " << it->first <<
"; // " <<
636 wq(
" // Import From Raw")
637 wq(
" void Import(HarpoDBRow &row);")
639 wq(
" // Export to Raw")
640 wq(
" void Export(HarpoDBRow &row);")
652 wqc(
"ClassDef(",clname,
",1) //! ROOT CINT declaration")
656 wqc(
"#endif // ",cluname,
"_H")
657 wq(
"// Local Variables:")
663 fname = clname +
".cxx";
664 cout <<
"Writing " << fname << endl;
665 fout.open(fname.Data());
666 wq(
"//////////////////////////////////////////////////////////////////////////")
674 wqc("
#include \"",clname,".h\"")
675 wl(#include <iostream>)
677 wqc("ClassImp(",clname,")")
680 wqc("",clname + "::" + clname,"()")
682 wq(
" // Empy default constructor for ROOT IO")
686 wqc(
"",clname +
"::~" + clname,
"()")
691 wq(
"// Import From Row")
692 wqc(
"void ",clname,
"::Import(HarpoDBRow &row)")
697 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
698 if (it->second.Contains(
"bool") ) {
700 }
else if (it->second.Contains(
"int") ) {
702 }
else if (it->second.Contains(
"dec") || it->second.Contains(
"float") ||
703 it->second.Contains(
"real") || it->second.Contains(
"double") ) {
705 }
else if ( it->second.Contains(
"date") || it->second.Contains(
"time")
706 || it->second.Contains(
"char") ) {
711 fout <<
" if (row.GetVal(" <<
'"' << it->first <<
'"' <<
",var))" << endl;
712 fout <<
" " << it->first <<
" = var" << func << endl;
716 wq(
"// Export to Row")
717 wqc(
"void ",clname,
"::Export(HarpoDBRow &row)")
720 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
721 fout <<
" if (row.Lookup(" <<
'"' << it->first <<
'"' <<
"))" << endl;
722 fout <<
" row.SetVal(" <<
'"' << it->first <<
'"' <<
723 ", "<< it->first <<
");"<< endl;
727 wq(
"// Reset to zero values")
728 wqc(
"void ",clname,
"::Reset()")
731 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
732 if (it->second.Contains(
"bool") ) {
734 }
else if (it->second.Contains(
"int") ) {
736 }
else if (it->second.Contains(
"dec") || it->second.Contains(
"float") ||
737 it->second.Contains(
"real") || it->second.Contains(
"double") ) {
739 }
else if ( it->second.Contains(
"date") || it->second.Contains(
"time")
740 || it->second.Contains(
"char") ) {
745 fout <<
" " << it->first <<
" = " << def <<
";"<< endl;
750 wqc(
"void ",clname,
"::print()")
752 for (dbFields::iterator it=ftypes.begin(); it!=ftypes.end(); ++it) {
753 fout <<
" std::cout << ";
754 fout <<
"\"" << it->first <<
" = \" << " << it->first ;
755 fout <<
" << std::endl;" << endl;
759 wqc(
"// EOF ",clname,
".cxx")
Bool_t Open(const char *dbnm, const char *tbl)
Bool_t UpdateRunRow(HarpoDBRow row, const TString option="")
Bool_t GetRunRow(Long_t nrun, HarpoDBRow &row)
Query Table for given run information.
static void SetUser(TString u, TString p)
Bool_t Lookup(const TString field)
Lookup filed exist.
Bool_t Open(const TString *DbUrl=NULL)
static const char * GetDBName()
std::list< dbFields > dbResults
Long_t GetNrun()
Get method for run number of this record.
static TSQLServer * GetDBServer()
Bool_t GetTable(const TString name, HarpoDBTable *table)
Bool_t InsertRunRow(HarpoDBRow row, const TString option="")
Bool_t SetVal(const TString field, TString val)
Bool_t DeleteRun(Long_t run)
Delete run from table.
#define wqc(pre, class, post)
Bool_t Lookup(const TString field)
Lookup filed exist.
Bool_t GetVal(const TString field, TString &val)
Return value of existing field in Tstring.
Bool_t TypeExpression(const TString field)
Mark filed as expression for insert and update.
Bool_t ExecQuery(const TString sql, dbResults *list=NULL)
void list()
list all fields with values
std::map< TString, TString > dbFields
Bool_t GetType(const TString field, TString &type)
Return Type of field in TString.
void SetFields(dbFields f)
void MakeClass(TString cname, TString option="")