92 if ((strncmp(db,
"odbc", 4)!=0) || (strlen(db)<8)) {
93 SetError(-1,
"db argument should be started from odbc...",
"TODBCServer");
97 if (strncmp(db,
"odbc://", 7)==0) {
100 SetError(-1,
Form(
"not valid URL: %s", db),
"TODBCServer");
103 const char* driver =
"MyODBC";
104 const char* dbase = url.
GetFile();
106 if (*dbase==
'/') dbase++;
108 if (((uid==0) || (*uid==0)) && (strlen(url.
GetUser())>0)) {
115 connstr.
Form(
"DRIVER={%s};"
121 driver, url.
GetHost(), dbase, uid, pw);
130 if (strncmp(db,
"odbcd://", 8)==0) {
134 if (strncmp(db,
"odbcn://", 8)==0) {
136 simpleconnect =
kTRUE;
138 SetError(-1,
"db argument is invalid",
"TODBCServer");
142 retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &fHenv);
143 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
146 retcode = SQLSetEnvAttr(fHenv, SQL_ATTR_ODBC_VERSION, (
void*)SQL_OV_ODBC3, 0);
147 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
150 retcode = SQLAllocHandle(SQL_HANDLE_DBC, fHenv, &fHdbc);
151 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
154 retcode = SQLSetConnectAttr(fHdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER) 5, 0);
155 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
165 retcode = SQLConnect(fHdbc, (SQLCHAR*) connstr.
Data(), SQL_NTS,
166 (SQLCHAR*) uid, SQL_NTS,
167 (SQLCHAR*) pw, SQL_NTS);
169 retcode = SQLDriverConnect(fHdbc, hwnd,
170 (SQLCHAR*) connstr.
Data(), SQL_NTS,
171 (SQLCHAR*) sbuf,
sizeof(sbuf), &reslen, SQL_DRIVER_NOPROMPT);
173 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
177 retcode = SQLGetInfo(fHdbc, SQL_USER_NAME, sbuf,
sizeof(sbuf), &reslen);
178 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
181 retcode = SQLGetInfo(fHdbc, SQL_DBMS_NAME, sbuf,
sizeof(sbuf), &reslen);
182 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
186 retcode = SQLGetInfo(fHdbc, SQL_DBMS_VER, sbuf,
sizeof(sbuf), &reslen);
187 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
192 retcode = SQLGetConnectAttr(fHdbc, SQL_ATTR_CURRENT_CATALOG, sbuf,
sizeof(sbuf), &reslen1);
193 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
194 if (fDB.Length()==0) fDB = sbuf;
196 retcode = SQLGetInfo(fHdbc, SQL_SERVER_NAME, sbuf,
sizeof(sbuf), &reslen);
197 if (ExtractErrors(retcode,
"TODBCServer"))
goto zombie;
198 if (fHost.Length()==0) fHost = sbuf;
242 retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
243 if ((retcode!=SQL_SUCCESS) && (retcode!=SQL_SUCCESS_WITH_INFO))
return 0;
245 retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (
void*)SQL_OV_ODBC3, 0);
246 if ((retcode!=SQL_SUCCESS) && (retcode!=SQL_SUCCESS_WITH_INFO))
return 0;
250 char namebuf[2048], optbuf[2048];
251 SQLSMALLINT reslen1, reslen2;
254 strlcpy(namebuf,
"",2048);
255 strlcpy(optbuf,
"",2048);
257 retcode = SQLDrivers(henv, (lst==0 ? SQL_FETCH_FIRST : SQL_FETCH_NEXT),
258 (SQLCHAR*) namebuf,
sizeof(namebuf), &reslen1,
259 (SQLCHAR*) optbuf,
sizeof(optbuf), &reslen2);
261 retcode = SQLDataSources(henv, (lst==0 ? SQL_FETCH_FIRST : SQL_FETCH_NEXT),
262 (SQLCHAR*) namebuf,
sizeof(namebuf), &reslen1,
263 (SQLCHAR*) optbuf,
sizeof(optbuf), &reslen2);
265 if (retcode==SQL_NO_DATA)
break;
266 if ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO)) {
271 for (
int n=0;
n<reslen2-1;
n++)
272 if (optbuf[
n]==
'\0') optbuf[
n] =
';';
276 }
while ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO));
278 SQLFreeHandle(SQL_HANDLE_ENV, henv);
303 std::cout <<
"List of ODBC drivers:" << std::endl;
307 std::cout <<
" " << n->
GetName() <<
" : " << n->
GetTitle() << std::endl;
329 std::cout <<
"List of ODBC data sources:" << std::endl;
333 std::cout <<
" " << n->
GetName() <<
" : " << n->
GetTitle() << std::endl;
342 if ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO))
return kFALSE;
350 while (SQLGetDiagRec(SQL_HANDLE_ENV,
fHenv, ++i, state, &native, text,
351 sizeof(text), &len ) == SQL_SUCCESS)
353 SetError(native, (
const char*) text, method);
357 while (SQLGetDiagRec(SQL_HANDLE_DBC,
fHdbc, ++i, state, &native, text,
358 sizeof(text), &len ) == SQL_SUCCESS)
360 SetError(native, (
const char*) text, method);
366 #define CheckConnect(method, res) \
369 if (!IsConnected()) { \
370 SetError(-1,"ODBC driver is not connected",method); \
380 SQLDisconnect(
fHdbc);
381 SQLFreeHandle(SQL_HANDLE_DBC,
fHdbc);
382 SQLFreeHandle(SQL_HANDLE_ENV,
fHenv);
398 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
400 retcode = SQLExecDirect(hstmt, (SQLCHAR*) sql, SQL_NTS);
402 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
420 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
422 retcode = SQLExecDirect(hstmt, (SQLCHAR*) sql, SQL_NTS);
426 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
440 SQLRETURN retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_CURRENT_CATALOG, (SQLCHAR*) db, SQL_NTS);
474 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
476 SQLCHAR* schemaName = 0;
477 SQLSMALLINT schemaNameLength = 0;
491 SQLCHAR* tableName = 0;
492 SQLSMALLINT tableNameLength = 0;
494 if ((wild!=0) && (strlen(wild)!=0)) {
495 tableName = (SQLCHAR*) wild;
496 tableNameLength = strlen(wild);
497 SQLSetStmtAttr(hstmt, SQL_ATTR_METADATA_ID, (SQLPOINTER) SQL_FALSE, 0);
500 retcode = SQLTables(hstmt,
NULL, 0, schemaName, schemaNameLength, tableName, tableNameLength, (SQLCHAR*)
"TABLE", 5);
502 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
518 if (res==0)
return 0;
524 while ((row = res->
Next())!=0) {
525 const char* tablename = row->
GetField(2);
551 #define STR_LEN 128+1
552 #define REM_LEN 254+1
560 SQLLEN columnSize, bufferLength, charOctetLength, ordinalPosition;
561 SQLSMALLINT dataType, decimalDigits, numPrecRadix, nullable;
562 SQLSMALLINT sqlDataType, datetimeSubtypeCode;
568 SQLLEN cbCatalog, cbSchema, cbTableName, cbColumnName;
569 SQLLEN cbDataType, cbTypeName, cbColumnSize, cbBufferLength;
570 SQLLEN cbDecimalDigits, cbNumPrecRadix, cbNullable, cbRemarks;
571 SQLLEN cbColumnDefault, cbSQLDataType, cbDatetimeSubtypeCode, cbCharOctetLength;
572 SQLLEN cbOrdinalPosition, cbIsNullable;
575 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
577 retcode = SQLColumns(hstmt,
NULL, 0,
NULL, 0, (SQLCHAR*) tablename, SQL_NTS,
NULL, 0);
579 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
587 SQLBindCol(hstmt, 1, SQL_C_CHAR, szCatalog,
STR_LEN,&cbCatalog);
588 SQLBindCol(hstmt, 2, SQL_C_CHAR, szSchema,
STR_LEN, &cbSchema);
589 SQLBindCol(hstmt, 3, SQL_C_CHAR, szTableName,
STR_LEN,&cbTableName);
590 SQLBindCol(hstmt, 4, SQL_C_CHAR, szColumnName,
STR_LEN, &cbColumnName);
591 SQLBindCol(hstmt, 5, SQL_C_SSHORT, &dataType, 0, &cbDataType);
592 SQLBindCol(hstmt, 6, SQL_C_CHAR, szTypeName,
STR_LEN, &cbTypeName);
593 SQLBindCol(hstmt, 7, SQL_C_SLONG, &columnSize, 0, &cbColumnSize);
594 SQLBindCol(hstmt, 8, SQL_C_SLONG, &bufferLength, 0, &cbBufferLength);
595 SQLBindCol(hstmt, 9, SQL_C_SSHORT, &decimalDigits, 0, &cbDecimalDigits);
596 SQLBindCol(hstmt, 10, SQL_C_SSHORT, &numPrecRadix, 0, &cbNumPrecRadix);
597 SQLBindCol(hstmt, 11, SQL_C_SSHORT, &nullable, 0, &cbNullable);
598 SQLBindCol(hstmt, 12, SQL_C_CHAR, szRemarks,
REM_LEN, &cbRemarks);
599 SQLBindCol(hstmt, 13, SQL_C_CHAR, szColumnDefault,
STR_LEN, &cbColumnDefault);
600 SQLBindCol(hstmt, 14, SQL_C_SSHORT, &sqlDataType, 0, &cbSQLDataType);
601 SQLBindCol(hstmt, 15, SQL_C_SSHORT, &datetimeSubtypeCode, 0, &cbDatetimeSubtypeCode);
602 SQLBindCol(hstmt, 16, SQL_C_SLONG, &charOctetLength, 0, &cbCharOctetLength);
603 SQLBindCol(hstmt, 17, SQL_C_SLONG, &ordinalPosition, 0, &cbOrdinalPosition);
604 SQLBindCol(hstmt, 18, SQL_C_CHAR, szIsNullable,
STR_LEN, &cbIsNullable);
606 retcode = SQLFetch(hstmt);
608 while ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO)) {
612 Int_t data_size = -1;
613 Int_t data_length = -1;
614 Int_t data_scale = -1;
615 Int_t data_sign = -1;
620 data_size = columnSize;
621 data_length = charOctetLength;
624 case SQL_LONGVARCHAR:
626 data_size = columnSize;
627 data_length = charOctetLength;
632 data_size = columnSize;
633 data_length = columnSize;
634 data_scale = decimalDigits;
640 data_size = columnSize;
645 data_size = columnSize;
650 data_size = columnSize;
655 case SQL_LONGVARBINARY:
657 data_size = columnSize;
659 case SQL_TYPE_TIMESTAMP:
661 data_size = columnSize;
665 if (lst==0) lst =
new TList;
668 (
const char*) szTypeName,
676 retcode = SQLFetch(hstmt);
679 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
697 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
699 retcode = SQLColumns(hstmt,
NULL, 0,
NULL, 0, (SQLCHAR*) table, SQL_NTS,
NULL, 0);
701 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
715 SQLUINTEGER info = 0;
718 retcode = SQLGetInfo(
fHdbc, SQL_MAX_IDENTIFIER_LEN, (SQLPOINTER)&info,
sizeof(info),
NULL);
720 if (
ExtractErrors(retcode,
"GetMaxIdentifierLength"))
return 20;
787 SetError(-1,
"no query string specified",
"Statement");
801 retcode = SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
804 retcode = SQLPrepare(hstmt, (SQLCHAR*) sql, SQL_NTS);
806 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
822 SQLUINTEGER info = 0;
825 retcode = SQLGetInfo(
fHdbc, SQL_TXN_CAPABLE, (SQLPOINTER)&info,
sizeof(info),
NULL);
829 SetError(-1,
"Transactions not supported",
"StartTransaction");
835 retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_OFF, 0);
847 const char* method = commit ?
"Commit" :
"Rollback";
851 SQLRETURN retcode = SQLEndTran(SQL_HANDLE_DBC,
fHdbc, commit ? SQL_COMMIT : SQL_ROLLBACK);
854 retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, 0);
const char * GetHost() const
virtual const char * GetTitle() const
Returns title of object.
Int_t SelectDataBase(const char *dbname)
Select a database.
static void PrintDrivers()
Print list of ODBC drivers in form: : ="">
Collectable string class.
static TList * ListData(Bool_t isdrivers)
Produce TList object with list of available ODBC drivers (isdrivers = kTRUE) or data sources (isdrive...
Bool_t EndTransaction(Bool_t commit)
Complete current transaction (commit = kTRUE) or rollback Switches on autocommit mode of ODBC driver...
TSQLResult * Query(const char *sql)
Execute SQL command.
This class represents a WWW compatible URL.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TSQLTableInfo * GetTableInfo(const char *tablename)
Produces SQL table info Object must be deleted by user.
ClassImp(TODBCServer) TODBCServer
Open a connection to a ODBC server.
const char * GetOptions() const
Bool_t Exec(const char *sql)
Executes query which does not produce any results set Return kTRUE if successfull.
const char * Data() const
void Close(Option_t *opt="")
Close connection to MySQL DB server.
The TNamed class is the base class for all named ROOT classes.
const char * GetPasswd() const
std::map< std::string, std::string >::const_iterator iter
static TList * GetDataSources()
Produce TList object with list of available ODBC data sources User must delete TList object aftewards...
virtual Bool_t IsConnected() const
TList * GetTablesList(const char *wild=0)
Return list of tables in database See TSQLServer::GetTablesList() for details.
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
char * Form(const char *fmt,...)
virtual const char * GetField(Int_t field)=0
virtual const char * GetName() const
Returns name of object.
Bool_t ExtractErrors(SQLRETURN retcode, const char *method)
Extract errors, produced by last ODBC function call.
static TList * GetDrivers()
Produce TList object with list of available ODBC drivers User must delete TList object aftewards Name...
TSQLStatement * Statement(const char *sql, Int_t=100)
Creates ODBC statement for provided query.
Bool_t StartTransaction()
Starts transaction.
Int_t Shutdown()
Shutdown the database server.
#define CheckConnect(method, res)
TSQLResult * GetTables(const char *dbname, const char *wild=0)
List all tables in the specified database.
Int_t DropDataBase(const char *dbname)
Drop (i.e.
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
Bool_t Commit()
Commit transaction.
Int_t Reload()
Reload permission tables.
static void PrintDataSources()
Print list of ODBC data sources in form: : ="">
const char * ServerInfo()
Return server info.
TSQLResult * GetDataBases(const char *wild=0)
List all available databases.
virtual void Add(TObject *obj)
virtual ~TODBCServer()
Close connection to MySQL DB server.
Bool_t Rollback()
Rollback transaction.
const char * GetUser() const
const char * GetFile() const
Int_t CreateDataBase(const char *dbname)
Create a database. Returns 0 if successful, non-zero otherwise.
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=0)
List all columns in specified table in the specified database.
virtual TSQLRow * Next()=0
Int_t GetMaxIdentifierLength()
returns maximum allowed length of identifier (table name, column name, index name) ...