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