Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:06

0001 // $Id: TSQL.cxx,v 1.2 2007/02/28 21:32:46 phnxbld Exp $
0002 //*-- Author : Valeriy Onuchin 14/02/2000 
0003 //
0004 
0005 ////////////////////////////////////////////////////////////////////
0006 //
0007 //  Base class for RDBC.  It provides error and exception handling.
0008 //
0009 // =============================================================================
0010 //______________________________________________________________________________
0011 //*-*-*-*-*-*-*-*-*-*-*   A simple RDBC example    *-*-*-*-*-*-*-*-*-**-*-*-*-*-*
0012 //            ===========================================
0013 //___________________________________________________________________
0014 //void RDBCexample(const Text_t* dsn="minos")
0015 //{
0016 //   //   Set exception handler function
0017 //   TSQL::SetHandler("Catch(TSQLException*)");
0018 //
0019 //  TSQLConnection* myConnection = TSQLDriverManager::GetConnection( dsn, 
0020 //                                                                   "scott", 
0021 //                                                                   "tiger" );
0022 //   if(!myConnection) return;  // return on error
0023 //      
0024 //   //    Create a statement...
0025 //   TSQLStatement* myStatement = myConnection->CreateStatement();
0026 //   if(!myStatement) return;   // return on error
0027 //      
0028 //   //    Execute the query...
0029 //   Bool_t success = myStatement->Execute( "select * from emp" );
0030 //   if(!success) return; // return on error
0031 //   
0032 //   //    Get the result set...
0033 //   TSQLResultSet* mySet = myStatement->GetResultSet();
0034 //   if(!mySet) return; // return on error
0035 //
0036 //   //    Advance to the next row...
0037 //   while ( mySet->Next() ) { 
0038 //                            
0039 //     //    Get the data...
0040 //      int empno = mySet->GetInt( "empno" );
0041 //      TString ename = mySet->GetString( "ename" );
0042 //      long salary = mySet->GetLong( "sal" );
0043 //
0044 //      //    Print it all out...
0045 //      printf( "%d - %s - %dn",empno,ename.Data(),salary );
0046 //   } 
0047 //      
0048 //   //    Close everything up...
0049 //   delete myStatement; 
0050 //   delete myConnection; 
0051 //}
0052 // 
0053 //___________________________________________________________________
0054 //void Catch(TSQLException* e)
0055 //{
0056 //   // exception handle function
0057 //   
0058 //   TString str = e->GetMessage();
0059 //   printf("SQL Error: %sn",str.Data()); 
0060 //}
0061 //
0062 //
0063 ////////////////////////////////////////////////////////////////////
0064 //
0065 // See also:
0066 //    TSQLException  TSQLStatement TSQLPreparedStatement  TSQLResultSet
0067 //    TSQLCallableStatement TSQLResultSet TSQLDatabaseMetaData
0068 //    TSQLDriverManager TSQLResultSetMetaData  TSQLWarning TSQLDate 
0069 //    TSQLTime TSQLTimestamp
0070 //
0071 ////////////////////////////////////////////////////////////////////
0072 
0073 #include <RDBC/TSQL.h>
0074 #include <TClass.h>
0075 
0076 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
0077 #include <TList.h>
0078 #endif
0079 
0080 ClassImpQ(TSQL)
0081 ClassImp(TSQLException)
0082 
0083 /////////////////////////////////////////////////////////////////////
0084 void TSQLException::Print(Option_t * /* option */) const
0085 {
0086    // dump information about this exception 
0087 
0088    printf( "%s %s %d\n",
0089              GetMessage().Data(),
0090              GetSQLState().Data(),
0091              GetErrorCode() );
0092 }
0093 
0094 /////////////////////////////////////////////////////////////////////
0095 //___________________________________________________________________
0096 TSQL::TSQL(void* imp):TQObject()
0097 {
0098    // ctor
0099 
0100    fWarnings = new TList();   
0101    fImp = imp;
0102 }
0103 
0104 //___________________________________________________________________
0105 TSQL::~TSQL()
0106 {
0107    // dtor
0108    
0109    ClearWarnings();
0110    delete fWarnings;
0111 }
0112 
0113 //___________________________________________________________________
0114 void TSQL::Throw( TSQLException* e )
0115 {
0116    // exception handling service 
0117    // 
0118    // Warning: shared exceptions are not allowed yet
0119 
0120    if(gDebug) {
0121       printf("%s:\t",IsA()->GetName());
0122       e->Print();
0123    }
0124 
0125    fWarnings->Add(e);
0126    e->AddReference(); 
0127    Emit("Throw(TSQLException*)",(long)e);
0128 }
0129 
0130 //___________________________________________________________________
0131 void TSQL::ClearWarnings()
0132 {
0133    // Clears all warnings reported for this TSQL object. 
0134    
0135    fWarnings->Delete();
0136 }
0137 
0138 //___________________________________________________________________
0139 Bool_t TSQL::SetHandler(const TString& handler)
0140 {
0141    // connect any TSQLxxx object to handler function 
0142    
0143    Bool_t failed = kFALSE;
0144    
0145    UnsetHandler();
0146       
0147    failed |= !TQObject::Connect("TSQL","Throw(TSQLException*)",
0148                      (const Text_t *)0,(void*)0,handler.Data());
0149    failed |= !TQObject::Connect("TSQLCallableStatement","Throw(TSQLException*)",
0150                      (const Text_t *)0,(void*)0,handler.Data());
0151    failed |= !TQObject::Connect("TSQLConnection","Throw(TSQLException*)",
0152                      (const Text_t *)0,(void*)0,handler.Data());
0153    failed |= !TQObject::Connect("TSQLDatabaseMetaData","Throw(TSQLException*)",
0154                      (const Text_t *)0,(void*)0,handler.Data());
0155    failed |= !TQObject::Connect("TSQLDriverManager","Throw(TSQLException*)",
0156                      (const Text_t *)0,(void*)0,handler.Data());
0157    failed |= !TQObject::Connect("TSQLPreparedStatement","Throw(TSQLException*)",
0158                      (const Text_t *)0,(void*)0,handler.Data());
0159    failed |= !TQObject::Connect("TSQLResultSet","Throw(TSQLException*)",
0160                      (const Text_t *)0,(void*)0,handler.Data());
0161    failed |= !TQObject::Connect("TSQLResultSetMetaData","Throw(TSQLException*)",
0162                      (const Text_t *)0,(void*)0,handler.Data());
0163    failed |= !TQObject::Connect("TSQLStatement","Throw(TSQLException*)",
0164                      (const Text_t *)0,(void*)0,handler.Data());
0165    
0166    return !failed;
0167 }
0168 
0169 //___________________________________________________________________
0170 Bool_t TSQL::UnsetHandler(const TString& handler)
0171 {
0172    // disconnect everything from handler function
0173    
0174    const Text_t* slot;
0175    Bool_t failed = kFALSE;
0176    
0177    if(handler.IsNull()) {
0178       slot = 0;
0179    } else {
0180       slot = handler.Data();
0181    }
0182       
0183    failed |= !TQObject::Disconnect( "TSQL",
0184                                     "Throw(TSQLException*)",0,slot );
0185    failed |= !TQObject::Disconnect( "TSQLCallableStatement",
0186                                     "Throw(TSQLException*)",0,slot );
0187    failed |= !TQObject::Disconnect( "TSQLConnection",
0188                                     "Throw(TSQLException*)",0,slot );
0189    failed |= !TQObject::Disconnect( "TSQLDatabaseMetaData",
0190                                     "Throw(TSQLException*)",0,slot );
0191    failed |= !TQObject::Disconnect( "TSQLDriverManager",
0192                                     "Throw(TSQLException*)",0,slot );
0193    failed |= !TQObject::Disconnect( "TSQLPreparedStatement",
0194                                     "Throw(TSQLException*)",0,slot );
0195    failed |= !TQObject::Disconnect( "TSQLResultSet",
0196                                     "Throw(TSQLException*)",0,slot );
0197    failed |= !TQObject::Disconnect( "TSQLResultSetMetaData",
0198                                     "Throw(TSQLException*)",0,slot );
0199    failed |= !TQObject::Disconnect( "TSQLStatement",
0200                                     "Throw(TSQLException*)",0,slot );
0201    return !failed;   
0202 }
0203