Logo ROOT  
Reference Guide
TMySQLServer.cxx
Go to the documentation of this file.
1// @(#)root/mysql:$Id$
2// Author: Fons Rademakers 15/02/2000
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// TMySQLServer //
15// //
16// MySQL server plugin implementing the TSQLServer interface. //
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// mysql://<host>[:<port>][/<database>], e.g. //
21// mysql://pcroot.cern.ch:3456/test //
22// //
23// As an example of connecting to mysql we assume that the server is //
24// running on the local host and that you have access to a database //
25// named "test" by connecting using an account that has a username and //
26// password of "tuser" and "tpass". You can set up this account //
27// by using the "mysql" program to connect to the server as the MySQL //
28// root user and issuing the following statement: //
29// //
30// mysql> GRANT ALL ON test.* TO 'tuser'@'localhost' IDENTIFIED BY 'tpass';
31// //
32// If the test database does not exist, create it with this statement: //
33// //
34// mysql> CREATE DATABASE test; //
35// //
36// If you want to use a different server host, username, password, //
37// or database name, just substitute the appropriate values. //
38// To connect do: //
39// //
40// TSQLServer *db = TSQLServer::Connect("mysql://localhost/test", "tuser", "tpass");
41// //
42//////////////////////////////////////////////////////////////////////////
43
44#include "TMySQLServer.h"
45#include "TMySQLResult.h"
46#include "TMySQLStatement.h"
47#include "TSQLColumnInfo.h"
48#include "TSQLTableInfo.h"
49#include "TSQLRow.h"
50#include "TUrl.h"
51#include "TList.h"
52#include "TObjString.h"
53#include "TObjArray.h"
54
56
57////////////////////////////////////////////////////////////////////////////////
58/// Open a connection to a MySQL DB server. The db arguments should be
59/// of the form "mysql://<host>[:<port>][/<database>]", e.g.:
60/// "mysql://pcroot.cern.ch:3456/test". The uid is the username and pw
61/// the password that should be used for the connection.
62///
63/// In addition, several parameters can be specified in url after "?" symbol:
64/// timeout=N n is connect timeout is seconds
65/// socket=socketname socketname should be name of Unix socket, used
66/// for connection
67/// multi_statements tell the server that the client may send multiple
68/// statements in a single string (separated by ;);
69/// multi_results tell the server that the client can handle multiple
70/// result sets from multiple-statement executions or
71/// stored procedures
72/// reconnect=0|1 enable or disable automatic reconnection to the server
73/// if the connection is found to have been lost
74/// compress use the compressed client/server protocol
75/// cnf_file=filename Read options from the named option file instead of
76/// from my.cnf
77/// cnf_group=groupname Read options from the named group from my.cnf or the
78/// file specified with cnf_file option
79/// If several parameters are specified, they should be separated by "&" symbol
80/// Example of connection argument:
81/// TSQLServer::Connect("mysql://host.domain/test?timeout=10&multi_statements");
82
83TMySQLServer::TMySQLServer(const char *db, const char *uid, const char *pw)
84{
85 fMySQL = nullptr;
86 fInfo = "MySQL";
87
88 TUrl url(db);
89
90 if (!url.IsValid()) {
91 TString errmsg("malformed db argument ");
92 errmsg += db;
93 SetError(-1, errmsg.Data(), "TMySQLServer");
94 MakeZombie();
95 return;
96 }
97
98 if (strncmp(url.GetProtocol(), "mysql", 5)) {
99 SetError(-1, "protocol in db argument should be mysql://", "TMySQLServer");
100 MakeZombie();
101 return;
102 }
103
104 const char* dbase = url.GetFile();
105 if (dbase)
106 if (*dbase=='/') dbase++; //skip leading "/" if appears
107
108 fMySQL = new MYSQL;
109 mysql_init(fMySQL);
110
111 ULong_t client_flag = 0;
112 TString socket;
113
114 TString optstr = url.GetOptions();
115 TObjArray* optarr = optstr.Tokenize("&");
116 if (optarr!=0) {
117 TIter next(optarr);
118 TObject *obj = 0;
119 while ((obj = next()) != 0) {
120 TString opt = obj->GetName();
121 opt.ToLower();
122 opt.ReplaceAll(" ","");
123 if (opt.Contains("timeout=")) {
124 opt.Remove(0, 8);
125 Int_t timeout = opt.Atoi();
126 if (timeout > 0) {
127 UInt_t mysqltimeout = (UInt_t) timeout;
128 mysql_options(fMySQL, MYSQL_OPT_CONNECT_TIMEOUT, (const char*) &mysqltimeout);
129 if (gDebug) Info("TMySQLServer","Set timeout %d",timeout);
130 }
131 } else
132 if (opt.Contains("read_timeout=")) {
133 #if MYSQL_VERSION_ID >= 40101
134 opt.Remove(0, 13);
135 Int_t timeout = opt.Atoi();
136 if (timeout > 0) {
137 UInt_t mysqltimeout = (UInt_t) timeout;
138 mysql_options(fMySQL, MYSQL_OPT_READ_TIMEOUT, (const char*) &mysqltimeout);
139 if (gDebug) Info("TMySQLServer","Set read timeout %d", timeout);
140 }
141 #else
142 Warning("TMySQLServer","MYSQL_OPT_READ_TIMEOUT option not supported by this version of MySql");
143 #endif
144
145 } else
146 if (opt.Contains("write_timeout=")) {
147 #if MYSQL_VERSION_ID >= 40101
148 opt.Remove(0, 14);
149 Int_t timeout = opt.Atoi();
150 if (timeout > 0) {
151 UInt_t mysqltimeout = (UInt_t) timeout;
152 mysql_options(fMySQL, MYSQL_OPT_WRITE_TIMEOUT, (const char*) &mysqltimeout);
153 if (gDebug) Info("TMySQLServer","Set write timeout %d", timeout);
154 }
155 #else
156 Warning("TMySQLServer","MYSQL_OPT_WRITE_TIMEOUT option not supported by this version of MySql");
157 #endif
158 } else
159 if (opt.Contains("reconnect=")) {
160 #if MYSQL_VERSION_ID >= 50013
161 opt.Remove(0, 10);
162 bool reconnect_on = (opt=="1") || (opt=="true");
163 mysql_options(fMySQL, MYSQL_OPT_RECONNECT, (const char*) &reconnect_on);
164 if (gDebug) Info("TMySQLServer","Set reconnect options %s", (reconnect_on ? "ON" : "OFF"));
165 #else
166 Warning("TMySQLServer","MYSQL_OPT_RECONNECT option not supported by this version of MySql");
167 #endif
168 } else
169 if (opt.Contains("socket=")) {
170 socket = (obj->GetName()+7);
171 if (gDebug) Info("TMySQLServer","Use socket %s", socket.Data());
172 } else
173 if (opt.Contains("multi_statements")) {
174 #if MYSQL_VERSION_ID >= 40100
175 client_flag = client_flag | CLIENT_MULTI_STATEMENTS;
176 if (gDebug) Info("TMySQLServer","Use CLIENT_MULTI_STATEMENTS");
177 #else
178 Warning("TMySQLServer","CLIENT_MULTI_STATEMENTS not supported by this version of MySql");
179 #endif
180 } else
181 if (opt.Contains("multi_results")) {
182 #if MYSQL_VERSION_ID >= 40100
183 client_flag = client_flag | CLIENT_MULTI_RESULTS;
184 if (gDebug) Info("TMySQLServer","Use CLIENT_MULTI_RESULTS");
185 #else
186 Warning("TMySQLServer","CLIENT_MULTI_RESULTS not supported by this version of MySql");
187 #endif
188 } else
189 if (opt.Contains("compress")) {
190 mysql_options(fMySQL, MYSQL_OPT_COMPRESS, 0);
191 if (gDebug) Info("TMySQLServer","Use compressed client/server protocol");
192 } else
193 if (opt.Contains("cnf_file=")) {
194 const char* filename = (obj->GetName()+9);
195 mysql_options(fMySQL, MYSQL_READ_DEFAULT_FILE, filename);
196 if (gDebug) Info("TMySQLServer","Read mysql options from %s file", filename);
197 } else
198 if (opt.Contains("cnf_group=")) {
199 const char* groupname = (obj->GetName()+10);
200 mysql_options(fMySQL, MYSQL_READ_DEFAULT_GROUP, groupname);
201 if (gDebug) Info("TMySQLServer","Read mysql options from %s group of my.cnf file", groupname);
202 }
203 }
204 optarr->Delete();
205 delete optarr;
206 }
207
208 Int_t port = 3306;
209 if (url.GetPort()>0) port = url.GetPort();
210
211 if (mysql_real_connect(fMySQL, url.GetHost(), uid, pw, dbase, port,
212 (socket.Length()>0) ? socket.Data() : 0 , client_flag)) {
213 fType = "MySQL";
214 fHost = url.GetHost();
215 fDB = dbase;
216 fPort = port;
217 } else {
218 SetError(mysql_errno(fMySQL), mysql_error(fMySQL), "TMySQLServer");
219 MakeZombie();
220 }
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Close connection to MySQL DB server.
225
227{
228 if (IsConnected())
229 Close();
230 delete fMySQL;
231}
232
233// Reset error and check that server connected
234#define CheckConnect(method, res) \
235 { \
236 ClearError(); \
237 if (!IsConnected()) { \
238 SetError(-1,"MySQL server is not connected",method); \
239 return res; \
240 } \
241 }
242
243
244// check last mysql error code
245#define CheckErrNo(method, force, res) \
246 { \
247 unsigned int sqlerrno = mysql_errno(fMySQL); \
248 if ((sqlerrno!=0) || force) { \
249 const char* sqlerrmsg = mysql_error(fMySQL); \
250 if (sqlerrno==0) { sqlerrno = 11111; sqlerrmsg = "MySQL error"; } \
251 SetError(sqlerrno, sqlerrmsg, method); \
252 return res; \
253 } \
254 }
255
256
257////////////////////////////////////////////////////////////////////////////////
258/// Close connection to MySQL DB server.
259
261{
262 ClearError();
263
264 if (!fMySQL)
265 return;
266
267 mysql_close(fMySQL);
268 fPort = -1;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Execute SQL command. Result object must be deleted by the user.
273/// Returns a pointer to a TSQLResult object if successful, 0 otherwise.
274/// The result object must be deleted by the user.
275
277{
278 CheckConnect("Query", 0);
279
280 if (mysql_query(fMySQL, sql))
281 CheckErrNo("Query",kTRUE,0);
282
283 MYSQL_RES *res = mysql_store_result(fMySQL);
284 CheckErrNo("Query", kFALSE, 0);
285
286 return new TMySQLResult(res);
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// Execute SQL command which does not produce any result sets.
291/// Returns kTRUE if successful.
292
294{
295 CheckConnect("Exec", kFALSE);
296
297 if (mysql_query(fMySQL, sql))
298 CheckErrNo("Exec",kTRUE,kFALSE);
299
300 return !IsError();
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Select a database. Returns 0 if successful, non-zero otherwise.
305
307{
308 CheckConnect("SelectDataBase", -1);
309
310 Int_t res = mysql_select_db(fMySQL, dbname);
311 if (res==0) fDB = dbname;
312 else CheckErrNo("SelectDataBase", kTRUE, res);
313
314 return res;
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// List all available databases. Wild is for wildcarding "t%" list all
319/// databases starting with "t".
320/// Returns a pointer to a TSQLResult object if successful, 0 otherwise.
321/// The result object must be deleted by the user.
322
324{
325 CheckConnect("GetDataBases", 0);
326
327 MYSQL_RES *res = mysql_list_dbs(fMySQL, wild);
328
329 CheckErrNo("GetDataBases", kFALSE, 0);
330
331 return new TMySQLResult(res);
332}
333
334////////////////////////////////////////////////////////////////////////////////
335/// List all tables in the specified database. Wild is for wildcarding
336/// "t%" list all tables starting with "t".
337/// Returns a pointer to a TSQLResult object if successful, 0 otherwise.
338/// The result object must be deleted by the user.
339
340TSQLResult *TMySQLServer::GetTables(const char *dbname, const char *wild)
341{
342 CheckConnect("GetTables", 0);
343
344 if (SelectDataBase(dbname) != 0) return nullptr;
345
346 MYSQL_RES *res = mysql_list_tables(fMySQL, wild);
347
348 CheckErrNo("GetTables", kFALSE, 0);
349
350 return new TMySQLResult(res);
351}
352
353
354////////////////////////////////////////////////////////////////////////////////
355/// Return list of tables with specified wildcard.
356
358{
359 CheckConnect("GetTablesList", 0);
360
361 MYSQL_RES *res = mysql_list_tables(fMySQL, wild);
362
363 CheckErrNo("GetTablesList", kFALSE, 0);
364
365 MYSQL_ROW row = mysql_fetch_row(res);
366
367 TList *lst = nullptr;
368
369 while (row!=0) {
370 CheckErrNo("GetTablesList", kFALSE, lst);
371
372 const char *tablename = row[0];
373
374 if (tablename) {
375 if (!lst) {
376 lst = new TList();
377 lst->SetOwner(kTRUE);
378 }
379 lst->Add(new TObjString(tablename));
380 }
381
382 row = mysql_fetch_row(res);
383 }
384
385 mysql_free_result(res);
386
387 return lst;
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Produces SQL table info.
392/// Object must be deleted by user.
393
395{
396 CheckConnect("GetTableInfo", 0);
397
398 if (!tablename || (*tablename==0)) return nullptr;
399
400 TString sql;
401 sql.Form("SELECT * FROM `%s` LIMIT 1", tablename);
402
403 if (mysql_query(fMySQL, sql.Data()) != 0)
404 CheckErrNo("GetTableInfo", kTRUE, 0);
405
406 MYSQL_RES *res = mysql_store_result(fMySQL);
407 CheckErrNo("GetTableInfo", kFALSE, 0);
408
409 unsigned int numfields = mysql_num_fields(res);
410
411 MYSQL_FIELD* fields = mysql_fetch_fields(res);
412
413 sql.Form("SHOW COLUMNS FROM `%s`", tablename);
414 TSQLResult* showres = Query(sql.Data());
415
416 if (!showres) {
417 mysql_free_result(res);
418 return nullptr;
419 }
420
421 TList *lst = nullptr;
422
423 unsigned int nfield = 0;
424
425 TSQLRow* row = nullptr;
426
427 while ((row = showres->Next()) != 0) {
428 const char* column_name = row->GetField(0);
429 const char* type_name = row->GetField(1);
430
431 if ((nfield>=numfields) ||
432 (strcmp(column_name, fields[nfield].name)!=0))
433 {
434 SetError(-1,"mismatch in column names","GetTableInfo");
435 break;
436 }
437
438 Int_t sqltype = kSQL_NONE;
439
440 Int_t data_size = -1; // size in bytes
441 Int_t data_length = -1; // declaration like VARCHAR(n) or NUMERIC(n)
442 Int_t data_scale = -1; // second argument in declaration
443 Int_t data_sign = -1; // signed type or not
444
445 if (IS_NUM(fields[nfield].type)) {
446 if (fields[nfield].flags & UNSIGNED_FLAG)
447 data_sign = 0;
448 else
449 data_sign = 1;
450 }
451
452 Bool_t nullable = (fields[nfield].flags & NOT_NULL_FLAG) == 0;
453
454 data_length = fields[nfield].length;
455 if (data_length==0) data_length = -1;
456
457#if MYSQL_VERSION_ID >= 40100
458
459 switch (fields[nfield].type) {
460 case MYSQL_TYPE_TINY:
461 case MYSQL_TYPE_SHORT:
462 case MYSQL_TYPE_LONG:
463 case MYSQL_TYPE_INT24:
464 case MYSQL_TYPE_LONGLONG:
465 sqltype = kSQL_INTEGER;
466 break;
467 case MYSQL_TYPE_DECIMAL:
468 sqltype = kSQL_NUMERIC;
469 data_scale = fields[nfield].decimals;
470 break;
471 case MYSQL_TYPE_FLOAT:
472 sqltype = kSQL_FLOAT;
473 break;
474 case MYSQL_TYPE_DOUBLE:
475 sqltype = kSQL_DOUBLE;
476 break;
477 case MYSQL_TYPE_TIMESTAMP:
478 sqltype = kSQL_TIMESTAMP;
479 break;
480 case MYSQL_TYPE_DATE:
481 case MYSQL_TYPE_TIME:
482 case MYSQL_TYPE_DATETIME:
483 case MYSQL_TYPE_YEAR:
484 break;
485 case MYSQL_TYPE_STRING:
486 if (fields[nfield].charsetnr==63)
487 sqltype = kSQL_BINARY;
488 else
489 sqltype = kSQL_CHAR;
490 data_size = data_length;
491 break;
492 case MYSQL_TYPE_VAR_STRING:
493 if (fields[nfield].charsetnr==63)
494 sqltype = kSQL_BINARY;
495 else
496 sqltype = kSQL_VARCHAR;
497 data_size = data_length;
498 break;
499 case MYSQL_TYPE_BLOB:
500 if (fields[nfield].charsetnr==63)
501 sqltype = kSQL_BINARY;
502 else
503 sqltype = kSQL_VARCHAR;
504 data_size = data_length;
505 break;
506 case MYSQL_TYPE_SET:
507 case MYSQL_TYPE_ENUM:
508 case MYSQL_TYPE_GEOMETRY:
509 case MYSQL_TYPE_NULL:
510 break;
511 default:
512 if (IS_NUM(fields[nfield].type))
513 sqltype = kSQL_NUMERIC;
514 }
515
516#endif
517
518 if (!lst)
519 lst = new TList;
520 lst->Add(new TSQLColumnInfo(column_name,
521 type_name,
522 nullable,
523 sqltype,
524 data_size,
525 data_length,
526 data_scale,
527 data_sign));
528
529 nfield++;
530 delete row;
531 }
532
533 mysql_free_result(res);
534 delete showres;
535
536 sql.Form("SHOW TABLE STATUS LIKE '%s'", tablename);
537
538 TSQLTableInfo* info = 0;
539
540 TSQLResult* stats = Query(sql.Data());
541
542 if (stats!=0) {
543 row = 0;
544
545 while ((row = stats->Next()) != 0) {
546 if (strcmp(row->GetField(0), tablename)!=0) {
547 delete row;
548 continue;
549 }
550 const char* comments = 0;
551 const char* engine = 0;
552 const char* create_time = 0;
553 const char* update_time = 0;
554
555 for (int n=1;n<stats->GetFieldCount();n++) {
556 TString fname = stats->GetFieldName(n);
557 fname.ToLower();
558 if (fname=="engine") engine = row->GetField(n); else
559 if (fname=="comment") comments = row->GetField(n); else
560 if (fname=="create_time") create_time = row->GetField(n); else
561 if (fname=="update_time") update_time = row->GetField(n);
562 }
563
564 info = new TSQLTableInfo(tablename,
565 lst,
566 comments,
567 engine,
568 create_time,
569 update_time);
570
571 delete row;
572 break;
573 }
574 delete stats;
575 }
576
577 if (info==0)
578 info = new TSQLTableInfo(tablename, lst);
579
580 return info;
581}
582
583////////////////////////////////////////////////////////////////////////////////
584/// List all columns in specified table in the specified database.
585/// Wild is for wildcarding "t%" list all columns starting with "t".
586/// Returns a pointer to a TSQLResult object if successful, 0 otherwise.
587/// The result object must be deleted by the user.
588
589TSQLResult *TMySQLServer::GetColumns(const char *dbname, const char *table,
590 const char *wild)
591{
592 CheckConnect("GetColumns", 0);
593
594 if (SelectDataBase(dbname) != 0) return nullptr;
595
596 TString sql;
597 if (wild)
598 sql.Form("SHOW COLUMNS FROM %s LIKE '%s'", table, wild);
599 else
600 sql.Form("SHOW COLUMNS FROM %s", table);
601
602 return Query(sql.Data());
603}
604
605////////////////////////////////////////////////////////////////////////////////
606/// Create a database. Returns 0 if successful, non-zero otherwise.
607
609{
610 CheckConnect("CreateDataBase", -1);
611
612 Int_t res = mysql_query(fMySQL, Form("CREATE DATABASE %s",dbname));
613
614 CheckErrNo("CreateDataBase", kFALSE, res);
615
616 return res;
617}
618
619////////////////////////////////////////////////////////////////////////////////
620/// Drop (i.e. delete) a database. Returns 0 if successful, non-zero
621/// otherwise.
622
624{
625 CheckConnect("DropDataBase", -1);
626
627 Int_t res = mysql_query(fMySQL, Form("DROP DATABASE %s",dbname));
628
629 CheckErrNo("DropDataBase", kFALSE, res);
630
631 return res;
632}
633
634////////////////////////////////////////////////////////////////////////////////
635/// Reload permission tables. Returns 0 if successful, non-zero
636/// otherwise. User must have reload permissions.
637
639{
640 CheckConnect("Reload", -1);
641
642 Int_t res = mysql_reload(fMySQL);
643
644 CheckErrNo("Reload", kFALSE, res);
645
646 return res;
647}
648
649////////////////////////////////////////////////////////////////////////////////
650/// Shutdown the database server. Returns 0 if successful, non-zero
651/// otherwise. User must have shutdown permissions.
652
654{
655 CheckConnect("Shutdown", -1);
656
657 Int_t res;
658
659#if MYSQL_VERSION_ID >= 50001 || \
660 (MYSQL_VERSION_ID < 50000 && MYSQL_VERSION_ID >= 40103)
661 res = mysql_shutdown(fMySQL, SHUTDOWN_DEFAULT);
662#else
663 res = mysql_shutdown(fMySQL);
664#endif
665
666 CheckErrNo("Shutdown", kFALSE, res);
667
668 return res;
669}
670
671////////////////////////////////////////////////////////////////////////////////
672/// Return server info in form "MySQL <vesrion>".
673
675{
676 CheckConnect("ServerInfo", 0);
677
678 const char* res = mysql_get_server_info(fMySQL);
679
680 CheckErrNo("ServerInfo", kFALSE, res);
681
682 fInfo = "MySQL ";
683 fInfo += res;
684
685 return fInfo.Data();
686}
687
688////////////////////////////////////////////////////////////////////////////////
689/// Return kTRUE if TSQLStatement class is supported.
690/// Starts from MySQL 4.1.
691
693{
694#if MYSQL_VERSION_ID < 40100
695 return kFALSE;
696#else
697 return kTRUE;
698#endif
699}
700
701
702////////////////////////////////////////////////////////////////////////////////
703/// Produce TMySQLStatement.
704
706{
707#if MYSQL_VERSION_ID < 40100
708 ClearError();
709 SetError(-1, "Statement class does not supported by MySQL version < 4.1", "Statement");
710 return nullptr;
711#else
712
713 CheckConnect("Statement", 0);
714
715 if (!sql || !*sql) {
716 SetError(-1, "no query string specified","Statement");
717 return nullptr;
718 }
719
720 MYSQL_STMT *stmt = mysql_stmt_init(fMySQL);
721 if (!stmt)
722 CheckErrNo("Statement", kTRUE, 0);
723
724 if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
725 SetError(mysql_errno(fMySQL), mysql_error(fMySQL), "Statement");
726 mysql_stmt_close(stmt);
727 return nullptr;
728 }
729
730 return new TMySQLStatement(stmt, fErrorOut);
731
732#endif
733}
734
735////////////////////////////////////////////////////////////////////////////////
736/// Start transaction
737
739{
740 CheckConnect("StartTransaction", kFALSE);
741
743}
744
745////////////////////////////////////////////////////////////////////////////////
746/// Commit changes
747
749{
750 CheckConnect("Commit", kFALSE);
751
752#if MYSQL_VERSION_ID >= 40100
753
754 if (mysql_commit(fMySQL))
755 CheckErrNo("Commit", kTRUE, kFALSE);
756
757 return kTRUE;
758
759#else
760
761 return TSQLServer::Commit();
762
763#endif
764
765}
766
767////////////////////////////////////////////////////////////////////////////////
768/// Rollback changes
769
771{
772 CheckConnect("Rollback", kFALSE);
773
774#if MYSQL_VERSION_ID >= 40100
775
776 if (mysql_rollback(fMySQL))
777 CheckErrNo("Rollback", kTRUE, kFALSE);
778
779 return kTRUE;
780
781#else
782
783 return TSQLServer::Rollback();
784
785#endif
786
787}
788
789////////////////////////////////////////////////////////////////////////////////
790/// Execute Ping to SQL Connection.
791/// Since mysql_ping tries to reconnect by itself,
792/// a double call to the mysql function is implemented.
793/// Returns kTRUE if successful
794
796{
797 CheckConnect("Ping", kFALSE);
798
799 if (mysql_ping(fMySQL)) {
800 if (mysql_ping(fMySQL)) {
801 Error("PingVerify", "not able to automatically reconnect a second time");
802 CheckErrNo("Ping", kTRUE, kFALSE);
803 } else
804 Info("PingVerify", "connection was lost, but could automatically reconnect");
805 }
806
807 return !IsError();
808}
809
810////////////////////////////////////////////////////////////////////////////////
811/// Execute Ping to SQL Connection using the mysql_ping function.
812/// Returns 0 if successful, non-zero in case an error occured.
813
815{
816 CheckConnect("PingInt", kFALSE);
817
818 return mysql_ping(fMySQL);
819}
unsigned int UInt_t
Definition: RtypesCore.h:44
const Bool_t kFALSE
Definition: RtypesCore.h:90
unsigned long ULong_t
Definition: RtypesCore.h:53
R__EXTERN Int_t gDebug
Definition: RtypesCore.h:117
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
#define CheckErrNo(method, force, res)
#define CheckConnect(method, res)
char * Form(const char *fmt,...)
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:44
virtual void Add(TObject *obj)
Definition: TList.h:87
Int_t SelectDataBase(const char *dbname) final
Select a database. Returns 0 if successful, non-zero otherwise.
Bool_t Rollback() final
Rollback changes.
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr) final
List all columns in specified table in the specified database.
TSQLResult * GetDataBases(const char *wild=nullptr) final
List all available databases.
TSQLStatement * Statement(const char *sql, Int_t=100) final
Produce TMySQLStatement.
void Close(Option_t *opt="") final
Close connection to MySQL DB server.
Bool_t PingVerify() final
Execute Ping to SQL Connection.
TSQLResult * GetTables(const char *dbname, const char *wild=nullptr) final
List all tables in the specified database.
Bool_t StartTransaction() final
Start transaction.
Int_t Reload() final
Reload permission tables.
TMySQLServer(const char *db, const char *uid, const char *pw)
Open a connection to a MySQL DB server.
TString fInfo
Definition: TMySQLServer.h:55
TList * GetTablesList(const char *wild=nullptr) final
Return list of tables with specified wildcard.
const char * ServerInfo() final
Return server info in form "MySQL <vesrion>".
TSQLTableInfo * GetTableInfo(const char *tablename) final
Produces SQL table info.
TSQLResult * Query(const char *sql) final
Execute SQL command.
Bool_t Exec(const char *sql) final
Execute SQL command which does not produce any result sets.
Int_t CreateDataBase(const char *dbname) final
Create a database. Returns 0 if successful, non-zero otherwise.
Bool_t HasStatement() const final
Return kTRUE if TSQLStatement class is supported.
~TMySQLServer()
Close connection to MySQL DB server.
MYSQL * fMySQL
Definition: TMySQLServer.h:54
Bool_t Commit() final
Commit changes.
Int_t Ping() final
Execute Ping to SQL Connection using the mysql_ping function.
Int_t DropDataBase(const char *dbname) final
Drop (i.e.
Int_t Shutdown() final
Shutdown the database server.
An array of TObjects.
Definition: TObjArray.h:37
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:356
Collectable string class.
Definition: TObjString.h:28
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:877
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
void MakeZombie()
Definition: TObject.h:49
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:865
virtual const char * GetFieldName(Int_t field)=0
virtual TSQLRow * Next()=0
virtual Int_t GetFieldCount()=0
virtual const char * GetField(Int_t field)=0
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:150
void ClearError()
reset error fields
Definition: TSQLServer.cxx:119
TString fHost
Definition: TSQLServer.h:45
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
Int_t fPort
Definition: TSQLServer.h:47
Bool_t fErrorOut
Definition: TSQLServer.h:50
virtual Bool_t Rollback()
submit "ROLLBACK" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:159
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:141
TString fDB
Definition: TSQLServer.h:46
virtual Bool_t IsError() const
Definition: TSQLServer.h:99
virtual Bool_t IsConnected() const
Definition: TSQLServer.h:93
TString fType
Definition: TSQLServer.h:44
@ kSQL_VARCHAR
Definition: TSQLServer.h:63
@ kSQL_TIMESTAMP
Definition: TSQLServer.h:69
@ kSQL_INTEGER
Definition: TSQLServer.h:64
@ kSQL_NUMERIC
Definition: TSQLServer.h:67
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1921
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2197
TString & Remove(Ssiz_t pos)
Definition: TString.h:668
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
This class represents a WWW compatible URL.
Definition: TUrl.h:35
const char * GetFile() const
Definition: TUrl.h:71
Bool_t IsValid() const
Definition: TUrl.h:81
const char * GetHost() const
Definition: TUrl.h:69
const char * GetOptions() const
Definition: TUrl.h:73
const char * GetProtocol() const
Definition: TUrl.h:66
Int_t GetPort() const
Definition: TUrl.h:80
const Int_t n
Definition: legend1.C:16