Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
46
47
48const char* TSQLServer::fgFloatFmt = "%e";
49
50
51////////////////////////////////////////////////////////////////////////////////
52/// The db should be of the form: `<dbms>://<host>[:<port>][/<database>]`,
53/// e.g.: `mysql://pcroot.cern.ch:3456/test`, `oracle://srv1.cern.ch/main`,
54/// `pgsql://...` or `sqlite://<database>...`
55/// The uid is the username and pw the password that should be used for
56/// the connection. Depending on the `<dbms>` the shared library (plugin)
57/// for the selected system will be loaded. When the connection could not
58/// be opened 0 is returned.
59
60TSQLServer *TSQLServer::Connect(const char *db, const char *uid, const char *pw)
61{
63 TSQLServer *serv = nullptr;
64
65 if ((h = gROOT->GetPluginManager()->FindHandler("TSQLServer", db))) {
66 if (h->LoadPlugin() == -1)
67 return 0;
68 serv = (TSQLServer *) h->ExecPlugin(3, db, uid, pw);
69 }
70
71 if (serv && serv->IsZombie()) {
72 delete serv;
73 serv = nullptr;
74 }
75
76 return serv;
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Execute sql query.
81/// Useful for commands like DROP TABLE or INSERT, where result set
82/// is not interested. Return kTRUE if no error
83
85{
86 TSQLResult* res = Query(sql);
87 if (!res) return kFALSE;
88
89 delete res;
90
91 return !IsError();
92}
93
94
95////////////////////////////////////////////////////////////////////////////////
96/// returns error code of last operation
97/// if res==0, no error
98/// Each specific implementation of TSQLServer provides its own error coding
99
101{
102 return fErrorCode;
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// returns error message of last operation
107/// if no errors, return 0
108/// Each specific implementation of TSQLServer provides its own error messages
109
110const char* TSQLServer::GetErrorMsg() const
111{
112 return GetErrorCode()==0 ? nullptr : fErrorMsg.Data();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// reset error fields
117
119{
120 fErrorCode = 0;
121 fErrorMsg = "";
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// set new values for error fields
126/// if method is specified, displays error message
127
128void TSQLServer::SetError(Int_t code, const char* msg, const char* method)
129{
130 fErrorCode = code;
131 fErrorMsg = msg;
132 if ((method!=0) && fErrorOut)
133 Error(method,"Code: %d Msg: %s", code, (msg ? msg : "No message"));
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// submit "START TRANSACTION" query to database
138/// return kTRUE, if successful
139
141{
142 return Exec("START TRANSACTION");
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// returns kTRUE when transaction is running
147/// Must be implemented in derived classes
148
150{
151 Warning("HasTransactionInFlight", "Not implemented");
152 return kFALSE;
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// submit "COMMIT" query to database
157/// return kTRUE, if successful
158
160{
161 return Exec("COMMIT");
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// submit "ROLLBACK" query to database
166/// return kTRUE, if successful
167
169{
170 return Exec("ROLLBACK");
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Return list of user tables
175/// Parameter wild specifies wildcard for table names.
176/// It either contains exact table name to verify that table is exists or
177/// wildcard with "%" (any number of symbols) and "_" (exactly one symbol).
178/// Example of valid wildcards: "%", "%name","___user__".
179/// If wild=="", list of all available tables will be produced.
180/// List contain just tables names in the TObjString.
181/// List must be deleted by the user.
182/// Example code of method usage:
183///
184/// TList* lst = serv->GetTablesList();
185/// TIter next(lst);
186/// TObject* obj;
187/// while (obj = next())
188/// std::cout << "Table: " << obj->GetName() << std::endl;
189/// delete lst;
190
192{
193 TSQLResult* res = GetTables(fDB.Data(), wild);
194 if (!res) return nullptr;
195
196 TList *lst = nullptr;
197 TSQLRow *row = nullptr;
198 while ((row = res->Next())!=nullptr) {
199 const char* tablename = row->GetField(0);
200 if (!lst) {
201 lst = new TList;
202 lst->SetOwner(kTRUE);
203 }
204 lst->Add(new TObjString(tablename));
205 delete row;
206 }
207
208 delete res;
209
210 return lst;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Tests if table of that name exists in database
215/// Return kTRUE, if table exists
216
218{
219 if (!tablename || (strlen(tablename)==0)) return kFALSE;
220
222 if (!lst) return kFALSE;
223
224 Bool_t res = kFALSE;
225
226 TObject* obj = nullptr;
227 TIter iter(lst);
228
229 // Can be, that tablename contains "_" or "%" symbols, which are wildcards in SQL,
230 // therefore more than one table can be returned as result.
231 // One should check that exactly same name is appears
232
233 while ((obj = iter()) != nullptr)
234 if (strcmp(tablename, obj->GetName())==0) res = kTRUE;
235
236 delete lst;
237 return res;
238}
239
240////////////////////////////////////////////////////////////////////////////////
241/// Produce TSQLTableInfo object, which contain info about
242/// table itself and each table column
243/// Object must be deleted by user.
244
246{
247 if (!tablename || (*tablename==0)) return 0;
248
250 if (!res) return nullptr;
251
252 TList* lst = nullptr;
253 TSQLRow* row = nullptr;
254 while ((row = res->Next())!=nullptr) {
255 const char *columnname = row->GetField(0);
256 if (!lst) lst = new TList;
257 lst->Add(new TSQLColumnInfo(columnname));
258 delete row;
259 }
260
261 delete res;
262
263 return new TSQLTableInfo(tablename, lst);
264}
265
266////////////////////////////////////////////////////////////////////////////////
267/// set printf format for float/double members, default "%e"
268
270{
271 if (!fmt) fmt = "%e";
272 fgFloatFmt = fmt;
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// return current printf format for float/double members, default "%e"
277
279{
280 return fgFloatFmt;
281}
#define h(i)
Definition RSha256.hxx:106
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:411
A doubly linked list.
Definition TList.h:38
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
void ClearError()
reset error fields
virtual const char * GetErrorMsg() const
returns error message of last operation if no errors, return 0 Each specific implementation of TSQLSe...
virtual TSQLTableInfo * GetTableInfo(const char *tablename)
Produce TSQLTableInfo object, which contain info about table itself and each table column Object must...
void SetError(Int_t code, const char *msg, const char *method=nullptr)
set new values for error fields if method is specified, displays error message
virtual Bool_t HasTransactionInFlight()
returns kTRUE when transaction is running Must be implemented in derived classes
virtual Int_t GetErrorCode() const
returns error code of last operation if res==0, no error Each specific implementation of TSQLServer p...
Bool_t fErrorOut
Definition TSQLServer.h:50
virtual TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr)=0
Int_t fErrorCode
Definition TSQLServer.h:48
virtual Bool_t HasTable(const char *tablename)
Tests if table of that name exists in database Return kTRUE, if table exists.
virtual Bool_t Exec(const char *sql)
Execute sql query.
virtual Bool_t Rollback()
submit "ROLLBACK" query to database return kTRUE, if successful
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
virtual TList * GetTablesList(const char *wild=nullptr)
Return list of user tables Parameter wild specifies wildcard for table names.
virtual TSQLResult * Query(const char *sql)=0
virtual TSQLResult * GetTables(const char *dbname, const char *wild=nullptr)=0
TString fErrorMsg
Definition TSQLServer.h:49
TString fDB
Definition TSQLServer.h:46
virtual Bool_t IsError() const
Definition TSQLServer.h:99
static void SetFloatFormat(const char *fmt="%e")
set printf format for float/double members, default "%e"
static const char * fgFloatFmt
Definition TSQLServer.h:57
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.: mysql://pcroot....
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
const char * Data() const
Definition TString.h:384