Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TSQLFile.h
Go to the documentation of this file.
1 // @(#)root/sql:$Id$
2 // Author: Sergey Linev 20/11/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, 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_TSQLFile
13 #define ROOT_TSQLFile
14 
15 #include "TFile.h"
16 #include <stdlib.h>
17 
18 class TList;
19 class TStreamerElement;
20 class TVirtualStreamerInfo;
21 
22 class TSQLServer;
23 class TSQLStatement;
24 class TSQLResult;
25 class TSQLRow;
26 class TKeySQL;
27 class TBufferSQL2;
28 class TSQLClassInfo;
29 
30 class TSQLFile final : public TFile {
31 
32  friend class TBufferSQL2;
33  friend class TKeySQL;
34  friend class TSQLStructure;
35  friend class TSQLTableData;
36  friend class TSqlRegistry;
37  friend class TSqlRawBuffer;
38  friend class TSqlCmdsBuffer;
39 
40 protected:
41  enum ELockingKinds { kLockFree = 0, kLockBusy = 1 };
42 
43  // Interface to basic system I/O routines, suppressed
44  Int_t SysOpen(const char *, Int_t, UInt_t) final { return 0; }
45  Int_t SysClose(Int_t) final { return 0; }
46  Int_t SysRead(Int_t, void *, Int_t) final { return 0; }
47  Int_t SysWrite(Int_t, const void *, Int_t) final { return 0; }
48  Long64_t SysSeek(Int_t, Long64_t, Int_t) final { return 0; }
49  Int_t SysStat(Int_t, Long_t *, Long64_t *, Long_t *, Long_t *) final { return 0; }
50  Int_t SysSync(Int_t) final { return 0; }
51 
52  // Overwrite methods for directory I/O
53  Long64_t DirCreateEntry(TDirectory *) final;
54  Int_t DirReadKeys(TDirectory *) final;
55  void DirWriteKeys(TDirectory *) final;
56  void DirWriteHeader(TDirectory *) final;
57 
58  InfoListRet GetStreamerInfoListImpl(bool) final;
59 
60  // functions to manipulate basic tables (Configurations, Objects, Keys) in database
61  void SaveToDatabase();
62  Bool_t ReadConfigurations();
63  Bool_t IsTablesExists();
64  void InitSqlDatabase(Bool_t create);
65  void CreateBasicTables();
66  void IncrementModifyCounter();
67  void SetLocking(Int_t mode);
68  Int_t GetLocking();
69 
70  // function for read/write access infos
71  Bool_t IsWriteAccess();
72  Bool_t IsReadAccess();
73 
74  // generic sql functions
75  TSQLResult *SQLQuery(const char *cmd, Int_t flag = 0, Bool_t *res = 0);
76  Bool_t SQLCanStatement();
77  TSQLStatement *SQLStatement(const char *cmd, Int_t bufsize = 1000);
78  void SQLDeleteStatement(TSQLStatement *stmt);
79  Bool_t SQLApplyCommands(TObjArray *cmds);
80  Bool_t SQLTestTable(const char *tablename);
81  Long64_t SQLMaximumValue(const char *tablename, const char *columnname);
82  void SQLDeleteAllTables();
83  Bool_t SQLStartTransaction();
84  Bool_t SQLCommit();
85  Bool_t SQLRollback();
86  Int_t SQLMaxIdentifierLength();
87 
88  // operation with keys structures in database
89  void DeleteKeyFromDB(Long64_t keyid);
90  Bool_t WriteKeyData(TKeySQL *key);
91  Bool_t UpdateKeyData(TKeySQL *key);
92  TKeySQL *FindSQLKey(TDirectory *dir, Long64_t keyid);
93  Long64_t DefineNextKeyId();
94  Int_t StreamKeysForDirectory(TDirectory *dir, Bool_t doupdate, Long64_t specialkeyid = -1, TKeySQL **specialkey = 0);
95 
96  // handling SQL class info structures
97  TSQLClassInfo *FindSQLClassInfo(const char *clname, Int_t version);
98  TSQLClassInfo *FindSQLClassInfo(const TClass *cl);
99  TSQLClassInfo *RequestSQLClassInfo(const char *clname, Int_t version);
100  TSQLClassInfo *RequestSQLClassInfo(const TClass *cl);
101  Bool_t CreateClassTable(TSQLClassInfo *sqlinfo, TObjArray *colinfos);
102  Bool_t CreateRawTable(TSQLClassInfo *sqlinfo);
103 
104  Bool_t ProduceClassSelectQuery(TVirtualStreamerInfo *info, TSQLClassInfo *sqlinfo, TString &columns, TString &tables,
105  Int_t &tablecnt);
106  void AddIdEntry(Long64_t tableid, Int_t subid, Int_t type, const char *name, const char *sqlname, const char *info);
107  void ReadSQLClassInfos();
108  TString DefineTableName(const char *clname, Int_t version, Bool_t rawtable);
109  Bool_t HasTable(const char *name);
110 
111  // operations with long string table
112  TString CodeLongString(Long64_t objid, Int_t strid);
113  Int_t IsLongStringCode(Long64_t objid, const char *value);
114  Bool_t VerifyLongStringTable();
115  Bool_t GetLongString(Long64_t objid, Int_t strid, TString &value);
116 
117  // operation with object tables in database
118  Long64_t VerifyObjectTable();
119  Bool_t SQLObjectInfo(Long64_t objid, TString &clname, Version_t &version);
120  TObjArray *SQLObjectsInfo(Long64_t keyid);
121  TSQLResult *GetNormalClassData(Long64_t objid, TSQLClassInfo *sqlinfo);
122  TSQLResult *GetNormalClassDataAll(Long64_t minobjid, Long64_t maxobjid, TSQLClassInfo *sqlinfo);
123  TSQLResult *GetBlobClassData(Long64_t objid, TSQLClassInfo *sqlinfo);
124  TSQLStatement *GetBlobClassDataStmt(Long64_t objid, TSQLClassInfo *sqlinfo);
125  Long64_t StoreObjectInTables(Long64_t keyid, const void *obj, const TClass *cl);
126  Bool_t WriteSpecialObject(Long64_t keyid, TObject *obj, const char *name, const char *title);
127  TObject *ReadSpecialObject(Long64_t keyid, TObject *obj = 0);
128 
129  // sql specific types
130  const char *SQLCompatibleType(Int_t typ) const;
131  const char *SQLIntType() const;
132  const char *SQLSmallTextType() const { return fOtherTypes[0]; }
133  Int_t SQLSmallTextTypeLimit() const { return atoi(fOtherTypes[1]); }
134  const char *SQLBigTextType() const { return fOtherTypes[2]; }
135  const char *SQLDatetimeType() const { return fOtherTypes[3]; }
136  const char *SQLIdentifierQuote() const { return fOtherTypes[4]; }
137  const char *SQLDirIdColumn() const { return fOtherTypes[5]; }
138  const char *SQLKeyIdColumn() const { return fOtherTypes[6]; }
139  const char *SQLObjectIdColumn() const { return fOtherTypes[7]; }
140  const char *SQLRawIdColumn() const { return fOtherTypes[8]; }
141  const char *SQLStrIdColumn() const { return fOtherTypes[9]; }
142  const char *SQLNameSeparator() const { return fOtherTypes[10]; }
143  const char *SQLValueQuote() const { return fOtherTypes[11]; }
144  const char *SQLDefaultTableType() const { return fOtherTypes[12]; }
145 
146  TSQLServer *fSQL; ///<! interface to SQL database
147 
148  TList *fSQLClassInfos; ///<! list of SQL class infos
149 
150  Bool_t fUseSuffixes; ///<! use suffixes in column names like fValue:Int_t or fObject:pointer
151  Int_t fSQLIOversion; ///<! version of SQL I/O which is stored in configurations
152  Int_t fArrayLimit; ///<! limit for array size. when array bigger, its content converted to raw format
153  Bool_t fCanChangeConfig; ///<! variable indicates can be basic configuration changed or not
154  TString fTablesType; ///<! type, used in CREATE TABLE statements
155  Int_t fUseTransactions; ///<! use transaction statements for writing data into the tables
156  Int_t fUseIndexes; ///<! use indexes for tables: 0 - off, 1 - only for basic tables, 2 + normal class tables, 3 - all tables
157  Int_t fModifyCounter; ///<! indicates how many changes was done with database tables
158  Int_t fQuerisCounter; ///<! how many query was applied
159 
160  const char **fBasicTypes; ///<! pointer on list of basic types specific for currently connected SQL server
161  const char **fOtherTypes; ///<! pointer on list of other SQL types like TEXT or blob
162 
163  TString fUserName; ///<! user name, used to access objects from database
164 
165  std::ofstream *fLogFile; ///<! log file with SQL statements
166 
167  Bool_t fIdsTableExists; ///<! indicate if IdsTable exists
168  Int_t fStmtCounter; ///<! count numbers of active statements
169 
170 private:
171  TSQLFile(const TSQLFile &) = delete; //Files cannot be copied - not implemented
172  void operator=(const TSQLFile &) = delete; //Files cannot be copied - not implemented
173 
174 public:
175  enum ETransactionKinds { kTransactionsOff = 0, kTransactionsAuto = 1, kTransactionsUser = 2 };
176 
177  enum EIndexesKinds { kIndexesNone = 0, kIndexesBasic = 1, kIndexesClass = 2, kIndexesAll = 3 };
178 
179  TSQLFile();
180  TSQLFile(const char *dbname, Option_t *option = "read", const char *user = "user", const char *pass = "pass");
181  virtual ~TSQLFile();
182 
183  // configuration of SQL
184  Bool_t GetUseSuffixes() const { return fUseSuffixes; }
185  void SetUseSuffixes(Bool_t on = kTRUE);
186  Int_t GetArrayLimit() const { return fArrayLimit; }
187  void SetArrayLimit(Int_t limit = 20);
188  void SkipArrayLimit() { SetArrayLimit(-1); }
189  void SetTablesType(const char *table_type);
190  const char *GetTablesType() const { return fTablesType.Data(); }
191  void SetUseTransactions(Int_t mode = kTransactionsAuto);
192  Int_t GetUseTransactions() const { return fUseTransactions; }
193  void SetUseIndexes(Int_t use_type = kIndexesBasic);
194  Int_t GetUseIndexes() const { return fUseIndexes; }
195  Int_t GetQuerisCounter() const { return fQuerisCounter; }
196  Int_t GetIOVersion() const { return fSQLIOversion; }
197 
198  TString MakeSelectQuery(TClass *cl);
199  Bool_t StartTransaction();
200  Bool_t Commit();
201  Bool_t Rollback();
202 
203  // log file for SQL statements
204  void StartLogFile(const char *fname); // *MENU*
205  void StopLogFile(); // *MENU*
206 
207  void Close(Option_t *option = "") final; // *MENU*
208  TKey *CreateKey(TDirectory *mother, const TObject *obj, const char *name, Int_t bufsize) final;
209  TKey *CreateKey(TDirectory *mother, const void *obj, const TClass *cl, const char *name, Int_t bufsize) final;
210  void DrawMap(const char * = "*", Option_t * = "") final {}
211  void FillBuffer(char *&) final {}
212  void Flush() final {}
213 
214  Long64_t GetEND() const final { return 0; }
215  Int_t GetErrno() const final { return 0; }
216  void ResetErrno() const final {}
217 
218  const char *GetDataBaseName() const;
219  Int_t GetNfree() const final { return 0; }
220  Int_t GetNbytesInfo() const final{ return 0; }
221  Int_t GetNbytesFree() const final { return 0; }
222  Long64_t GetSeekFree() const final { return 0; }
223  Long64_t GetSeekInfo() const final { return 0; }
224  Long64_t GetSize() const final { return 0; }
225 
226  Bool_t IsOpen() const final;
227  Bool_t IsMySQL() const;
228  Bool_t IsOracle() const;
229  Bool_t IsODBC() const;
230 
231  void MakeFree(Long64_t, Long64_t) final {}
232  void MakeProject(const char *, const char * = "*", Option_t * = "new") final {} // *MENU*
233  void Map(Option_t *) final {} //
234  void Map() final {} //
235  void Paint(Option_t * = "") final {}
236  void Print(Option_t * = "") const final {}
237  Bool_t ReadBuffer(char *, Int_t) final { return kFALSE; }
238  Bool_t ReadBuffer(char *, Long64_t, Int_t) final { return kFALSE; }
239  void ReadFree() final {}
240  Int_t Recover() final { return 0; }
241  Int_t ReOpen(Option_t *mode) final;
242  void Seek(Long64_t, ERelativeTo = kBeg) final {}
243 
244  void SetEND(Long64_t) final {}
245  Int_t Sizeof() const final { return 0; }
246 
247  Bool_t WriteBuffer(const char *, Int_t) final { return kFALSE; }
248  Int_t Write(const char * = nullptr, Int_t = 0, Int_t = 0) final { return 0; }
249  Int_t Write(const char * = nullptr, Int_t = 0, Int_t = 0) const final { return 0; }
250  void WriteFree() final {}
251  void WriteHeader() final;
252  void WriteStreamerInfo() final;
253 
254  ClassDefOverride(TSQLFile, 1) // ROOT TFile interface to SQL database
255 };
256 
257 #endif