55 #include <my_global.h>
94 TString errmsg(
"malformed db argument ");
96 SetError(-1, errmsg.
Data(),
"TMySQLServer");
102 SetError(-1,
"protocol in db argument should be mysql://",
"TMySQLServer");
107 const char* dbase = url.
GetFile();
109 if (*dbase==
'/') dbase++;
122 while ((obj =
next()) != 0) {
131 mysql_options(fMySQL, MYSQL_OPT_CONNECT_TIMEOUT, (
const char*) &mysqltimeout);
132 if (
gDebug)
Info(
"TMySQLServer",
"Set timeout %d",timeout);
135 if (opt.
Contains(
"read_timeout=")) {
136 #if MYSQL_VERSION_ID >= 40101
141 mysql_options(fMySQL, MYSQL_OPT_READ_TIMEOUT, (
const char*) &mysqltimeout);
142 if (
gDebug)
Info(
"TMySQLServer",
"Set read timeout %d", timeout);
145 Warning(
"TMySQLServer",
"MYSQL_OPT_READ_TIMEOUT option not supported by this version of MySql");
149 if (opt.
Contains(
"write_timeout=")) {
150 #if MYSQL_VERSION_ID >= 40101
155 mysql_options(fMySQL, MYSQL_OPT_WRITE_TIMEOUT, (
const char*) &mysqltimeout);
156 if (
gDebug)
Info(
"TMySQLServer",
"Set write timeout %d", timeout);
159 Warning(
"TMySQLServer",
"MYSQL_OPT_WRITE_TIMEOUT option not supported by this version of MySql");
163 #if MYSQL_VERSION_ID >= 50013
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"));
169 Warning(
"TMySQLServer",
"MYSQL_OPT_RECONNECT option not supported by this version of MySql");
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");
181 Warning(
"TMySQLServer",
"CLIENT_MULTI_STATEMENTS not supported by this version of MySql");
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");
189 Warning(
"TMySQLServer",
"CLIENT_MULTI_RESULTS not supported by this version of MySql");
193 mysql_options(fMySQL, MYSQL_OPT_COMPRESS, 0);
194 if (
gDebug)
Info(
"TMySQLServer",
"Use compressed client/server protocol");
198 mysql_options(fMySQL, MYSQL_READ_DEFAULT_FILE, filename);
199 if (
gDebug)
Info(
"TMySQLServer",
"Read mysql options from %s file", filename);
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);
214 if (mysql_real_connect(fMySQL, url.
GetHost(), uid, pw, dbase, port,
215 (socket.
Length()>0) ? socket.
Data() : 0 , client_flag)) {
221 SetError(mysql_errno(fMySQL), mysql_error(fMySQL),
"TMySQLServer");
237 #define CheckConnect(method, res) \
240 if (!IsConnected()) { \
241 SetError(-1,"MySQL server is not connected",method); \
248 #define CheckErrNo(method, force, res) \
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); \
283 if (mysql_query(
fMySQL, sql))
286 MYSQL_RES *res = mysql_store_result(
fMySQL);
300 if (mysql_query(
fMySQL, sql))
314 if (res==0)
fDB = dbname;
330 MYSQL_RES *res = mysql_list_dbs(
fMySQL, wild);
349 MYSQL_RES *res = mysql_list_tables(
fMySQL, wild);
364 MYSQL_RES *res = mysql_list_tables(
fMySQL, wild);
368 MYSQL_ROW row = mysql_fetch_row(res);
375 const char* tablename = row[0];
385 row = mysql_fetch_row(res);
388 mysql_free_result(res);
401 if ((tablename==0) || (*tablename==0))
return 0;
404 sql.
Form(
"SELECT * FROM `%s` LIMIT 1", tablename);
409 MYSQL_RES *res = mysql_store_result(
fMySQL);
412 unsigned int numfields = mysql_num_fields(res);
414 MYSQL_FIELD* fields = mysql_fetch_fields(res);
416 sql.
Form(
"SHOW COLUMNS FROM `%s`", tablename);
420 mysql_free_result(res);
426 unsigned int nfield = 0;
430 while ((row = showres->
Next()) != 0) {
431 const char* column_name = row->
GetField(0);
432 const char* type_name = row->
GetField(1);
434 if ((nfield>=numfields) ||
435 (strcmp(column_name, fields[nfield].
name)!=0))
437 SetError(-1,
"missmatch in column names",
"GetTableInfo");
443 Int_t data_size = -1;
444 Int_t data_length = -1;
445 Int_t data_scale = -1;
446 Int_t data_sign = -1;
448 if (IS_NUM(fields[nfield].
type)) {
449 if (fields[nfield].flags & UNSIGNED_FLAG)
455 Bool_t nullable = (fields[nfield].flags & NOT_NULL_FLAG) == 0;
457 data_length = fields[nfield].length;
458 if (data_length==0) data_length = -1;
460 #if MYSQL_VERSION_ID >= 40100
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:
470 case MYSQL_TYPE_DECIMAL:
472 data_scale = fields[nfield].decimals;
474 case MYSQL_TYPE_FLOAT:
477 case MYSQL_TYPE_DOUBLE:
480 case MYSQL_TYPE_TIMESTAMP:
483 case MYSQL_TYPE_DATE:
484 case MYSQL_TYPE_TIME:
485 case MYSQL_TYPE_DATETIME:
486 case MYSQL_TYPE_YEAR:
488 case MYSQL_TYPE_STRING:
489 if (fields[nfield].charsetnr==63)
493 data_size = data_length;
495 case MYSQL_TYPE_VAR_STRING:
496 if (fields[nfield].charsetnr==63)
500 data_size = data_length;
502 case MYSQL_TYPE_BLOB:
503 if (fields[nfield].charsetnr==63)
507 data_size = data_length;
510 case MYSQL_TYPE_ENUM:
511 case MYSQL_TYPE_GEOMETRY:
512 case MYSQL_TYPE_NULL:
515 if (IS_NUM(fields[nfield].type))
536 mysql_free_result(res);
539 sql.
Form(
"SHOW TABLE STATUS LIKE '%s'", tablename);
548 while ((row = stats->
Next()) != 0) {
549 if (strcmp(row->
GetField(0), tablename)!=0) {
553 const char* comments = 0;
554 const char* engine = 0;
555 const char* create_time = 0;
556 const char* update_time = 0;
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);
601 sql.
Form(
"SHOW COLUMNS FROM %s LIKE '%s'", table, wild);
603 sql.
Form(
"SHOW COLUMNS FROM %s", table);
662 #if MYSQL_VERSION_ID >= 50001 || \
663 (MYSQL_VERSION_ID < 50000 && MYSQL_VERSION_ID >= 40103)
664 res = mysql_shutdown(
fMySQL, SHUTDOWN_DEFAULT);
666 res = mysql_shutdown(
fMySQL);
681 const char* res = mysql_get_server_info(
fMySQL);
697 #if MYSQL_VERSION_ID < 40100
710 #if MYSQL_VERSION_ID < 40100
712 SetError(-1,
"Statement class does not supported by MySQL version < 4.1",
"Statement");
719 SetError(-1,
"no query string specified",
"Statement");
727 if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
729 mysql_stmt_close(stmt);
755 #if MYSQL_VERSION_ID >= 40100
777 #if MYSQL_VERSION_ID >= 40100
779 if (mysql_rollback(
fMySQL))
804 Error(
"PingVerify",
"not able to automatically reconnect a second time");
807 Info(
"PingVerify",
"connection was lost, but could automatically reconnect");
821 return mysql_ping(
fMySQL);
const char * GetHost() const
virtual Bool_t IsError() const
const char * ServerInfo()
Return server info in form "MySQL ".
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Collectable string class.
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
This class represents a WWW compatible URL.
TString & ReplaceAll(const TString &s1, const TString &s2)
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
const char * GetProtocol() const
~TMySQLServer()
Close connection to MySQL DB server.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Bool_t Exec(const char *sql)
Execute SQL command which does not produce any result sets.
static const char * filename()
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
TSQLResult * GetDataBases(const char *wild=0)
List all available databases.
Int_t Shutdown()
Shutdown the database server.
void Close(Option_t *opt="")
Close connection to MySQL DB server.
void ToLower()
Change string to lower-case.
TSQLStatement * Statement(const char *sql, Int_t=100)
Produce TMySQLStatement.
TSQLResult * Query(const char *sql)
Execute SQL command.
const char * GetOptions() const
const char * Data() const
void ClearError()
reset error fields
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
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.
Int_t Atoi() const
Return integer value of string.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual Bool_t IsConnected() const
#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.
ClassImp(TMySQLServer) TMySQLServer
Open a connection to a MySQL DB server.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
char * Form(const char *fmt,...)
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
virtual const char * GetField(Int_t field)=0
virtual Int_t GetFieldCount()=0
void Warning(const char *location, const char *msgfmt,...)
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Int_t Ping()
Execute Ping to SQL Connection using the mysql_ping function.
#define CheckConnect(method, res)
TString & Remove(Ssiz_t pos)
Bool_t Commit()
Commit changes.
virtual const char * GetName() const
Returns name of object.
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.
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
Mother of all ROOT objects.
virtual void Add(TObject *obj)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Bool_t PingVerify()
Execute Ping to SQL Connection.
Bool_t HasStatement() const
Return kTRUE if TSQLStatement class is supported.
virtual const char * GetFieldName(Int_t field)=0
Bool_t Rollback()
Rollback changes.
TSQLTableInfo * GetTableInfo(const char *tablename)
Produces SQL table info.
Int_t Reload()
Reload permission tables.
const char * GetFile() const
virtual TSQLRow * Next()=0