Logo ROOT  
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 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 
61 TSQLServer *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 
85 Bool_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 
111 const 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 
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 valid 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) return nullptr;
186 
187  TList *lst = nullptr;
188  TSQLRow *row = nullptr;
189  while ((row = res->Next())!=nullptr) {
190  const char* tablename = row->GetField(0);
191  if (!lst) {
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 || (strlen(tablename)==0)) return kFALSE;
211 
212  TList *lst = GetTablesList(tablename);
213  if (!lst) return kFALSE;
214 
215  Bool_t res = kFALSE;
216 
217  TObject* obj = nullptr;
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()) != nullptr)
225  if (strcmp(tablename, obj->GetName())==0) res = kTRUE;
226 
227  delete lst;
228  return res;
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// Produce 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 || (*tablename==0)) return 0;
239 
240  TSQLResult* res = GetColumns(fDB.Data(), tablename);
241  if (!res) return nullptr;
242 
243  TList* lst = nullptr;
244  TSQLRow* row = nullptr;
245  while ((row = res->Next())!=nullptr) {
246  const char *columnname = row->GetField(0);
247  if (!lst) 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) 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 }
TSQLColumnInfo
Definition: TSQLColumnInfo.h:17
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TSQLResult::Next
virtual TSQLRow * Next()=0
TString::Data
const char * Data() const
Definition: TString.h:369
TSQLServer.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TSQLServer::GetColumns
virtual TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr)=0
TObjString.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TCollection::SetOwner
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Definition: TCollection.cxx:746
TSQLServer::GetTableInfo
virtual TSQLTableInfo * GetTableInfo(const char *tablename)
Produce TSQLTableInfo object, which contain info about table itself and each table column Object must...
Definition: TSQLServer.cxx:236
TSQLServer::SetFloatFormat
static void SetFloatFormat(const char *fmt="%e")
set printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:260
TSQLServer::Commit
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:150
TSQLTableInfo
Definition: TSQLTableInfo.h:20
TList.h
TSQLServer::HasTable
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
bool
TSQLResult
Definition: TSQLResult.h:32
TSQLServer::SetError
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
Definition: TSQLServer.cxx:129
TROOT.h
TObjString
Definition: TObjString.h:28
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TPluginHandler
Definition: TPluginManager.h:101
TSQLServer::Connect
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
TSQLServer::Query
virtual TSQLResult * Query(const char *sql)=0
TSQLServer::fDB
TString fDB
Definition: TSQLServer.h:46
h
#define h(i)
Definition: RSha256.hxx:124
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSQLRow
Definition: TSQLRow.h:30
TSQLServer::Exec
virtual Bool_t Exec(const char *sql)
Execute sql query.
Definition: TSQLServer.cxx:85
TSQLRow.h
TSQLServer::fgFloatFmt
static const char * fgFloatFmt
Definition: TSQLServer.h:57
TSQLServer::fErrorOut
Bool_t fErrorOut
Definition: TSQLServer.h:50
TVirtualMutex.h
TSQLServer::GetErrorCode
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
TObject::IsZombie
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
TSQLServer::ClearError
void ClearError()
reset error fields
Definition: TSQLServer.cxx:119
TSQLResult.h
TSQLServer::GetFloatFormat
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:269
TPluginManager.h
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Definition: TObject.h:37
TSQLColumnInfo.h
TSQLServer::Rollback
virtual Bool_t Rollback()
submit "ROLLBACK" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:159
TSQLTableInfo.h
TSQLServer::StartTransaction
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:141
TIter
Definition: TCollection.h:233
TSQLServer::fErrorMsg
TString fErrorMsg
Definition: TSQLServer.h:49
TSQLServer::GetErrorMsg
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
TSQLRow::GetField
virtual const char * GetField(Int_t field)=0
TSQLServer::GetTables
virtual TSQLResult * GetTables(const char *dbname, const char *wild=nullptr)=0
TSQLServer::GetTablesList
virtual TList * GetTablesList(const char *wild=nullptr)
Return list of user tables Parameter wild specifies wildcard for table names.
Definition: TSQLServer.cxx:182
TSQLServer::IsError
virtual Bool_t IsError() const
Definition: TSQLServer.h:99
TSQLServer
Definition: TSQLServer.h:41
TList
Definition: TList.h:44
gROOT
#define gROOT
Definition: TROOT.h:406
int
TSQLServer::fErrorCode
Int_t fErrorCode
Definition: TSQLServer.h:48