Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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; // port to which we are connected
48  Int_t fErrorCode; // error code of last operation
49  TString fErrorMsg; // error message of last operation
50  Bool_t fErrorOut; // enable error output
51 
52  TSQLServer()
53  : fType(), fHost(), fDB(), fPort(-1), fErrorCode(0),
54  fErrorMsg(), fErrorOut(kTRUE) { ClearError(); }
55 
56  void ClearError();
57  void SetError(Int_t code, const char* msg, const char* method = 0);
58 
59  static const char* fgFloatFmt; //! printf argument for floats and doubles, either "%f" or "%e" or "%10f" and so on
60 
61 public:
62  enum ESQLDataTypes { // data types, recognised by TSQLServer and other classes, extrction from ODBC
63  kSQL_NONE = -1, // data type unknown
64  kSQL_CHAR = 1, // CHAR(n) - string with fixed length n
65  kSQL_VARCHAR = 2, // VARCHAR(n) - string with variable length upto n
66  kSQL_INTEGER = 3, // INTEGER, INT - integer value
67  kSQL_FLOAT = 4, // FLOAT - float value
68  kSQL_DOUBLE = 5, // DOUBLE - double value
69  kSQL_NUMERIC = 6, // NUMERIC - numeric values with length and precion
70  kSQL_BINARY = 7, // BLOB - binary data
71  kSQL_TIMESTAMP = 8 // TIMESTAMP -
72  };
73 
74  virtual ~TSQLServer() { }
75 
76  virtual void Close(Option_t *option="") = 0;
77  virtual TSQLResult *Query(const char *sql) = 0;
78  virtual Bool_t Exec(const char* sql);
79  virtual TSQLStatement *Statement(const char*, Int_t = 100)
80  { AbstractMethod("Statement"); return 0; }
81  virtual Bool_t HasStatement() const { return kFALSE; }
82  virtual Int_t SelectDataBase(const char *dbname) = 0;
83  virtual TSQLResult *GetDataBases(const char *wild = 0) = 0;
84  virtual TSQLResult *GetTables(const char *dbname, const char *wild = 0) = 0;
85  virtual TList *GetTablesList(const char* wild = 0);
86  virtual Bool_t HasTable(const char* tablename);
87  virtual TSQLTableInfo *GetTableInfo(const char* tablename);
88  virtual TSQLResult *GetColumns(const char *dbname, const char *table, const char *wild = 0) = 0;
89  virtual Int_t GetMaxIdentifierLength() { return 20; }
90  virtual Int_t CreateDataBase(const char *dbname) = 0;
91  virtual Int_t DropDataBase(const char *dbname) = 0;
92  virtual Int_t Reload() = 0;
93  virtual Int_t Shutdown() = 0;
94  virtual const char *ServerInfo() = 0;
95  virtual Bool_t IsConnected() const { return fPort == -1 ? kFALSE : kTRUE; }
96  const char *GetDBMS() const { return fType.Data(); }
97  const char *GetDB() const { return fDB.Data(); }
98  const char *GetHost() const { return fHost.Data(); }
99  Int_t GetPort() const { return fPort; }
100 
101  virtual Bool_t IsError() const { return GetErrorCode()!=0; }
102  virtual Int_t GetErrorCode() const;
103  virtual const char* GetErrorMsg() const;
104  virtual void EnableErrorOutput(Bool_t on = kTRUE) { fErrorOut = on; }
105 
106  virtual Bool_t StartTransaction();
107  virtual Bool_t Commit();
108  virtual Bool_t Rollback();
109 
110  virtual Bool_t PingVerify() { return kFALSE; }
111  virtual Int_t Ping() { return -9999; }
112 
113  static TSQLServer *Connect(const char *db, const char *uid, const char *pw);
114 
115  static void SetFloatFormat(const char* fmt = "%e");
116  static const char* GetFloatFormat();
117 
118  ClassDef(TSQLServer,0) // Connection to SQL server
119 };
120 
121 #endif