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
47
48
49const 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 `sqlite://<database>...`
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
61TSQLServer *TSQLServer::Connect(const char *db, const char *uid, const char *pw)
62{
64 TSQLServer *serv = nullptr;
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 = nullptr;
75 }
76
77 return serv;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Execute sql query.
82/// Useful for commands like DROP TABLE or INSERT, where result set
83/// is not interested. Return kTRUE if no error
84
85Bool_t TSQLServer::Exec(const char* sql)
86{
87 TSQLResult* res = Query(sql);
88 if (!res) 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
111const char* TSQLServer::GetErrorMsg() const
112{
113 return GetErrorCode()==0 ? nullptr : 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
129void 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/// returns kTRUE when transaction is running
148/// Must be implemented in derived classes
149
151{
152 Warning("HasTransactionInFlight", "Not implemented");
153 return kFALSE;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// submit "COMMIT" query to database
158/// return kTRUE, if successful
159
161{
162 return Exec("COMMIT");
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// submit "ROLLBACK" query to database
167/// return kTRUE, if successful
168
170{
171 return Exec("ROLLBACK");
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Return list of user tables
176/// Parameter wild specifies wildcard for table names.
177/// It either contains exact table name to verify that table is exists or
178/// wildcard with "%" (any number of symbols) and "_" (exactly one symbol).
179/// Example of valid wildcards: "%", "%name","___user__".
180/// If wild=="", list of all available tables will be produced.
181/// List contain just tables names in the TObjString.
182/// List must be deleted by the user.
183/// Example code of method usage:
184///
185/// TList* lst = serv->GetTablesList();
186/// TIter next(lst);
187/// TObject* obj;
188/// while (obj = next())
189/// std::cout << "Table: " << obj->GetName() << std::endl;
190/// delete lst;
191
193{
194 TSQLResult* res = GetTables(fDB.Data(), wild);
195 if (!res) return nullptr;
196
197 TList *lst = nullptr;
198 TSQLRow *row = nullptr;
199 while ((row = res->Next())!=nullptr) {
200 const char* tablename = row->GetField(0);
201 if (!lst) {
202 lst = new TList;
203 lst->SetOwner(kTRUE);
204 }
205 lst->Add(new TObjString(tablename));
206 delete row;
207 }
208
209 delete res;
210
211 return lst;
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Tests if table of that name exists in database
216/// Return kTRUE, if table exists
217
218Bool_t TSQLServer::HasTable(const char* tablename)
219{
220 if (!tablename || (strlen(tablename)==0)) return kFALSE;
221
222 TList *lst = GetTablesList(tablename);
223 if (!lst) return kFALSE;
224
225 Bool_t res = kFALSE;
226
227 TObject* obj = nullptr;
228 TIter iter(lst);
229
230 // Can be, that tablename contains "_" or "%" symbols, which are wildcards in SQL,
231 // therefore more than one table can be returned as result.
232 // One should check that exactly same name is appears
233
234 while ((obj = iter()) != nullptr)
235 if (strcmp(tablename, obj->GetName())==0) res = kTRUE;
236
237 delete lst;
238 return res;
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Produce TSQLTableInfo object, which contain info about
243/// table itself and each table column
244/// Object must be deleted by user.
245
247{
248 if (!tablename || (*tablename==0)) return 0;
249
250 TSQLResult* res = GetColumns(fDB.Data(), tablename);
251 if (!res) return nullptr;
252
253 TList* lst = nullptr;
254 TSQLRow* row = nullptr;
255 while ((row = res->Next())!=nullptr) {
256 const char *columnname = row->GetField(0);
257 if (!lst) lst = new TList;
258 lst->Add(new TSQLColumnInfo(columnname));
259 delete row;
260 }
261
262 delete res;
263
264 return new TSQLTableInfo(tablename, lst);
265}
266
267////////////////////////////////////////////////////////////////////////////////
268/// set printf format for float/double members, default "%e"
269
270void TSQLServer::SetFloatFormat(const char* fmt)
271{
272 if (!fmt) fmt = "%e";
273 fgFloatFmt = fmt;
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// return current printf format for float/double members, default "%e"
278
280{
281 return fgFloatFmt;
282}
#define h(i)
Definition RSha256.hxx:106
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
#define gROOT
Definition TROOT.h:406
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
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:456
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:991
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1005
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:376