ROOT logo
// @(#)root/net:$Id: TSQLServer.cxx 29321 2009-07-03 10:42:10Z brun $
// Author: Fons Rademakers   25/11/99

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSQLServer                                                           //
//                                                                      //
// Abstract base class defining interface to a SQL server.              //
//                                                                      //
// To open a connection to a server use the static method Connect().    //
// The db argument of Connect() is of the form:                         //
//    <dbms>://<host>[:<port>][/<database>], e.g.                       //
// mysql://pcroot.cern.ch:3456/test, oracle://srv1.cern.ch/main, ...    //
// Depending on the <dbms> specified an appropriate plugin library      //
// will be loaded which will provide the real interface.                //
//                                                                      //
// Related classes are TSQLResult and TSQLRow.                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TSQLServer.h"
#include "TSQLResult.h"
#include "TSQLRow.h"
#include "TSQLTableInfo.h"
#include "TSQLColumnInfo.h"
#include "TROOT.h"
#include "TList.h"
#include "TObjString.h"
#include "TPluginManager.h"
#include "TVirtualMutex.h"

ClassImp(TSQLServer)


const char* TSQLServer::fgFloatFmt = "%e";


//______________________________________________________________________________
TSQLServer *TSQLServer::Connect(const char *db, const char *uid, const char *pw)
{
   // The db should be of the form:  <dbms>://<host>[:<port>][/<database>],
   // e.g.:  mysql://pcroot.cern.ch:3456/test, oracle://srv1.cern.ch/main,
   // pgsql://... or sapdb://...
   // The uid is the username and pw the password that should be used for
   // the connection. Depending on the <dbms> the shared library (plugin)
   // for the selected system will be loaded. When the connection could not
   // be opened 0 is returned.

   TPluginHandler *h;
   TSQLServer *serv = 0;

   if ((h = gROOT->GetPluginManager()->FindHandler("TSQLServer", db))) {
      if (h->LoadPlugin() == -1)
         return 0;
      serv = (TSQLServer *) h->ExecPlugin(3, db, uid, pw);
   }

   if (serv && serv->IsZombie()) {
      delete serv;
      serv = 0;
   }

   return serv;
}

//______________________________________________________________________________
Bool_t TSQLServer::Exec(const char* sql)
{
   // Execute sql query. 
   // Usefull for commands like DROP TABLE or INSERT, where result set
   // is not interested. Return kTRUE if no error
   
   TSQLResult* res = Query(sql);
   if (res==0) return kFALSE;
   
   delete res;
   
   return !IsError();
}


//______________________________________________________________________________
Int_t TSQLServer::GetErrorCode() const
{
   // returns error code of last operation
   // if res==0, no error
   // Each specific implementation of TSQLServer provides its own error coding
   
   return fErrorCode;
}

//______________________________________________________________________________
const char* TSQLServer::GetErrorMsg() const
{
   // returns error message of last operation
   // if no errors, return 0
   // Each specific implementation of TSQLServer provides its own error messages
   
   return GetErrorCode()==0 ? 0 : fErrorMsg.Data();
}

//______________________________________________________________________________
void TSQLServer::ClearError()
{
   // reset error fields
   
   fErrorCode = 0;
   fErrorMsg = "";
}

//______________________________________________________________________________
void TSQLServer::SetError(Int_t code, const char* msg, const char* method)
{
   // set new values for error fields
   // if method is specified, displays error message
   
   fErrorCode = code;
   fErrorMsg = msg;
   if ((method!=0) && fErrorOut)
      Error(method,"Code: %d  Msg: %s", code, (msg ? msg : "No message"));
}

//______________________________________________________________________________
Bool_t TSQLServer::StartTransaction()
{
   // submit "START TRANSACTION" query to database
   // return kTRUE, if succesfull
   
   return Exec("START TRANSACTION");
}

//______________________________________________________________________________
Bool_t TSQLServer::Commit()
{
   // submit "COMMIT" query to database
   // return kTRUE, if succesfull

   return Exec("COMMIT");
}

//______________________________________________________________________________
Bool_t TSQLServer::Rollback()
{
   // submit "ROLLBACK" query to database
   // return kTRUE, if succesfull

   return Exec("ROLLBACK");
}

//______________________________________________________________________________
TList* TSQLServer::GetTablesList(const char* wild)
{
   // Return list of user tables
   // Parameter wild specifies wildcard for table names.
   // It either contains exact table name to verify that table is exists or
   // wildcard with "%" (any number of symbols) and "_" (exactly one symbol).
   // Example of vaild wildcards: "%", "%name","___user__".
   // If wild=="", list of all available tables will be produced.
   // List contain just tables names in the TObjString. 
   // List must be deleted by the user.
   // Example code of method usage:
   //
   // TList* lst = serv->GetTablesList();
   // TIter next(lst);
   // TObject* obj;
   // while (obj = next()) 
   //   cout << "Table: " << obj->GetName() << endl; 
   // delete lst;
   
   TSQLResult* res = GetTables(fDB.Data(), wild);
   if (res==0) return 0;

   TList* lst = 0;
   TSQLRow* row = 0;
   while ((row = res->Next())!=0) {
      const char* tablename = row->GetField(0);
      if (lst==0) {
         lst = new TList;
         lst->SetOwner(kTRUE);  
      }
      lst->Add(new TObjString(tablename));
      delete row;
   }
   
   delete res;
   
   return lst;
}

//______________________________________________________________________________
Bool_t TSQLServer::HasTable(const char* tablename)
{
   // Tests if table of that name exists in database
   // Return kTRUE, if table exists
   
   if ((tablename==0) || (strlen(tablename)==0)) return kFALSE;
   
   TList* lst = GetTablesList(tablename);
   if (lst==0) return kFALSE;
   
   Bool_t res = kFALSE;
   
   TObject* obj = 0;
   TIter iter(lst);
   
   // Can be, that tablename contains "_" or "%" symbols, which are wildcards in SQL,
   // therefore more than one table can be returned as result. 
   // One should check that exactly same name is appears
   
   while ((obj = iter()) != 0) 
      if (strcmp(tablename, obj->GetName())==0) res = kTRUE;
   
   delete lst;
   return res;
}

//______________________________________________________________________________
TSQLTableInfo* TSQLServer::GetTableInfo(const char* tablename)
{
   // Producec TSQLTableInfo object, which contain info about 
   // table itself and each table column
   // Object must be deleted by user.
   
   if ((tablename==0) || (*tablename==0)) return 0;
   
   TSQLResult* res = GetColumns(fDB.Data(), tablename);
   if (res==0) return 0;

   TList* lst = 0;
   TSQLRow* row = 0;
   while ((row = res->Next())!=0) {
      const char* columnname = row->GetField(0);
      if (lst==0) lst = new TList;
      lst->Add(new TSQLColumnInfo(columnname));
      delete row;
   }
   
   delete res;
   
   return new TSQLTableInfo(tablename, lst);
}

//______________________________________________________________________________
void TSQLServer::SetFloatFormat(const char* fmt)
{
   // set printf format for float/double members, default "%e"
   
   if (fmt==0) fmt = "%e";
   fgFloatFmt = fmt;
}
    
//______________________________________________________________________________
const char* TSQLServer::GetFloatFormat()
{
   // return current printf format for float/double members, default "%e"

   return fgFloatFmt;
}
 TSQLServer.cxx:1
 TSQLServer.cxx:2
 TSQLServer.cxx:3
 TSQLServer.cxx:4
 TSQLServer.cxx:5
 TSQLServer.cxx:6
 TSQLServer.cxx:7
 TSQLServer.cxx:8
 TSQLServer.cxx:9
 TSQLServer.cxx:10
 TSQLServer.cxx:11
 TSQLServer.cxx:12
 TSQLServer.cxx:13
 TSQLServer.cxx:14
 TSQLServer.cxx:15
 TSQLServer.cxx:16
 TSQLServer.cxx:17
 TSQLServer.cxx:18
 TSQLServer.cxx:19
 TSQLServer.cxx:20
 TSQLServer.cxx:21
 TSQLServer.cxx:22
 TSQLServer.cxx:23
 TSQLServer.cxx:24
 TSQLServer.cxx:25
 TSQLServer.cxx:26
 TSQLServer.cxx:27
 TSQLServer.cxx:28
 TSQLServer.cxx:29
 TSQLServer.cxx:30
 TSQLServer.cxx:31
 TSQLServer.cxx:32
 TSQLServer.cxx:33
 TSQLServer.cxx:34
 TSQLServer.cxx:35
 TSQLServer.cxx:36
 TSQLServer.cxx:37
 TSQLServer.cxx:38
 TSQLServer.cxx:39
 TSQLServer.cxx:40
 TSQLServer.cxx:41
 TSQLServer.cxx:42
 TSQLServer.cxx:43
 TSQLServer.cxx:44
 TSQLServer.cxx:45
 TSQLServer.cxx:46
 TSQLServer.cxx:47
 TSQLServer.cxx:48
 TSQLServer.cxx:49
 TSQLServer.cxx:50
 TSQLServer.cxx:51
 TSQLServer.cxx:52
 TSQLServer.cxx:53
 TSQLServer.cxx:54
 TSQLServer.cxx:55
 TSQLServer.cxx:56
 TSQLServer.cxx:57
 TSQLServer.cxx:58
 TSQLServer.cxx:59
 TSQLServer.cxx:60
 TSQLServer.cxx:61
 TSQLServer.cxx:62
 TSQLServer.cxx:63
 TSQLServer.cxx:64
 TSQLServer.cxx:65
 TSQLServer.cxx:66
 TSQLServer.cxx:67
 TSQLServer.cxx:68
 TSQLServer.cxx:69
 TSQLServer.cxx:70
 TSQLServer.cxx:71
 TSQLServer.cxx:72
 TSQLServer.cxx:73
 TSQLServer.cxx:74
 TSQLServer.cxx:75
 TSQLServer.cxx:76
 TSQLServer.cxx:77
 TSQLServer.cxx:78
 TSQLServer.cxx:79
 TSQLServer.cxx:80
 TSQLServer.cxx:81
 TSQLServer.cxx:82
 TSQLServer.cxx:83
 TSQLServer.cxx:84
 TSQLServer.cxx:85
 TSQLServer.cxx:86
 TSQLServer.cxx:87
 TSQLServer.cxx:88
 TSQLServer.cxx:89
 TSQLServer.cxx:90
 TSQLServer.cxx:91
 TSQLServer.cxx:92
 TSQLServer.cxx:93
 TSQLServer.cxx:94
 TSQLServer.cxx:95
 TSQLServer.cxx:96
 TSQLServer.cxx:97
 TSQLServer.cxx:98
 TSQLServer.cxx:99
 TSQLServer.cxx:100
 TSQLServer.cxx:101
 TSQLServer.cxx:102
 TSQLServer.cxx:103
 TSQLServer.cxx:104
 TSQLServer.cxx:105
 TSQLServer.cxx:106
 TSQLServer.cxx:107
 TSQLServer.cxx:108
 TSQLServer.cxx:109
 TSQLServer.cxx:110
 TSQLServer.cxx:111
 TSQLServer.cxx:112
 TSQLServer.cxx:113
 TSQLServer.cxx:114
 TSQLServer.cxx:115
 TSQLServer.cxx:116
 TSQLServer.cxx:117
 TSQLServer.cxx:118
 TSQLServer.cxx:119
 TSQLServer.cxx:120
 TSQLServer.cxx:121
 TSQLServer.cxx:122
 TSQLServer.cxx:123
 TSQLServer.cxx:124
 TSQLServer.cxx:125
 TSQLServer.cxx:126
 TSQLServer.cxx:127
 TSQLServer.cxx:128
 TSQLServer.cxx:129
 TSQLServer.cxx:130
 TSQLServer.cxx:131
 TSQLServer.cxx:132
 TSQLServer.cxx:133
 TSQLServer.cxx:134
 TSQLServer.cxx:135
 TSQLServer.cxx:136
 TSQLServer.cxx:137
 TSQLServer.cxx:138
 TSQLServer.cxx:139
 TSQLServer.cxx:140
 TSQLServer.cxx:141
 TSQLServer.cxx:142
 TSQLServer.cxx:143
 TSQLServer.cxx:144
 TSQLServer.cxx:145
 TSQLServer.cxx:146
 TSQLServer.cxx:147
 TSQLServer.cxx:148
 TSQLServer.cxx:149
 TSQLServer.cxx:150
 TSQLServer.cxx:151
 TSQLServer.cxx:152
 TSQLServer.cxx:153
 TSQLServer.cxx:154
 TSQLServer.cxx:155
 TSQLServer.cxx:156
 TSQLServer.cxx:157
 TSQLServer.cxx:158
 TSQLServer.cxx:159
 TSQLServer.cxx:160
 TSQLServer.cxx:161
 TSQLServer.cxx:162
 TSQLServer.cxx:163
 TSQLServer.cxx:164
 TSQLServer.cxx:165
 TSQLServer.cxx:166
 TSQLServer.cxx:167
 TSQLServer.cxx:168
 TSQLServer.cxx:169
 TSQLServer.cxx:170
 TSQLServer.cxx:171
 TSQLServer.cxx:172
 TSQLServer.cxx:173
 TSQLServer.cxx:174
 TSQLServer.cxx:175
 TSQLServer.cxx:176
 TSQLServer.cxx:177
 TSQLServer.cxx:178
 TSQLServer.cxx:179
 TSQLServer.cxx:180
 TSQLServer.cxx:181
 TSQLServer.cxx:182
 TSQLServer.cxx:183
 TSQLServer.cxx:184
 TSQLServer.cxx:185
 TSQLServer.cxx:186
 TSQLServer.cxx:187
 TSQLServer.cxx:188
 TSQLServer.cxx:189
 TSQLServer.cxx:190
 TSQLServer.cxx:191
 TSQLServer.cxx:192
 TSQLServer.cxx:193
 TSQLServer.cxx:194
 TSQLServer.cxx:195
 TSQLServer.cxx:196
 TSQLServer.cxx:197
 TSQLServer.cxx:198
 TSQLServer.cxx:199
 TSQLServer.cxx:200
 TSQLServer.cxx:201
 TSQLServer.cxx:202
 TSQLServer.cxx:203
 TSQLServer.cxx:204
 TSQLServer.cxx:205
 TSQLServer.cxx:206
 TSQLServer.cxx:207
 TSQLServer.cxx:208
 TSQLServer.cxx:209
 TSQLServer.cxx:210
 TSQLServer.cxx:211
 TSQLServer.cxx:212
 TSQLServer.cxx:213
 TSQLServer.cxx:214
 TSQLServer.cxx:215
 TSQLServer.cxx:216
 TSQLServer.cxx:217
 TSQLServer.cxx:218
 TSQLServer.cxx:219
 TSQLServer.cxx:220
 TSQLServer.cxx:221
 TSQLServer.cxx:222
 TSQLServer.cxx:223
 TSQLServer.cxx:224
 TSQLServer.cxx:225
 TSQLServer.cxx:226
 TSQLServer.cxx:227
 TSQLServer.cxx:228
 TSQLServer.cxx:229
 TSQLServer.cxx:230
 TSQLServer.cxx:231
 TSQLServer.cxx:232
 TSQLServer.cxx:233
 TSQLServer.cxx:234
 TSQLServer.cxx:235
 TSQLServer.cxx:236
 TSQLServer.cxx:237
 TSQLServer.cxx:238
 TSQLServer.cxx:239
 TSQLServer.cxx:240
 TSQLServer.cxx:241
 TSQLServer.cxx:242
 TSQLServer.cxx:243
 TSQLServer.cxx:244
 TSQLServer.cxx:245
 TSQLServer.cxx:246
 TSQLServer.cxx:247
 TSQLServer.cxx:248
 TSQLServer.cxx:249
 TSQLServer.cxx:250
 TSQLServer.cxx:251
 TSQLServer.cxx:252
 TSQLServer.cxx:253
 TSQLServer.cxx:254
 TSQLServer.cxx:255
 TSQLServer.cxx:256
 TSQLServer.cxx:257
 TSQLServer.cxx:258
 TSQLServer.cxx:259
 TSQLServer.cxx:260
 TSQLServer.cxx:261
 TSQLServer.cxx:262
 TSQLServer.cxx:263
 TSQLServer.cxx:264
 TSQLServer.cxx:265
 TSQLServer.cxx:266