91 TString errmsg(
"malformed db argument ");
99 SetError(-1,
"protocol in db argument should be mysql://",
"TMySQLServer");
104 const char* dbase = url.
GetFile();
106 if (*dbase==
'/') dbase++;
119 while ((obj = next()) != 0) {
128 mysql_options(
fMySQL, MYSQL_OPT_CONNECT_TIMEOUT, (
const char*) &mysqltimeout);
129 if (
gDebug)
Info(
"TMySQLServer",
"Set timeout %d",timeout);
132 if (opt.
Contains(
"read_timeout=")) {
133 #if MYSQL_VERSION_ID >= 40101
138 mysql_options(
fMySQL, MYSQL_OPT_READ_TIMEOUT, (
const char*) &mysqltimeout);
139 if (
gDebug)
Info(
"TMySQLServer",
"Set read timeout %d", timeout);
142 Warning(
"TMySQLServer",
"MYSQL_OPT_READ_TIMEOUT option not supported by this version of MySql");
146 if (opt.
Contains(
"write_timeout=")) {
147 #if MYSQL_VERSION_ID >= 40101
152 mysql_options(
fMySQL, MYSQL_OPT_WRITE_TIMEOUT, (
const char*) &mysqltimeout);
153 if (
gDebug)
Info(
"TMySQLServer",
"Set write timeout %d", timeout);
156 Warning(
"TMySQLServer",
"MYSQL_OPT_WRITE_TIMEOUT option not supported by this version of MySql");
160 #if MYSQL_VERSION_ID >= 50013
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"));
166 Warning(
"TMySQLServer",
"MYSQL_OPT_RECONNECT option not supported by this version of MySql");
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");
178 Warning(
"TMySQLServer",
"CLIENT_MULTI_STATEMENTS not supported by this version of MySql");
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");
186 Warning(
"TMySQLServer",
"CLIENT_MULTI_RESULTS not supported by this version of MySql");
190 mysql_options(
fMySQL, MYSQL_OPT_COMPRESS, 0);
191 if (
gDebug)
Info(
"TMySQLServer",
"Use compressed client/server protocol");
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);
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);
211 if (mysql_real_connect(
fMySQL, url.
GetHost(), uid, pw, dbase, port,
212 (
socket.Length()>0) ?
socket.Data() : 0 , client_flag)) {
234#define CheckConnect(method, res) \
237 if (!IsConnected()) { \
238 SetError(-1,"MySQL server is not connected",method); \
245#define CheckErrNo(method, force, res) \
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); \
280 if (mysql_query(
fMySQL, sql))
283 MYSQL_RES *res = mysql_store_result(
fMySQL);
297 if (mysql_query(
fMySQL, sql))
311 if (res==0)
fDB = dbname;
327 MYSQL_RES *res = mysql_list_dbs(
fMySQL, wild);
346 MYSQL_RES *res = mysql_list_tables(
fMySQL, wild);
361 MYSQL_RES *res = mysql_list_tables(
fMySQL, wild);
365 MYSQL_ROW row = mysql_fetch_row(res);
367 TList *lst =
nullptr;
372 const char *tablename = row[0];
382 row = mysql_fetch_row(res);
385 mysql_free_result(res);
398 if (!tablename || (*tablename==0))
return nullptr;
401 sql.
Form(
"SELECT * FROM `%s` LIMIT 1", tablename);
406 MYSQL_RES *res = mysql_store_result(
fMySQL);
409 unsigned int numfields = mysql_num_fields(res);
411 MYSQL_FIELD* fields = mysql_fetch_fields(res);
413 sql.
Form(
"SHOW COLUMNS FROM `%s`", tablename);
417 mysql_free_result(res);
421 TList *lst =
nullptr;
423 unsigned int nfield = 0;
427 while ((row = showres->
Next()) != 0) {
428 const char* column_name = row->
GetField(0);
429 const char* type_name = row->
GetField(1);
431 if ((nfield>=numfields) ||
432 (strcmp(column_name, fields[nfield].
name)!=0))
434 SetError(-1,
"mismatch in column names",
"GetTableInfo");
440 Int_t data_size = -1;
441 Int_t data_length = -1;
442 Int_t data_scale = -1;
443 Int_t data_sign = -1;
445 if (IS_NUM(fields[nfield].
type)) {
446 if (fields[nfield].flags & UNSIGNED_FLAG)
452 Bool_t nullable = (fields[nfield].flags & NOT_NULL_FLAG) == 0;
454 data_length = fields[nfield].length;
455 if (data_length==0) data_length = -1;
457#if MYSQL_VERSION_ID >= 40100
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:
467 case MYSQL_TYPE_DECIMAL:
469 data_scale = fields[nfield].decimals;
471 case MYSQL_TYPE_FLOAT:
474 case MYSQL_TYPE_DOUBLE:
477 case MYSQL_TYPE_TIMESTAMP:
480 case MYSQL_TYPE_DATE:
481 case MYSQL_TYPE_TIME:
482 case MYSQL_TYPE_DATETIME:
483 case MYSQL_TYPE_YEAR:
485 case MYSQL_TYPE_STRING:
486 if (fields[nfield].charsetnr==63)
490 data_size = data_length;
492 case MYSQL_TYPE_VAR_STRING:
493 if (fields[nfield].charsetnr==63)
497 data_size = data_length;
499 case MYSQL_TYPE_BLOB:
500 if (fields[nfield].charsetnr==63)
504 data_size = data_length;
507 case MYSQL_TYPE_ENUM:
508 case MYSQL_TYPE_GEOMETRY:
509 case MYSQL_TYPE_NULL:
512 if (IS_NUM(fields[nfield].
type))
533 mysql_free_result(res);
536 sql.
Form(
"SHOW TABLE STATUS LIKE '%s'", tablename);
545 while ((row = stats->
Next()) != 0) {
546 if (strcmp(row->
GetField(0), tablename)!=0) {
550 const char* comments = 0;
551 const char* engine = 0;
552 const char* create_time = 0;
553 const char* update_time = 0;
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);
598 sql.
Form(
"SHOW COLUMNS FROM %s LIKE '%s'", table, wild);
600 sql.
Form(
"SHOW COLUMNS FROM %s", table);
659#if MYSQL_VERSION_ID >= 50001 || \
660 (MYSQL_VERSION_ID < 50000 && MYSQL_VERSION_ID >= 40103)
661 res = mysql_shutdown(
fMySQL, SHUTDOWN_DEFAULT);
663 res = mysql_shutdown(
fMySQL);
678 const char* res = mysql_get_server_info(
fMySQL);
694#if MYSQL_VERSION_ID < 40100
707#if MYSQL_VERSION_ID < 40100
709 SetError(-1,
"Statement class does not supported by MySQL version < 4.1",
"Statement");
716 SetError(-1,
"no query string specified",
"Statement");
724 if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
726 mysql_stmt_close(stmt);
752#if MYSQL_VERSION_ID >= 40100
774#if MYSQL_VERSION_ID >= 40100
776 if (mysql_rollback(
fMySQL))
801 Error(
"PingVerify",
"not able to automatically reconnect a second time");
804 Info(
"PingVerify",
"connection was lost, but could automatically reconnect");
818 return mysql_ping(
fMySQL);
#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.
virtual void Add(TObject *obj)
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.
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.
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.
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Collectable string class.
Mother of all ROOT objects.
virtual const char * GetName() const
Returns name of object.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
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
void ClearError()
reset error fields
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 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 Bool_t IsError() const
virtual Bool_t IsConnected() const
void ToLower()
Change string to lower-case.
Int_t Atoi() const
Return integer value of string.
const char * Data() const
TString & ReplaceAll(const TString &s1, const TString &s2)
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
TString & Remove(Ssiz_t pos)
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
This class represents a WWW compatible URL.
const char * GetFile() const
const char * GetHost() const
const char * GetOptions() const
const char * GetProtocol() const