8 #include "TSQLStatement.h"
35 const char* strUser = gSystem->Getenv(
"ENV_TSQL_UPDATE_USER");
36 if ( ! strUser ) { strUser = gSystem->Getenv(
"ENV_TSQL_USER"); }
37 const char* strPswd = gSystem->Getenv(
"ENV_TSQL_UPDATE_PSWD");
38 if ( ! strPswd ) { strPswd = gSystem->Getenv(
"ENV_TSQL_PSWD"); }
39 const char* strUrl = gSystem->Getenv(
"ENV_TSQL_UPDATE_URL");
40 if ( !strUrl ) { strUrl = gSystem->Getenv(
"ENV_TSQL_URL"); }
41 string userList = ( strUser ) ? strUser :
"";
42 string pswdList = ( strPswd ) ? strPswd :
"";
43 string urlList = ( strUrl ) ? strUrl :
"";
45 if ( urlList ==
"" || userList ==
"" || pswdList ==
"" ) {
46 cout <<
"-E- FairDbMultConnector() "
47 <<
"Cannnot open a Database cascade;\n"
48 <<
" the environmental variables ENV_DB_*:-" << endl
49 <<
"USER: \"" << userList <<
"\" PSWD:\"" << pswdList
50 <<
"\" URL:\"" << urlList << endl
51 <<
" are either not defined or empty.\n"
52 <<
" Please check your settings of ENV_DB_USER,"
53 <<
" ENV_DB_PSWD and ENV_DB_URL\n"
54 <<
"Aborting due to above errors " << endl;
58 std::vector<std::string> users, pswds, urls;
65 for (
unsigned entry = 0; entry < urls.size(); ++entry ) {
66 string url = urls[entry];
67 string user = ( entry >= users.size() ) ? users[0] : users[entry];
68 string pswd = ( entry >= pswds.size() ) ? pswds[0] : pswds[entry];
72 if ( pswd ==
"\\0" ) { pswd =
""; }
75 fConnections.push_back(con);
76 if ( ! con->
Open() ) {
82 if ( fGlobalSeqNoDbNo != -1 ) {
continue; }
84 if ( ! stmtDb.get() ) {
continue; }
85 TSQLStatement* stmt = stmtDb->
ExecuteQuery(
"Select * from GLOBALSEQNO where 1=0");
87 fGlobalSeqNoDbNo = fConnections.size()-1;
93 if ( this->GetTableDbNo(
"DBI_STATE_FLAGS",entry) != -1 ) {
94 cout <<
" -E- FairDbMultConnector POSSIBLE VERSION SHEAR DETECTED !!!\n"
95 <<
" The DB_STATE_FLAGS table is present on cascade entry " << entry <<
". This table will\n"
96 <<
" only be introduced to manage backward incompatible changes that could lead\n"
97 <<
" to version shear between the code and the database. This version of the\n"
98 <<
" code does not support the change the presence of that table indicates\n"
99 <<
" so has to shut down. \n";
109 cout <<
" -E- FairDbMultConnector() Aborting due to above errors" << endl;
117 : fGlobalSeqNoDbNo(conn.fGlobalSeqNoDbNo),
118 fConnections(conn.fConnections),
119 fTemporaryTables(conn.fTemporaryTables)
132 cout <<
"-I- FairDbMultConnector : Destroying FairDbMultConnector"
134 for (Int_t dbNo = this->
GetNumDb()-1; dbNo >= 0; --dbNo) {
145 os <<
"-I- FairDbMultConnector() Status:- " << endl
146 <<
"Status URL" << endl << endl;
149 for (Int_t dbNo = 0; dbNo < maxDb; ++dbNo)
152 << cascader.
GetURL(dbNo) << endl;
168 if ( requireGlobal > 0
171 cout <<
" -I- FairDbMultConnector: Unable to issue global SEQNO - no authorising DB in cascade\n"
172 <<
" will issue local one instead" << endl;
173 }
else if ( isTemporary ) {
174 cout <<
" -I- FairDbMultConnector: Unable to issue global SEQNO - " << tableName <<
" is temporary\n"
175 <<
" will issue local one instead" << endl;
210 const string& tableDescr)
215 || tableDescr[0] !=
'('
216 || tableDescr[tableDescr.size()-1] !=
')' ) {
217 cout <<
"-I- FairDbMultConnector:: Illegal input args:-" << endl
218 <<
" Table Name: " << tableName
219 <<
" Table Description: " << tableDescr
228 auto_ptr<FairDbStatement> stmtDb;
229 for (UInt_t dbNoTry = 0; dbNoTry <
fConnections.size(); ++dbNoTry ) {
231 if ( stmtDb.get() ) {
232 sqlMakeTable =
" create temporary table ";
233 sqlMakeTable += tableName;
235 sqlMakeTable += tableDescr;
257 cout <<
"-I- FairDbMultConnector: Making connection: " << conDb.
GetUrl()
258 <<
" permanent to preserve temporary tables." << endl;
264 cout <<
"-I- FairDbMultConnector: Validity Table creation: "
265 <<
" Database: " << dbNoAcc <<
" "
266 << sqlMakeTable << endl;
299 else { cout <<
"-I- FairDbMultConnector: Database does not contain entry " << dbNo << endl; }
307 for (
unsigned dbNo = 0; dbNo < this->
GetNumDb(); ++dbNo) {
311 cout <<
"-I- FairDbMultConnector:Database does not contain entry " << dbName << endl;
333 Int_t selectDbNo )
const
335 string::const_iterator itr = tableName.begin();
336 string::const_iterator itrEnd = tableName.end();
337 while ( itr != itrEnd )
if ( islower(*itr++) ) {
return -1; }
340 for (UInt_t dbNoTry = 0; dbNoTry <
fConnections.size(); ++dbNoTry ) {
341 if ( selectDbNo >= 0 && (UInt_t) selectDbNo != dbNoTry ) {
continue; }
343 if ( con && con->
TableExists(tableName) ) {
return dbNoTry; }
354 for (UInt_t dbNo = 0; dbNo <
fConnections.size(); ++dbNo ) {
363 map<string,Int_t>::const_iterator itr
366 && (*itr).second == dbNo );
374 fSeqnoTableName(seqnoTable),
375 fDataTableName(dataTable),
379 cout <<
"-E FairDbMultConnector::Block Cannot obtain statment to set lock" << endl;
390 this->SetBLock(kFALSE);
398 if ( setting == fLocked || ! fStmt ) {
return; }
408 sql =
"LOCK TABLES ";
409 sql += fSeqnoTableName +
" WRITE";
410 if ( fDataTableName !=
"" ) { sql +=
", " + fDataTableName +
"VAL WRITE"; }
412 sql =
"UNLOCK TABLES;";
414 cout <<
"Lock requested: " << setting
415 <<
" issuing lock command: " << sql << endl;
416 fStmt->ExecuteUpdate(sql.c_str());
417 if ( fStmt->GetExceptionLog().IsEmpty() ) { fLocked = setting; }
418 fStmt->PrintExceptions();
425 for (UInt_t dbNo = 0; dbNo <
fConnections.size(); ++dbNo ) {
437 string seqnoTableName = isGlobal ?
"GLOBALSEQNO" :
"LOCALSEQNO";
438 bool seqnoTableNameExists = this->
TableExists(seqnoTableName,dbNo);
439 bool tableNameExists = this->
TableExists(tableName,dbNo);
442 if ( ! stmtDb.get() ) {
return 0; }
447 if ( ! seqnoTableNameExists ) {
448 cout<<
"-I- FairDbMultConnector: Unable to issue global SEQNO - " << dbNo
449 <<
" is not an authorising DB" << endl;
453 if ( ! seqnoTableNameExists ) {
455 sql <<
"CREATE TABLE " << seqnoTableName
456 <<
"(TABLENAME CHAR(64) NOT NULL PRIMARY KEY,\n"
457 <<
" LASTUSEDSEQNO INT )";
459 cout<<
"-I- FairDbMultConnector: Database: " << dbNo
460 <<
" create local SEQNO table query: " << sql.
c_str() << endl;
464 sql <<
"INSERT INTO " << seqnoTableName <<
" VALUES ('*',0)";
465 cout<<
"-I- FairDbMultConnector: Database: " << dbNo
466 <<
" prime local SEQNO table query: " << sql.
c_str() << endl;
479 && tableNameExists ) { dataTable = tableName; }
482 cout<<
"-I- FairDbMultConnector: Unable to lock " << seqnoTableName << endl;
489 sql <<
"select * from " << seqnoTableName <<
" where TABLENAME = '*' or TABLENAME = '";
490 sql << tableName +
"' order by TABLENAME";
491 cout<<
"-I- FairDbMultConnector: "<< seqnoTableName <<
" query: " << sql.
c_str() << endl;
494 Int_t seqNoDefault = 0;
495 if ( stmt && stmt->NextResultRow() ) {
496 seqNoDefault = stmt->GetInt(1);
498 cout<<
"-I- FairDbMultConnector: Unable to find default SeqNo"
499 <<
" due to above error" << endl;
504 Int_t seqNoTable = seqNoDefault;
505 if ( stmt->NextResultRow() ) {
506 seqNoTable = stmt->GetInt(1);
510 cout<<
"-I- FairDbMultConnector: query returned last used seqno: " << seqNoTable << endl;
516 static std::string checkedCombinations;
517 ostringstream combination;
518 combination <<
":" << tableName << isGlobal << dbNo <<
":";
519 bool notChecked = checkedCombinations.find(combination.str()) == std::string::npos;
520 if ( notChecked ) { checkedCombinations += combination.str(); }
521 if ( tableNameExists && notChecked ) {
522 Int_t seqNoMin = seqNoDefault;
525 sql <<
"select max(SEQNO) from " << tableName <<
"VAL"
526 <<
" where SEQNO between " << seqNoMin <<
" and " << seqNoMax;
527 cout<<
"-I- FairDbMultConnector: Database: " << dbNo
528 <<
" max SEQNO query: " << sql.
c_str() << endl;
533 if ( stmt && stmt->NextResultRow() && ! stmt->IsNull(0) ) {
534 minValue = stmt->GetInt(0);
535 if ( minValue <= 0 ) { minValue = 0; }
540 if ( minValue > seqNoTable ) {
541 cout<<
"-I- FairDbMultConnector: "
542 <<
"Database: " << dbNo <<
" "
543 << seqnoTableName <<
" has last used SEQNO of "
544 << seqNoTable <<
" for table " << tableName
545 <<
",\n but the highest SEQNO in the band " << seqNoMin <<
" to " << seqNoMax
546 <<
" is " << minValue <<
" for that table\n "
547 << seqnoTableName <<
" is out of date! It will be updated for " << tableName << endl;
548 seqNoTable = minValue;
555 sql <<
"delete from " << seqnoTableName <<
" where TABLENAME='";
556 sql << tableName +
"'";
557 cout<<
"-I- FairDbMultConnector: SEQNO entry removal: "
558 << sql.
c_str() << endl;
565 sql <<
"insert into " << seqnoTableName <<
" values('";
566 sql << tableName +
"'," << seqNoTable <<
")";
567 cout<<
"-I- FairDbMultConnector: SEQNO entry add: "
568 << sql.
c_str() << endl;