Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:06

0001 // $Id: TRDBCServer.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
0002 //*-- Author : Valeriy Onuchin 14/02/2001
0003 //
0004 
0005 /////////////////////////////////////////////////////////////////////
0006 //
0007 //  TRDBCServer is implementation of TSQLServer with RDBC 
0008 //
0009 //  This class is experimental and not completed yet, 
0010 //  as much as possible use  TSQLDriverManager instead.
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    // Open a connection to a  DB server. The db arguments should be
0027    // of the form 
0028    // "protocol:[subprotocol]:[driver]://<host>[:<port>][/<database>]", e.g.:
0029    // "mysql:odbc:myodbc//pcroot.cern.ch:3456/test". 
0030    // The uid is the username and pw
0031    // the password that should be used for the connection.
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    // Close connection to DB server
0048    
0049    if (IsConnected()) Close();
0050 }
0051 
0052 //______________________________________________________________________________
0053 void TRDBCServer::Close(Option_t *)
0054 {
0055    // Close connection to DB server.
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    // Execute SQL command.
0066    //
0067    // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
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    // Select/Change a database. Returns kTRUE if successful, 
0080    // kFALSE otherwise.
0081    //
0082    // Corresponds to MySQL statement "USE database"
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    // List all available databases. Wild is for wildcarding "t%" list all
0100    // databases starting with "t".
0101    // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
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    // List all tables in the specified database. Wild is for wildcarding 
0123    // "t%" list all tables starting with "t".
0124    // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
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    // List all columns in specified table in the specified database.
0156    // Wild is for wildcarding "t%" list all columns starting with "t".
0157    // Returns a pointer to a TSQLResultSet object if successful, 0 otherwise.
0158    // The result object must be deleted by the user.
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    // Create a database. Returns kTRUE if successful, kFALSE otherwise.
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 ";   // standart SQL
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    // Drop (i.e. delete) a database. Returns kTRUE if successful, kFALSE otherwise.
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 ";  // standart SQL
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    // Reload permission tables. Returns kTRUE if successful, kFALSE
0233    // otherwise. User must have reload permissions.
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    // Shutdown the database server. Returns kTRUE if successful, kFALSE
0257    // otherwise. User must have shutdown permissions.
0258 
0259    if (!IsConnected()) {
0260       Error("Shutdown", "not connected");
0261       return kFALSE;
0262    }
0263    return kFALSE; // not implemented
0264 }
0265 
0266 //______________________________________________________________________________
0267 const char* TRDBCServer::ServerInfo()
0268 {
0269    // Returns a string that represents the server version number.
0270    //
0271    // Corresponds to:
0272    //
0273    //     mysql> SELECT VERSION();
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