Logo ROOT   6.12/07
Reference Guide
TSQLServer.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: Fons Rademakers 25/11/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TSQLServer //
15 // //
16 // Abstract base class defining interface to a SQL server. //
17 // //
18 // To open a connection to a server use the static method Connect(). //
19 // The db argument of Connect() is of the form: //
20 // <dbms>://<host>[:<port>][/<database>], e.g. //
21 // mysql://pcroot.cern.ch:3456/test, oracle://srv1.cern.ch/main, ... //
22 // Depending on the <dbms> specified an appropriate plugin library //
23 // will be loaded which will provide the real interface. //
24 // For SQLite, the syntax is slightly different: //
25 // sqlite://<database> //
26 // The string 'database' is directly passed to sqlite3_open(_v2), //
27 // so e.g. a filename or ":memory:" are possible values. //
28 // For SQLite versions >= 3.7.7, SQLITE_OPEN_URI is activated to also //
29 // allow URI-parameters if needed. //
30 // //
31 // Related classes are TSQLResult and TSQLRow. //
32 // //
33 //////////////////////////////////////////////////////////////////////////
34 
35 #include "TSQLServer.h"
36 #include "TSQLResult.h"
37 #include "TSQLRow.h"
38 #include "TSQLTableInfo.h"
39 #include "TSQLColumnInfo.h"
40 #include "TROOT.h"
41 #include "TList.h"
42 #include "TObjString.h"
43 #include "TPluginManager.h"
44 #include "TVirtualMutex.h"
45 
47 
48 
49 const char* TSQLServer::fgFloatFmt = "%e";
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// The db should be of the form: <dbms>://<host>[:<port>][/<database>],
54 /// e.g.: mysql://pcroot.cern.ch:3456/test, oracle://srv1.cern.ch/main,
55 /// pgsql://... or sapdb://...
56 /// The uid is the username and pw the password that should be used for
57 /// the connection. Depending on the <dbms> the shared library (plugin)
58 /// for the selected system will be loaded. When the connection could not
59 /// be opened 0 is returned.
60 
61 TSQLServer *TSQLServer::Connect(const char *db, const char *uid, const char *pw)
62 {
64  TSQLServer *serv = 0;
65 
66  if ((h = gROOT->GetPluginManager()->FindHandler("TSQLServer", db))) {
67  if (h->LoadPlugin() == -1)
68  return 0;
69  serv = (TSQLServer *) h->ExecPlugin(3, db, uid, pw);
70  }
71 
72  if (serv && serv->IsZombie()) {
73  delete serv;
74  serv = 0;
75  }
76 
77  return serv;
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Execute sql query.
82 /// Usefull for commands like DROP TABLE or INSERT, where result set
83 /// is not interested. Return kTRUE if no error
84 
85 Bool_t TSQLServer::Exec(const char* sql)
86 {
87  TSQLResult* res = Query(sql);
88  if (res==0) return kFALSE;
89 
90  delete res;
91 
92  return !IsError();
93 }
94 
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// returns error code of last operation
98 /// if res==0, no error
99 /// Each specific implementation of TSQLServer provides its own error coding
100 
102 {
103  return fErrorCode;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// returns error message of last operation
108 /// if no errors, return 0
109 /// Each specific implementation of TSQLServer provides its own error messages
110 
111 const char* TSQLServer::GetErrorMsg() const
112 {
113  return GetErrorCode()==0 ? 0 : fErrorMsg.Data();
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// reset error fields
118 
120 {
121  fErrorCode = 0;
122  fErrorMsg = "";
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// set new values for error fields
127 /// if method is specified, displays error message
128 
129 void TSQLServer::SetError(Int_t code, const char* msg, const char* method)
130 {
131  fErrorCode = code;
132  fErrorMsg = msg;
133  if ((method!=0) && fErrorOut)
134  Error(method,"Code: %d Msg: %s", code, (msg ? msg : "No message"));
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// submit "START TRANSACTION" query to database
139 /// return kTRUE, if successful
140 
142 {
143  return Exec("START TRANSACTION");
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// submit "COMMIT" query to database
148 /// return kTRUE, if successful
149 
151 {
152  return Exec("COMMIT");
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// submit "ROLLBACK" query to database
157 /// return kTRUE, if successful
158 
160 {
161  return Exec("ROLLBACK");
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Return list of user tables
166 /// Parameter wild specifies wildcard for table names.
167 /// It either contains exact table name to verify that table is exists or
168 /// wildcard with "%" (any number of symbols) and "_" (exactly one symbol).
169 /// Example of vaild wildcards: "%", "%name","___user__".
170 /// If wild=="", list of all available tables will be produced.
171 /// List contain just tables names in the TObjString.
172 /// List must be deleted by the user.
173 /// Example code of method usage:
174 ///
175 /// TList* lst = serv->GetTablesList();
176 /// TIter next(lst);
177 /// TObject* obj;
178 /// while (obj = next())
179 /// std::cout << "Table: " << obj->GetName() << std::endl;
180 /// delete lst;
181 
183 {
184  TSQLResult* res = GetTables(fDB.Data(), wild);
185  if (res==0) return 0;
186 
187  TList* lst = 0;
188  TSQLRow* row = 0;
189  while ((row = res->Next())!=0) {
190  const char* tablename = row->GetField(0);
191  if (lst==0) {
192  lst = new TList;
193  lst->SetOwner(kTRUE);
194  }
195  lst->Add(new TObjString(tablename));
196  delete row;
197  }
198 
199  delete res;
200 
201  return lst;
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Tests if table of that name exists in database
206 /// Return kTRUE, if table exists
207 
208 Bool_t TSQLServer::HasTable(const char* tablename)
209 {
210  if ((tablename==0) || (strlen(tablename)==0)) return kFALSE;
211 
212  TList* lst = GetTablesList(tablename);
213  if (lst==0) return kFALSE;
214 
215  Bool_t res = kFALSE;
216 
217  TObject* obj = 0;
218  TIter iter(lst);
219 
220  // Can be, that tablename contains "_" or "%" symbols, which are wildcards in SQL,
221  // therefore more than one table can be returned as result.
222  // One should check that exactly same name is appears
223 
224  while ((obj = iter()) != 0)
225  if (strcmp(tablename, obj->GetName())==0) res = kTRUE;
226 
227  delete lst;
228  return res;
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// Producec TSQLTableInfo object, which contain info about
233 /// table itself and each table column
234 /// Object must be deleted by user.
235 
237 {
238  if ((tablename==0) || (*tablename==0)) return 0;
239 
240  TSQLResult* res = GetColumns(fDB.Data(), tablename);
241  if (res==0) return 0;
242 
243  TList* lst = 0;
244  TSQLRow* row = 0;
245  while ((row = res->Next())!=0) {
246  const char* columnname = row->GetField(0);
247  if (lst==0) lst = new TList;
248  lst->Add(new TSQLColumnInfo(columnname));
249  delete row;
250  }
251 
252  delete res;
253 
254  return new TSQLTableInfo(tablename, lst);
255 }
256 
257 ////////////////////////////////////////////////////////////////////////////////
258 /// set printf format for float/double members, default "%e"
259 
260 void TSQLServer::SetFloatFormat(const char* fmt)
261 {
262  if (fmt==0) fmt = "%e";
263  fgFloatFmt = fmt;
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 /// return current printf format for float/double members, default "%e"
268 
270 {
271  return fgFloatFmt;
272 }
virtual Bool_t Exec(const char *sql)
Execute sql query.
Definition: TSQLServer.cxx:85
TString fDB
Definition: TSQLServer.h:46
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
The db should be of the form: <dbms>://<host>[:<port>][/<database>], e.g.
Definition: TSQLServer.cxx:61
Collectable string class.
Definition: TObjString.h:28
virtual TSQLResult * Query(const char *sql)=0
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TH1 * h
Definition: legend2.C:5
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:141
#define gROOT
Definition: TROOT.h:402
Int_t LoadPlugin()
Load the plugin library for this handler.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static const char * fgFloatFmt
Definition: TSQLServer.h:59
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:269
virtual Bool_t IsError() const
Definition: TSQLServer.h:101
virtual Int_t GetErrorCode() const
returns error code of last operation if res==0, no error Each specific implementation of TSQLServer p...
Definition: TSQLServer.cxx:101
TString fErrorMsg
Definition: TSQLServer.h:49
void ClearError()
reset error fields
Definition: TSQLServer.cxx:119
virtual Bool_t Rollback()
submit "ROLLBACK" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:159
virtual TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=0)=0
virtual const char * GetErrorMsg() const
returns error message of last operation if no errors, return 0 Each specific implementation of TSQLSe...
Definition: TSQLServer.cxx:111
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
A doubly linked list.
Definition: TList.h:44
Int_t fErrorCode
Definition: TSQLServer.h:48
Long_t ExecPlugin(int nargs, const T &... params)
static void SetFloatFormat(const char *fmt="%e")
set printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:260
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:150
virtual const char * GetField(Int_t field)=0
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
virtual TSQLResult * GetTables(const char *dbname, const char *wild=0)=0
virtual TSQLTableInfo * GetTableInfo(const char *tablename)
Producec TSQLTableInfo object, which contain info about table itself and each table column Object mus...
Definition: TSQLServer.cxx:236
void SetError(Int_t code, const char *msg, const char *method=0)
set new values for error fields if method is specified, displays error message
Definition: TSQLServer.cxx:129
Mother of all ROOT objects.
Definition: TObject.h:37
virtual TList * GetTablesList(const char *wild=0)
Return list of user tables Parameter wild specifies wildcard for table names.
Definition: TSQLServer.cxx:182
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual Bool_t HasTable(const char *tablename)
Tests if table of that name exists in database Return kTRUE, if table exists.
Definition: TSQLServer.cxx:208
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
const Bool_t kTRUE
Definition: RtypesCore.h:87
Bool_t fErrorOut
Definition: TSQLServer.h:50
virtual TSQLRow * Next()=0
const char * Data() const
Definition: TString.h:345