94 if ((strncmp(db,
"odbc", 4)!=0) || (strlen(db)<8)) {
95 SetError(-1,
"db argument should be started from odbc...",
"TODBCServer");
99 if (strncmp(db,
"odbc://", 7)==0) {
102 SetError(-1,
Form(
"not valid URL: %s", db),
"TODBCServer");
105 const char* driver =
"MyODBC";
106 const char* dbase = url.
GetFile();
108 if (*dbase==
'/') dbase++;
110 if ((!uid || (*uid==0)) && (strlen(url.
GetUser())>0)) {
117 connstr.
Form(
"DRIVER={%s};"
123 driver, url.
GetHost(), dbase, uid, pw);
132 if (strncmp(db,
"odbcd://", 8)==0) {
136 if (strncmp(db,
"odbcn://", 8)==0) {
138 simpleconnect =
kTRUE;
140 SetError(-1,
"db argument is invalid",
"TODBCServer");
144 retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &
fHenv);
148 retcode = SQLSetEnvAttr(
fHenv, SQL_ATTR_ODBC_VERSION, (
void*)SQL_OV_ODBC3, 0);
152 retcode = SQLAllocHandle(SQL_HANDLE_DBC,
fHenv, &
fHdbc);
156 retcode = SQLSetConnectAttr(
fHdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER) 5, 0);
167 retcode = SQLConnect(
fHdbc, (SQLCHAR*) connstr.
Data(), SQL_NTS,
168 (SQLCHAR*) uid, SQL_NTS,
169 (SQLCHAR*) pw, SQL_NTS);
171 retcode = SQLDriverConnect(
fHdbc, hwnd,
172 (SQLCHAR*) connstr.
Data(), SQL_NTS,
173 (SQLCHAR*) sbuf,
sizeof(sbuf), &reslen, SQL_DRIVER_NOPROMPT);
179 retcode = SQLGetInfo(
fHdbc, SQL_USER_NAME, sbuf,
sizeof(sbuf), &reslen);
183 retcode = SQLGetInfo(
fHdbc, SQL_DBMS_NAME, sbuf,
sizeof(sbuf), &reslen);
188 retcode = SQLGetInfo(
fHdbc, SQL_DBMS_VER, sbuf,
sizeof(sbuf), &reslen);
194 retcode = SQLGetConnectAttr(
fHdbc, SQL_ATTR_CURRENT_CATALOG, sbuf,
sizeof(sbuf), &reslen1);
198 retcode = SQLGetInfo(
fHdbc, SQL_SERVER_NAME, sbuf,
sizeof(sbuf), &reslen);
244 retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
245 if ((retcode!=SQL_SUCCESS) && (retcode!=SQL_SUCCESS_WITH_INFO))
return nullptr;
247 retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (
void*)SQL_OV_ODBC3, 0);
248 if ((retcode!=SQL_SUCCESS) && (retcode!=SQL_SUCCESS_WITH_INFO))
return nullptr;
250 TList* lst =
nullptr;
252 char namebuf[2048], optbuf[2048];
253 SQLSMALLINT reslen1, reslen2;
256 strlcpy(namebuf,
"",2048);
257 strlcpy(optbuf,
"",2048);
259 retcode = SQLDrivers(henv, (!lst ? SQL_FETCH_FIRST : SQL_FETCH_NEXT),
260 (SQLCHAR*) namebuf,
sizeof(namebuf), &reslen1,
261 (SQLCHAR*) optbuf,
sizeof(optbuf), &reslen2);
263 retcode = SQLDataSources(henv, (!lst ? SQL_FETCH_FIRST : SQL_FETCH_NEXT),
264 (SQLCHAR*) namebuf,
sizeof(namebuf), &reslen1,
265 (SQLCHAR*) optbuf,
sizeof(optbuf), &reslen2);
267 if (retcode==SQL_NO_DATA)
break;
268 if ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO)) {
273 for (
int n = 0;
n < reslen2 - 1;
n++)
274 if (optbuf[
n] ==
'\0')
279 }
while ((retcode==SQL_SUCCESS) || (retcode==SQL_SUCCESS_WITH_INFO));
281 SQLFreeHandle(SQL_HANDLE_ENV, henv);
305 std::cout <<
"List of ODBC drivers:" << std::endl;
307 while (
auto n =
dynamic_cast<TNamed *
>(iter()))
308 std::cout <<
" " <<
n->GetName() <<
" : " <<
n->GetTitle() << std::endl;
330 std::cout <<
"List of ODBC data sources:" << std::endl;
332 while (
auto n =
dynamic_cast<TNamed *
>(iter()))
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)
356 while (SQLGetDiagRec(SQL_HANDLE_DBC,
fHdbc, ++i, state, &native,
text,
357 sizeof(
text), &
len ) == SQL_SUCCESS)
364#define CheckConnect(method, res) \
367 if (!IsConnected()) { \
368 SetError(-1,"ODBC driver is not connected",method); \
378 SQLDisconnect(
fHdbc);
379 SQLFreeHandle(SQL_HANDLE_DBC,
fHdbc);
380 SQLFreeHandle(SQL_HANDLE_ENV,
fHenv);
396 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
398 retcode = SQLExecDirect(hstmt, (SQLCHAR*) sql, SQL_NTS);
400 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
418 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
420 retcode = SQLExecDirect(hstmt, (SQLCHAR*) sql, SQL_NTS);
424 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
438 SQLRETURN retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_CURRENT_CATALOG, (SQLCHAR*) db, SQL_NTS);
472 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
474 SQLCHAR* schemaName =
nullptr;
475 SQLSMALLINT schemaNameLength = 0;
489 SQLCHAR* tableName =
nullptr;
490 SQLSMALLINT tableNameLength = 0;
493 tableName = (SQLCHAR*) wild;
494 tableNameLength = strlen(wild);
495 SQLSetStmtAttr(hstmt, SQL_ATTR_METADATA_ID, (SQLPOINTER) SQL_FALSE, 0);
498 retcode = SQLTables(hstmt,
nullptr, 0, schemaName, schemaNameLength, tableName, tableNameLength, (SQLCHAR*)
"TABLE", 5);
500 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
516 if (!res)
return nullptr;
518 TList* lst =
nullptr;
522 while ((row = res->
Next()) !=
nullptr) {
523 const char* tablename = row->
GetField(2);
549 #define STR_LEN 128+1
550 #define REM_LEN 254+1
558 SQLLEN columnSize, bufferLength, charOctetLength, ordinalPosition;
559 SQLSMALLINT dataType, decimalDigits, numPrecRadix, nullable;
560 SQLSMALLINT sqlDataType, datetimeSubtypeCode;
566 SQLLEN cbCatalog, cbSchema, cbTableName, cbColumnName;
567 SQLLEN cbDataType, cbTypeName, cbColumnSize, cbBufferLength;
568 SQLLEN cbDecimalDigits, cbNumPrecRadix, cbNullable, cbRemarks;
569 SQLLEN cbColumnDefault, cbSQLDataType, cbDatetimeSubtypeCode, cbCharOctetLength;
570 SQLLEN cbOrdinalPosition, cbIsNullable;
573 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
575 retcode = SQLColumns(hstmt,
nullptr, 0,
nullptr, 0, (SQLCHAR*) tablename, SQL_NTS,
nullptr, 0);
577 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
581 TList* lst =
nullptr;
585 SQLBindCol(hstmt, 1, SQL_C_CHAR, szCatalog,
STR_LEN,&cbCatalog);
586 SQLBindCol(hstmt, 2, SQL_C_CHAR, szSchema,
STR_LEN, &cbSchema);
587 SQLBindCol(hstmt, 3, SQL_C_CHAR, szTableName,
STR_LEN,&cbTableName);
588 SQLBindCol(hstmt, 4, SQL_C_CHAR, szColumnName,
STR_LEN, &cbColumnName);
589 SQLBindCol(hstmt, 5, SQL_C_SSHORT, &dataType, 0, &cbDataType);
590 SQLBindCol(hstmt, 6, SQL_C_CHAR, szTypeName,
STR_LEN, &cbTypeName);
591 SQLBindCol(hstmt, 7, SQL_C_SLONG, &columnSize, 0, &cbColumnSize);
592 SQLBindCol(hstmt, 8, SQL_C_SLONG, &bufferLength, 0, &cbBufferLength);
593 SQLBindCol(hstmt, 9, SQL_C_SSHORT, &decimalDigits, 0, &cbDecimalDigits);
594 SQLBindCol(hstmt, 10, SQL_C_SSHORT, &numPrecRadix, 0, &cbNumPrecRadix);
595 SQLBindCol(hstmt, 11, SQL_C_SSHORT, &nullable, 0, &cbNullable);
596 SQLBindCol(hstmt, 12, SQL_C_CHAR, szRemarks,
REM_LEN, &cbRemarks);
597 SQLBindCol(hstmt, 13, SQL_C_CHAR, szColumnDefault,
STR_LEN, &cbColumnDefault);
598 SQLBindCol(hstmt, 14, SQL_C_SSHORT, &sqlDataType, 0, &cbSQLDataType);
599 SQLBindCol(hstmt, 15, SQL_C_SSHORT, &datetimeSubtypeCode, 0, &cbDatetimeSubtypeCode);
600 SQLBindCol(hstmt, 16, SQL_C_SLONG, &charOctetLength, 0, &cbCharOctetLength);
601 SQLBindCol(hstmt, 17, SQL_C_SLONG, &ordinalPosition, 0, &cbOrdinalPosition);
602 SQLBindCol(hstmt, 18, SQL_C_CHAR, szIsNullable,
STR_LEN, &cbIsNullable);
604 retcode = SQLFetch(hstmt);
606 while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO)) {
610 Int_t data_size = -1;
611 Int_t data_length = -1;
612 Int_t data_scale = -1;
613 Int_t data_sign = -1;
618 data_size = columnSize;
619 data_length = charOctetLength;
622 case SQL_LONGVARCHAR:
624 data_size = columnSize;
625 data_length = charOctetLength;
630 data_size = columnSize;
631 data_length = columnSize;
632 data_scale = decimalDigits;
638 data_size = columnSize;
643 data_size = columnSize;
648 data_size = columnSize;
653 case SQL_LONGVARBINARY:
655 data_size = columnSize;
657 case SQL_TYPE_TIMESTAMP:
659 data_size = columnSize;
663 if (!lst) lst =
new TList;
666 (
const char *) szTypeName,
674 retcode = SQLFetch(hstmt);
677 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
695 SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
697 retcode = SQLColumns(hstmt,
nullptr, 0,
nullptr, 0, (SQLCHAR*) table, SQL_NTS,
nullptr, 0);
699 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
713 SQLUINTEGER info = 0;
716 retcode = SQLGetInfo(
fHdbc, SQL_MAX_IDENTIFIER_LEN, (SQLPOINTER)&info,
sizeof(info),
nullptr);
718 if (
ExtractErrors(retcode,
"GetMaxIdentifierLength"))
return 20;
785 SetError(-1,
"no query string specified",
"Statement");
799 retcode = SQLAllocHandle(SQL_HANDLE_STMT,
fHdbc, &hstmt);
802 retcode = SQLPrepare(hstmt, (SQLCHAR*) sql, SQL_NTS);
804 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
820 SQLUINTEGER info = 0;
823 retcode = SQLGetInfo(
fHdbc, SQL_TXN_CAPABLE, (SQLPOINTER)&info,
sizeof(info),
nullptr);
827 SetError(-1,
"Transactions not supported",
"StartTransaction");
833 retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_OFF, 0);
845 const char* method = commit ?
"Commit" :
"Rollback";
849 SQLRETURN retcode = SQLEndTran(SQL_HANDLE_DBC,
fHdbc, commit ? SQL_COMMIT : SQL_ROLLBACK);
852 retcode = SQLSetConnectAttr(
fHdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, 0);
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char text
#define CheckConnect(method, res)
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
void Add(TObject *obj) override
The TNamed class is the base class for all named ROOT classes.
Bool_t ExtractErrors(SQLRETURN retcode, const char *method)
Extract errors, produced by last ODBC function call.
static void PrintDrivers()
Print list of ODBC drivers in form: <name> : <options-list>
TSQLResult * GetTables(const char *dbname, const char *wild=nullptr) final
List all tables in the specified database.
TSQLResult * GetDataBases(const char *wild=nullptr) final
List all available databases.
Int_t Shutdown() final
Shutdown the database server.
static TList * ListData(Bool_t isdrivers)
Produce TList object with list of available ODBC drivers (isdrivers = kTRUE) or data sources (isdrive...
TODBCServer(const char *db, const char *uid, const char *pw)
Open a connection to a ODBC server.
TSQLStatement * Statement(const char *sql, Int_t=100) final
Creates ODBC statement for provided query.
Bool_t EndTransaction(Bool_t commit)
Complete current transaction (commit = kTRUE) or rollback Switches on autocommit mode of ODBC driver.
Int_t Reload() final
Reload permission tables.
Bool_t StartTransaction() final
Starts transaction.
Int_t SelectDataBase(const char *dbname) final
Select a database.
virtual ~TODBCServer()
Close connection to MySQL DB server.
void Close(Option_t *opt="") final
Close connection to MySQL DB server.
Int_t GetMaxIdentifierLength() final
returns maximum allowed length of identifier (table name, column name, index name)
TSQLTableInfo * GetTableInfo(const char *tablename) final
Produces SQL table info Object must be deleted by user.
static TList * GetDrivers()
Produce TList object with list of available ODBC drivers User must delete TList object afterwards Nam...
Bool_t Rollback() final
Rollback transaction.
Int_t DropDataBase(const char *dbname) final
Drop (i.e.
static void PrintDataSources()
Print list of ODBC data sources in form: <name> : <options list>
const char * ServerInfo() final
Return server info.
TSQLResult * Query(const char *sql) final
Execute SQL command.
Int_t CreateDataBase(const char *dbname) final
Create a database. Returns 0 if successful, non-zero otherwise.
static TList * GetDataSources()
Produce TList object with list of available ODBC data sources User must delete TList object afterward...
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr) final
List all columns in specified table in the specified database.
TList * GetTablesList(const char *wild=nullptr) final
Return list of tables in database See TSQLServer::GetTablesList() for details.
Bool_t Commit() final
Commit transaction.
Bool_t Exec(const char *sql) final
Executes query which does not produce any results set Return kTRUE if successful.
Collectable string class.
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
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 IsConnected() const
const char * Data() const
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
This class represents a WWW compatible URL.
const char * GetFile() const
const char * GetUser() const
const char * GetHost() const
const char * GetPasswd() const
const char * GetOptions() const