Logo ROOT  
Reference Guide
TSQLServer.h
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: Fons Rademakers 25/11/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TSQLServer
13 #define ROOT_TSQLServer
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TSQLServer //
19 // //
20 // Abstract base class defining interface to a SQL server. //
21 // //
22 // To open a connection to a server use the static method Connect(). //
23 // The db argument of Connect() is of the form: //
24 // <dbms>://<host>[:<port>][/<database>], e.g. //
25 // mysql://pcroot.cern.ch:3456/test, oracle://srv1.cern.ch/main, ... //
26 // Depending on the <dbms> specified an appropriate plugin library //
27 // will be loaded which will provide the real interface. //
28 // //
29 // Related classes are TSQLStatement, TSQLResult and TSQLRow. //
30 // //
31 //////////////////////////////////////////////////////////////////////////
32 
33 #include "TObject.h"
34 #include "TString.h"
35 
36 class TSQLResult;
37 class TSQLStatement;
38 class TSQLTableInfo;
39 class TList;
40 
41 class TSQLServer : public TObject {
42 
43 protected:
44  TString fType; // type of DBMS (MySQL, Oracle, SysBase, ...)
45  TString fHost; // host to which we are connected
46  TString fDB; // currently selected DB
47  Int_t fPort{-1}; // port to which we are connected
48  Int_t fErrorCode{0}; // error code of last operation
49  TString fErrorMsg; // error message of last operation
50  Bool_t fErrorOut{kTRUE}; // enable error output
51 
52  TSQLServer() {} // not allowed to use =default for TObject-derived classes
53 
54  void ClearError();
55  void SetError(Int_t code, const char* msg, const char *method = nullptr);
56 
57  static const char* fgFloatFmt; //! printf argument for floats and doubles, either "%f" or "%e" or "%10f" and so on
58 
59 public:
60  enum ESQLDataTypes { // data types, recognised by TSQLServer and other classes, extrction from ODBC
61  kSQL_NONE = -1, // data type unknown
62  kSQL_CHAR = 1, // CHAR(n) - string with fixed length n
63  kSQL_VARCHAR = 2, // VARCHAR(n) - string with variable length upto n
64  kSQL_INTEGER = 3, // INTEGER, INT - integer value
65  kSQL_FLOAT = 4, // FLOAT - float value
66  kSQL_DOUBLE = 5, // DOUBLE - double value
67  kSQL_NUMERIC = 6, // NUMERIC - numeric values with length and precion
68  kSQL_BINARY = 7, // BLOB - binary data
69  kSQL_TIMESTAMP = 8 // TIMESTAMP -
70  };
71 
72  virtual ~TSQLServer() { }
73 
74  virtual void Close(Option_t *option = "") = 0;
75  virtual TSQLResult *Query(const char *sql) = 0;
76  virtual Bool_t Exec(const char* sql);
77  virtual TSQLStatement *Statement(const char*, Int_t = 100)
78  { AbstractMethod("Statement"); return nullptr; }
79  virtual Bool_t HasStatement() const { return kFALSE; }
80  virtual Int_t SelectDataBase(const char *dbname) = 0;
81  virtual TSQLResult *GetDataBases(const char *wild = nullptr) = 0;
82  virtual TSQLResult *GetTables(const char *dbname, const char *wild = nullptr) = 0;
83  virtual TList *GetTablesList(const char* wild = nullptr);
84  virtual Bool_t HasTable(const char* tablename);
85  virtual TSQLTableInfo *GetTableInfo(const char* tablename);
86  virtual TSQLResult *GetColumns(const char *dbname, const char *table, const char *wild = nullptr) = 0;
87  virtual Int_t GetMaxIdentifierLength() { return 20; }
88  virtual Int_t CreateDataBase(const char *dbname) = 0;
89  virtual Int_t DropDataBase(const char *dbname) = 0;
90  virtual Int_t Reload() = 0;
91  virtual Int_t Shutdown() = 0;
92  virtual const char *ServerInfo() = 0;
93  virtual Bool_t IsConnected() const { return fPort == -1 ? kFALSE : kTRUE; }
94  const char *GetDBMS() const { return fType.Data(); }
95  const char *GetDB() const { return fDB.Data(); }
96  const char *GetHost() const { return fHost.Data(); }
97  Int_t GetPort() const { return fPort; }
98 
99  virtual Bool_t IsError() const { return GetErrorCode()!=0; }
100  virtual Int_t GetErrorCode() const;
101  virtual const char* GetErrorMsg() const;
102  virtual void EnableErrorOutput(Bool_t on = kTRUE) { fErrorOut = on; }
103 
104  virtual Bool_t StartTransaction();
105  virtual Bool_t Commit();
106  virtual Bool_t Rollback();
107 
108  virtual Bool_t PingVerify() { return kFALSE; }
109  virtual Int_t Ping() { return -9999; }
110 
111  static TSQLServer *Connect(const char *db, const char *uid, const char *pw);
112 
113  static void SetFloatFormat(const char* fmt = "%e");
114  static const char* GetFloatFormat();
115 
116  ClassDef(TSQLServer,0) // Connection to SQL server
117 };
118 
119 #endif
TSQLServer::GetDBMS
const char * GetDBMS() const
Definition: TSQLServer.h:94
TSQLServer::fHost
TString fHost
Definition: TSQLServer.h:45
TSQLServer::GetMaxIdentifierLength
virtual Int_t GetMaxIdentifierLength()
Definition: TSQLServer.h:87
TSQLServer::kSQL_TIMESTAMP
@ kSQL_TIMESTAMP
Definition: TSQLServer.h:69
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TSQLServer::TSQLServer
TSQLServer()
Definition: TSQLServer.h:52
TSQLServer::GetDataBases
virtual TSQLResult * GetDataBases(const char *wild=nullptr)=0
TSQLServer::kSQL_BINARY
@ kSQL_BINARY
Definition: TSQLServer.h:68
TSQLServer::ServerInfo
virtual const char * ServerInfo()=0
TString::Data
const char * Data() const
Definition: TString.h:369
TSQLServer::GetPort
Int_t GetPort() const
Definition: TSQLServer.h:97
TSQLServer::fPort
Int_t fPort
Definition: TSQLServer.h:47
TSQLServer::GetColumns
virtual TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr)=0
TSQLServer::kSQL_VARCHAR
@ kSQL_VARCHAR
Definition: TSQLServer.h:63
TSQLServer::kSQL_DOUBLE
@ kSQL_DOUBLE
Definition: TSQLServer.h:66
TSQLServer::Shutdown
virtual Int_t Shutdown()=0
TSQLServer::GetTableInfo
virtual TSQLTableInfo * GetTableInfo(const char *tablename)
Produce TSQLTableInfo object, which contain info about table itself and each table column Object must...
Definition: TSQLServer.cxx:236
TSQLServer::SetFloatFormat
static void SetFloatFormat(const char *fmt="%e")
set printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:260
TSQLServer::GetHost
const char * GetHost() const
Definition: TSQLServer.h:96
Int_t
int Int_t
Definition: RtypesCore.h:45
TSQLServer::Commit
virtual Bool_t Commit()
submit "COMMIT" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:150
TSQLServer::kSQL_CHAR
@ kSQL_CHAR
Definition: TSQLServer.h:62
TSQLTableInfo
Definition: TSQLTableInfo.h:20
TSQLServer::EnableErrorOutput
virtual void EnableErrorOutput(Bool_t on=kTRUE)
Definition: TSQLServer.h:102
TSQLServer::Close
virtual void Close(Option_t *option="")=0
TString
Definition: TString.h:136
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TString.h
TSQLServer::fType
TString fType
Definition: TSQLServer.h:44
TSQLServer::HasTable
virtual Bool_t HasTable(const char *tablename)
Tests if table of that name exists in database Return kTRUE, if table exists.
Definition: TSQLServer.cxx:208
bool
TSQLResult
Definition: TSQLResult.h:32
TSQLServer::SetError
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
Definition: TSQLServer.cxx:129
TSQLServer::kSQL_NONE
@ kSQL_NONE
Definition: TSQLServer.h:61
TSQLServer::Connect
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
The db should be of the form: <dbms>://<host>[:<port>][/<database>], e.g.
Definition: TSQLServer.cxx:61
TSQLServer::IsConnected
virtual Bool_t IsConnected() const
Definition: TSQLServer.h:93
TSQLServer::HasStatement
virtual Bool_t HasStatement() const
Definition: TSQLServer.h:79
TSQLServer::kSQL_INTEGER
@ kSQL_INTEGER
Definition: TSQLServer.h:64
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TSQLServer::Query
virtual TSQLResult * Query(const char *sql)=0
TSQLServer::Ping
virtual Int_t Ping()
Definition: TSQLServer.h:109
TSQLServer::fDB
TString fDB
Definition: TSQLServer.h:46
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSQLServer::CreateDataBase
virtual Int_t CreateDataBase(const char *dbname)=0
TSQLServer::Exec
virtual Bool_t Exec(const char *sql)
Execute sql query.
Definition: TSQLServer.cxx:85
TSQLServer::kSQL_FLOAT
@ kSQL_FLOAT
Definition: TSQLServer.h:65
TSQLServer::~TSQLServer
virtual ~TSQLServer()
Definition: TSQLServer.h:72
TSQLServer::fgFloatFmt
static const char * fgFloatFmt
Definition: TSQLServer.h:57
TObject::AbstractMethod
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract.
Definition: TObject.cxx:932
TSQLServer::fErrorOut
Bool_t fErrorOut
Definition: TSQLServer.h:50
TSQLServer::kSQL_NUMERIC
@ kSQL_NUMERIC
Definition: TSQLServer.h:67
TSQLServer::GetErrorCode
virtual Int_t GetErrorCode() const
returns error code of last operation if res==0, no error Each specific implementation of TSQLServer p...
Definition: TSQLServer.cxx:101
TSQLServer::Statement
virtual TSQLStatement * Statement(const char *, Int_t=100)
Definition: TSQLServer.h:77
TSQLServer::PingVerify
virtual Bool_t PingVerify()
Definition: TSQLServer.h:108
TSQLServer::ClearError
void ClearError()
reset error fields
Definition: TSQLServer.cxx:119
TSQLServer::GetFloatFormat
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
Definition: TSQLServer.cxx:269
TObject.h
TSQLServer::ESQLDataTypes
ESQLDataTypes
printf argument for floats and doubles, either "%f" or "%e" or "%10f" and so on
Definition: TSQLServer.h:60
TSQLStatement
Definition: TSQLStatement.h:21
TObject
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TSQLServer::Rollback
virtual Bool_t Rollback()
submit "ROLLBACK" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:159
TSQLServer::StartTransaction
virtual Bool_t StartTransaction()
submit "START TRANSACTION" query to database return kTRUE, if successful
Definition: TSQLServer.cxx:141
TSQLServer::SelectDataBase
virtual Int_t SelectDataBase(const char *dbname)=0
TSQLServer::Reload
virtual Int_t Reload()=0
TSQLServer::fErrorMsg
TString fErrorMsg
Definition: TSQLServer.h:49
TSQLServer::GetErrorMsg
virtual const char * GetErrorMsg() const
returns error message of last operation if no errors, return 0 Each specific implementation of TSQLSe...
Definition: TSQLServer.cxx:111
TSQLServer::GetDB
const char * GetDB() const
Definition: TSQLServer.h:95
TSQLServer::GetTables
virtual TSQLResult * GetTables(const char *dbname, const char *wild=nullptr)=0
TSQLServer::DropDataBase
virtual Int_t DropDataBase(const char *dbname)=0
TSQLServer::GetTablesList
virtual TList * GetTablesList(const char *wild=nullptr)
Return list of user tables Parameter wild specifies wildcard for table names.
Definition: TSQLServer.cxx:182
TSQLServer::IsError
virtual Bool_t IsError() const
Definition: TSQLServer.h:99
TSQLServer
Definition: TSQLServer.h:41
TList
Definition: TList.h:44
int
TSQLServer::fErrorCode
Int_t fErrorCode
Definition: TSQLServer.h:48