00001 00002 ////////////////////////////////////////////////////////////////////////// 00003 //////////////////////////// ROOT API //////////////////////////// 00004 ////////////////////////////////////////////////////////////////////////// 00005 00006 00007 #include <iostream> 00008 #include <sstream> 00009 #include <string> 00010 #include <vector> 00011 00012 #include "TString.h" 00013 00014 #include "TDbiStatement.hxx" 00015 #include "TDbiTableMetaData.hxx" 00016 #include <TSK_DBI_Log.hxx> 00017 #include <MsgFormat.h> 00018 using std::endl; 00019 #include "UtilString.hxx" 00020 00021 ClassImp(TDbiStatement) 00022 00023 00024 // Definition of static data members 00025 // ********************************* 00026 00027 00028 // Definition of all member functions (static or otherwise) 00029 // ******************************************************* 00030 // 00031 // - ordered: ctors, dtor, operators then in alphabetical order. 00032 00033 //..................................................................... 00034 00035 TDbiStatement::TDbiStatement(TDbiConnection& conDb) : 00036 fConDb(conDb) 00037 { 00038 // 00039 // 00040 // Purpose: Constructor 00041 // 00042 // Arguments: None. 00043 // 00044 // Return: 00045 // 00046 // conDb in The connection associated with the statement. 00047 // 00048 00049 00050 SK_DBI_Trace( "Creating TDbiStatement" << " "); 00051 fConDb.ConnectStatement(); 00052 00053 } 00054 00055 //..................................................................... 00056 00057 TDbiStatement::~TDbiStatement() { 00058 // 00059 // 00060 // Purpose: Destructor 00061 00062 SK_DBI_Trace( "Destroying TDbiStatement" << " "); 00063 00064 fConDb.DisConnectStatement(); 00065 } 00066 00067 //..................................................................... 00068 00069 TSQLStatement* TDbiStatement::CreateProcessedStatement(const TString& sql /* ="" */) { 00070 00071 // Attempt to create a processed statement (caller must delete). Return 0 if failure. 00072 00073 TSQLStatement* stmt = fConDb.CreatePreparedStatement(sql.Data()); 00074 if ( ! stmt ) { 00075 this->AppendExceptionLog(fConDb); 00076 return 0; 00077 } 00078 if ( stmt->Process() ) return stmt; 00079 this->AppendExceptionLog(stmt); 00080 delete stmt; 00081 stmt = 0; 00082 return 0; 00083 00084 } 00085 00086 00087 //..................................................................... 00088 00089 TSQLStatement* TDbiStatement::ExecuteQuery( const TString& sql) { 00090 // 00091 // 00092 // Purpose: Execute SQL. 00093 // Return: TSQLStatement with Process() and StoreResult() already performed. 00094 00095 this->ClearExceptionLog(); 00096 00097 SK_DBI_Info( "SQL:" << fConDb.GetDbName() << ":" << sql << " "); 00098 TSQLStatement* stmt = this->CreateProcessedStatement(sql); 00099 if ( ! stmt ) return 0; 00100 if ( ! stmt->StoreResult() ) { 00101 this->AppendExceptionLog(stmt); 00102 delete stmt; 00103 stmt = 0; 00104 } 00105 00106 // Final sanity check: If there is a statement then the exception log should still 00107 // be clear otherwise it should not be. 00108 if ( stmt ) { 00109 if ( ! fExceptionLog.IsEmpty() ) { 00110 delete stmt; 00111 stmt = 0; 00112 } 00113 } 00114 else if ( fExceptionLog.IsEmpty() ) { 00115 ostringstream oss; 00116 oss << "Unknown failure (no execption but no TSQLStatement either executing " << sql; 00117 fExceptionLog.AddEntry(oss.str().c_str()); 00118 } 00119 return stmt; 00120 00121 } 00122 00123 //..................................................................... 00124 00125 Bool_t TDbiStatement::ExecuteUpdate( const TString& sql) { 00126 // 00127 // 00128 // Purpose: Translate SQL if required and Execute. 00129 // 00130 // Return true if all updates successful. 00131 00132 00133 this->ClearExceptionLog(); 00134 00135 SK_DBI_Info( "SQL:" << fConDb.GetDbName() << ":" << sql << " "); 00136 bool ok = fConDb.GetServer()->Exec(sql.Data()); 00137 if ( ! ok ) { 00138 fConDb.RecordException(); 00139 this->AppendExceptionLog(fConDb); 00140 return false; 00141 } 00142 00143 return fExceptionLog.IsEmpty(); 00144 00145 } 00146 00147 //..................................................................... 00148 00149 Bool_t TDbiStatement::PrintExceptions(Int_t level) const { 00150 00151 // Purpose: Print accumulated exceptions at supplied Msg level, 00152 // add them to the Global Exception Log if level >= kWarning 00153 // and return true if there are any. 00154 00155 const TDbiExceptionLog& el(this->GetExceptionLog()); 00156 if ( el.IsEmpty() ) return false; 00157 00158 return true; 00159 00160 } 00161 00162