Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSQL.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TSQL.cxx
1 // $Id: TSQL.cxx,v 1.2 2007/02/28 21:32:46 phnxbld Exp $
2 //*-- Author : Valeriy Onuchin 14/02/2000
3 //
4 
6 //
7 // Base class for RDBC. It provides error and exception handling.
8 //
9 // =============================================================================
10 //______________________________________________________________________________
11 //*-*-*-*-*-*-*-*-*-*-* A simple RDBC example *-*-*-*-*-*-*-*-*-**-*-*-*-*-*
12 // ===========================================
13 //___________________________________________________________________
14 //void RDBCexample(const Text_t* dsn="minos")
15 //{
16 // // Set exception handler function
17 // TSQL::SetHandler("Catch(TSQLException*)");
18 //
19 // TSQLConnection* myConnection = TSQLDriverManager::GetConnection( dsn,
20 // "scott",
21 // "tiger" );
22 // if(!myConnection) return; // return on error
23 //
24 // // Create a statement...
25 // TSQLStatement* myStatement = myConnection->CreateStatement();
26 // if(!myStatement) return; // return on error
27 //
28 // // Execute the query...
29 // Bool_t success = myStatement->Execute( "select * from emp" );
30 // if(!success) return; // return on error
31 //
32 // // Get the result set...
33 // TSQLResultSet* mySet = myStatement->GetResultSet();
34 // if(!mySet) return; // return on error
35 //
36 // // Advance to the next row...
37 // while ( mySet->Next() ) {
38 //
39 // // Get the data...
40 // int empno = mySet->GetInt( "empno" );
41 // TString ename = mySet->GetString( "ename" );
42 // long salary = mySet->GetLong( "sal" );
43 //
44 // // Print it all out...
45 // printf( "%d - %s - %dn",empno,ename.Data(),salary );
46 // }
47 //
48 // // Close everything up...
49 // delete myStatement;
50 // delete myConnection;
51 //}
52 //
53 //___________________________________________________________________
54 //void Catch(TSQLException* e)
55 //{
56 // // exception handle function
57 //
58 // TString str = e->GetMessage();
59 // printf("SQL Error: %sn",str.Data());
60 //}
61 //
62 //
64 //
65 // See also:
66 // TSQLException TSQLStatement TSQLPreparedStatement TSQLResultSet
67 // TSQLCallableStatement TSQLResultSet TSQLDatabaseMetaData
68 // TSQLDriverManager TSQLResultSetMetaData TSQLWarning TSQLDate
69 // TSQLTime TSQLTimestamp
70 //
72 
73 #include <RDBC/TSQL.h>
74 #include <TClass.h>
75 
76 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
77 #include <TList.h>
78 #endif
79 
82 
84 void TSQLException::Print(Option_t * /* option */) const
85 {
86  // dump information about this exception
87 
88  printf( "%s %s %d\n",
89  GetMessage().Data(),
90  GetSQLState().Data(),
91  GetErrorCode() );
92 }
93 
95 //___________________________________________________________________
96 TSQL::TSQL(void* imp):TQObject()
97 {
98  // ctor
99 
100  fWarnings = new TList();
101  fImp = imp;
102 }
103 
104 //___________________________________________________________________
106 {
107  // dtor
108 
109  ClearWarnings();
110  delete fWarnings;
111 }
112 
113 //___________________________________________________________________
115 {
116  // exception handling service
117  //
118  // Warning: shared exceptions are not allowed yet
119 
120  if(gDebug) {
121  printf("%s:\t",IsA()->GetName());
122  e->Print();
123  }
124 
125  fWarnings->Add(e);
126  e->AddReference();
127  Emit("Throw(TSQLException*)",(long)e);
128 }
129 
130 //___________________________________________________________________
132 {
133  // Clears all warnings reported for this TSQL object.
134 
135  fWarnings->Delete();
136 }
137 
138 //___________________________________________________________________
139 Bool_t TSQL::SetHandler(const TString& handler)
140 {
141  // connect any TSQLxxx object to handler function
142 
143  Bool_t failed = kFALSE;
144 
145  UnsetHandler();
146 
147  failed |= !TQObject::Connect("TSQL","Throw(TSQLException*)",
148  (const Text_t *)0,(void*)0,handler.Data());
149  failed |= !TQObject::Connect("TSQLCallableStatement","Throw(TSQLException*)",
150  (const Text_t *)0,(void*)0,handler.Data());
151  failed |= !TQObject::Connect("TSQLConnection","Throw(TSQLException*)",
152  (const Text_t *)0,(void*)0,handler.Data());
153  failed |= !TQObject::Connect("TSQLDatabaseMetaData","Throw(TSQLException*)",
154  (const Text_t *)0,(void*)0,handler.Data());
155  failed |= !TQObject::Connect("TSQLDriverManager","Throw(TSQLException*)",
156  (const Text_t *)0,(void*)0,handler.Data());
157  failed |= !TQObject::Connect("TSQLPreparedStatement","Throw(TSQLException*)",
158  (const Text_t *)0,(void*)0,handler.Data());
159  failed |= !TQObject::Connect("TSQLResultSet","Throw(TSQLException*)",
160  (const Text_t *)0,(void*)0,handler.Data());
161  failed |= !TQObject::Connect("TSQLResultSetMetaData","Throw(TSQLException*)",
162  (const Text_t *)0,(void*)0,handler.Data());
163  failed |= !TQObject::Connect("TSQLStatement","Throw(TSQLException*)",
164  (const Text_t *)0,(void*)0,handler.Data());
165 
166  return !failed;
167 }
168 
169 //___________________________________________________________________
170 Bool_t TSQL::UnsetHandler(const TString& handler)
171 {
172  // disconnect everything from handler function
173 
174  const Text_t* slot;
175  Bool_t failed = kFALSE;
176 
177  if(handler.IsNull()) {
178  slot = 0;
179  } else {
180  slot = handler.Data();
181  }
182 
183  failed |= !TQObject::Disconnect( "TSQL",
184  "Throw(TSQLException*)",0,slot );
185  failed |= !TQObject::Disconnect( "TSQLCallableStatement",
186  "Throw(TSQLException*)",0,slot );
187  failed |= !TQObject::Disconnect( "TSQLConnection",
188  "Throw(TSQLException*)",0,slot );
189  failed |= !TQObject::Disconnect( "TSQLDatabaseMetaData",
190  "Throw(TSQLException*)",0,slot );
191  failed |= !TQObject::Disconnect( "TSQLDriverManager",
192  "Throw(TSQLException*)",0,slot );
193  failed |= !TQObject::Disconnect( "TSQLPreparedStatement",
194  "Throw(TSQLException*)",0,slot );
195  failed |= !TQObject::Disconnect( "TSQLResultSet",
196  "Throw(TSQLException*)",0,slot );
197  failed |= !TQObject::Disconnect( "TSQLResultSetMetaData",
198  "Throw(TSQLException*)",0,slot );
199  failed |= !TQObject::Disconnect( "TSQLStatement",
200  "Throw(TSQLException*)",0,slot );
201  return !failed;
202 }
203