File indexing completed on 2025-08-03 08:22:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <RDBC/TRDBCServer.h>
0014 #include <RDBC/TSQLConnection.h>
0015 #include <RDBC/TSQLDriverManager.h>
0016 #include <RDBC/TSQLResultSet.h>
0017 #include <RDBC/TSQLDatabaseMetaData.h>
0018 #include <RDBC/TSQLStatement.h>
0019 #include <iostream>
0020
0021 ClassImp(TRDBCServer)
0022
0023
0024 TRDBCServer::TRDBCServer(const char *db, const char *uid, const char *pw)
0025 {
0026
0027
0028
0029
0030
0031
0032
0033
0034 fConnection = TSQLDriverManager::GetConnection(db,uid,pw);
0035
0036 if(!fConnection) {
0037 Error("TRDBCServer", "connection to %s failed", db);
0038 return;
0039 }
0040 TSQLDatabaseMetaData* md = fConnection->GetMetaData();
0041 fDbName = md->GetDatabaseProductName();
0042 }
0043
0044
0045 TRDBCServer::~TRDBCServer()
0046 {
0047
0048
0049 if (IsConnected()) Close();
0050 }
0051
0052
0053 void TRDBCServer::Close(Option_t *)
0054 {
0055
0056
0057 if (!fConnection) return;
0058 fConnection->Close();
0059 if(fConnection->IsClosed()) fConnection = 0;
0060 }
0061
0062
0063 TSQLResultSet* TRDBCServer::Query(const char *sql)
0064 {
0065
0066
0067
0068
0069 if (!IsConnected()) {
0070 Error("Query", "not connected");
0071 return 0;
0072 }
0073 return fConnection->CreateStatement()->ExecuteQuery(sql);
0074 }
0075
0076
0077 Int_t TRDBCServer::SelectDataBase(const char* dbname)
0078 {
0079
0080
0081
0082
0083
0084 if(!IsConnected()) return 0;
0085
0086
0087 TSQLStatement* stmt = fConnection->CreateStatement();
0088 TString query = "USE ";
0089 query += dbname;
0090
0091 if(!stmt->Execute(query)) return 0;
0092 delete stmt;
0093 return 1;
0094 }
0095
0096
0097 TSQLResultSet* TRDBCServer::GetDataBases(const char *wild)
0098 {
0099
0100
0101
0102
0103 if (!IsConnected()) {
0104 Error("GetDataBases", "not connected");
0105 return 0;
0106 }
0107
0108 TString query;
0109
0110 if( fDbName == "MySQL" ) {
0111 query= "show databases";
0112 if(wild) { query += " like "; query += wild; }
0113 return Query(query);
0114 } else {
0115 return 0;
0116 }
0117 }
0118
0119
0120 TSQLResultSet* TRDBCServer::GetTables(const char *dbname, const char *wild)
0121 {
0122
0123
0124
0125
0126 if (!IsConnected()) {
0127 Error("GetTables", "not connected");
0128 return 0;
0129 }
0130
0131 TString query;
0132
0133 if( fDbName == "MySQL") {
0134 query = "SHOW TABLES";
0135
0136 if(dbname) {
0137 if(SelectDataBase(dbname) != 0) {
0138 Error("GetTables", "no such database %s", dbname);
0139 return 0;
0140 }
0141 query += " from "; query += dbname;
0142 }
0143
0144 if(wild) { query += " like "; query += wild; }
0145 return Query(query);
0146 } else {
0147 return 0;
0148 }
0149 }
0150
0151
0152 TSQLResultSet* TRDBCServer::GetColumns( const char *dbname, const char *table,
0153 const char *wild )
0154 {
0155
0156
0157
0158
0159
0160 if (!IsConnected()) {
0161 Error("GetColumns", "not connected");
0162 return 0;
0163 }
0164
0165 TString query;
0166
0167 if ( dbname && (SelectDataBase(dbname) != 0) ) {
0168 Error("GetColumns", "no such database %s", dbname);
0169 return 0;
0170 }
0171
0172 if(fDbName == "MySQL") {
0173 query = "SHOW COLUMNS FROM ";
0174 query += table;
0175
0176 if (wild) { query += " LIKE "; query += wild; }
0177 return Query(query);
0178 } else {
0179 return 0;
0180 }
0181 }
0182
0183
0184 Int_t TRDBCServer::CreateDataBase(const char *dbname)
0185 {
0186
0187
0188 if (!IsConnected()) {
0189 Error("CreateDataBase", "not connected");
0190 return kFALSE;
0191 }
0192
0193 Bool_t res;
0194 TString query;
0195 TSQLStatement* stmt;
0196
0197 query = "CREATE DATABASE ";
0198 query += dbname;
0199
0200 stmt = fConnection->CreateStatement();
0201 res = stmt->Execute(query);
0202 delete stmt;
0203 return res;
0204 }
0205
0206
0207 Int_t TRDBCServer::DropDataBase(const char *dbname)
0208 {
0209
0210
0211 if (!IsConnected()) {
0212 Error("DropDataBase", "not connected");
0213 return 0;
0214 }
0215
0216 Bool_t res;
0217 TString query;
0218 TSQLStatement* stmt;
0219
0220 query = "DROP DATABASE ";
0221 query += dbname;
0222
0223 stmt = fConnection->CreateStatement();
0224 res = stmt->Execute(query);
0225 delete stmt;
0226 return res;
0227 }
0228
0229
0230 Int_t TRDBCServer::Reload()
0231 {
0232
0233
0234
0235 if (!IsConnected()) {
0236 Error("Reload", "not connected");
0237 return kFALSE;
0238 }
0239
0240 Bool_t res;
0241 TSQLStatement* stmt;
0242
0243 if(fDbName == "MySQL") {
0244 stmt = fConnection->CreateStatement();
0245 res = stmt->Execute("FLUSH PRIVILEGES");
0246 delete stmt;
0247 return res;
0248 } else {
0249 return kFALSE;
0250 }
0251 }
0252
0253
0254 Int_t TRDBCServer::Shutdown()
0255 {
0256
0257
0258
0259 if (!IsConnected()) {
0260 Error("Shutdown", "not connected");
0261 return kFALSE;
0262 }
0263 return kFALSE;
0264 }
0265
0266
0267 const char* TRDBCServer::ServerInfo()
0268 {
0269
0270
0271
0272
0273
0274
0275 if (!IsConnected()) {
0276 Error("ServerInfo", "not connected");
0277 return 0;
0278 }
0279
0280 static TString str;
0281 TSQLResultSet* rs;
0282
0283 if(fDbName == "MySQL") {
0284
0285 rs = Query("SELECT VERSION()");
0286 if(!rs) return str.Data();
0287
0288 if(!rs->Next()) return str.Data();
0289 str = rs->GetString(1);
0290 delete rs;
0291 return str.Data();
0292 } else {
0293 return str.Data();
0294 }
0295 }
0296
0297