Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TSQLStructure.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_TSQLStructure
13 #define ROOT_TSQLStructure
14 
15 #include "TObjArray.h"
16 
17 #ifdef Bool
18 #undef Bool
19 #endif
20 #ifdef True
21 #undef True
22 #endif
23 #ifdef False
24 #undef False
25 #endif
26 
27 class TStreamerInfo;
28 class TStreamerInfo;
29 class TStreamerElement;
30 class TSQLFile;
31 class TSqlRegistry;
32 class TSqlRawBuffer;
33 class TSQLObjectData;
34 class TSQLClassInfo;
35 class TBufferSQL2;
36 
37 class TSQLColumnData final : public TObject {
38 
39 protected:
40  TString fName; ///<! name of the table column
41  TString fType; ///<! type of the table column
42  TString fValue; ///<! value of the table column
43  Bool_t fNumeric{kFALSE}; ///<! for numeric quotes (double quotes) are not required
44 public:
45  TSQLColumnData(const char *name, const char *sqltype, const char *value, Bool_t numeric);
46 
47  TSQLColumnData(const char *name, Long64_t value);
48 
49  const char *GetName() const final { return fName.Data(); }
50  const char *GetType() const { return fType.Data(); }
51  const char *GetValue() const { return fValue.Data(); }
52  Bool_t IsNumeric() const { return fNumeric; }
53 
54  ClassDefOverride(TSQLColumnData, 1); // Single SQL column data.
55 };
56 
57 //______________________________________________________________________
58 
59 class TSQLTableData : public TObject {
60 
61 protected:
62  TSQLFile *fFile{nullptr}; ///<!
63  TSQLClassInfo *fInfo{nullptr}; ///<!
64  TObjArray fColumns; ///<! collection of columns
65  TObjArray *fColInfos{nullptr}; ///<! array with TSQLClassColumnInfo, used later for TSQLClassInfo
66 
67  TString DefineSQLName(const char *fullname);
68  Bool_t HasSQLName(const char *sqlname);
69 
70 public:
71  TSQLTableData(TSQLFile *f = nullptr, TSQLClassInfo *info = nullptr);
72  virtual ~TSQLTableData();
73 
74  void AddColumn(const char *name, Long64_t value);
75  void AddColumn(const char *name, const char *sqltype, const char *value, Bool_t numeric);
76 
77  TObjArray *TakeColInfos();
78 
79  Int_t GetNumColumns();
80  const char *GetColumn(Int_t n);
81  Bool_t IsNumeric(Int_t n);
82 
83  ClassDef(TSQLTableData, 1); // Collection of columns data for single SQL table
84 };
85 
86 //______________________________________________________________________
87 
88 class TSQLStructure : public TObject {
89 protected:
90  Bool_t CheckNormalClassPair(TSQLStructure *vers, TSQLStructure *info);
91 
92  Long64_t FindMaxObjectId();
93  void PerformConversion(TSqlRegistry *reg, TSqlRawBuffer *blobs, const char *topname, Bool_t useblob = kFALSE);
94  Bool_t StoreObject(TSqlRegistry *reg, Long64_t objid, TClass *cl, Bool_t registerobj = kTRUE);
95  Bool_t StoreObjectInNormalForm(TSqlRegistry *reg);
96  Bool_t StoreClassInNormalForm(TSqlRegistry *reg);
97  Bool_t StoreElementInNormalForm(TSqlRegistry *reg, TSQLTableData *columns);
98  Bool_t TryConvertObjectArray(TSqlRegistry *reg, TSqlRawBuffer *blobs);
99 
100  Bool_t StoreTObject(TSqlRegistry *reg);
101  Bool_t StoreTString(TSqlRegistry *reg);
102  Bool_t RecognizeTString(const char *&value);
103 
104  TSQLStructure *fParent{nullptr}; //!
105  Int_t fType{0}; //!
106  const void *fPointer{nullptr}; //!
107  TString fValue; //!
108  Int_t fArrayIndex{-1}; //!
109  Int_t fRepeatCnt{0}; //!
110  TObjArray fChilds; //!
111 
112 public:
113  TSQLStructure() {} // NOLINT: not allowed to use = default because of TObject::kIsOnHeap detection, see ROOT-10300
114  virtual ~TSQLStructure();
115 
116  TSQLStructure *GetParent() const { return fParent; }
117  void SetParent(TSQLStructure *p) { fParent = p; }
118  Int_t NumChilds() const;
119  TSQLStructure *GetChild(Int_t n) const;
120 
121  void SetType(Int_t typ) { fType = typ; }
122  Int_t GetType() const { return fType; }
123 
124  // this part requried for writing to SQL tables
125  void SetObjectRef(Long64_t refid, const TClass *cl);
126  void SetObjectPointer(Long64_t ptrid);
127  void SetVersion(const TClass *cl, Int_t version = -100);
128  void SetClassStreamer(const TClass *cl);
129  void SetStreamerInfo(const TStreamerInfo *info);
130  void SetStreamerElement(const TStreamerElement *elem, Int_t number);
131  void SetCustomClass(const TClass *cl, Version_t version);
132  void SetCustomElement(TStreamerElement *elem);
133  void SetValue(const char *value, const char *tname = 0);
134  void SetArrayIndex(Int_t indx, Int_t cnt = 1);
135  void SetArray(Int_t sz = -1);
136  void ChangeValueOnly(const char *value);
137 
138  TClass *GetObjectClass() const;
139  TClass *GetVersionClass() const;
140  TStreamerInfo *GetStreamerInfo() const;
141  TStreamerElement *GetElement() const;
142  Int_t GetElementNumber() const;
143  TClass *GetCustomClass() const;
144  Version_t GetCustomClassVersion() const;
145  Bool_t GetClassInfo(TClass *&cl, Version_t &version);
146  const char *GetValueType() const;
147  const char *GetValue() const;
148  Int_t GetArrayIndex() const { return fArrayIndex; }
149  Int_t GetRepeatCounter() const { return fRepeatCnt; }
150 
151  void Add(TSQLStructure *child);
152  void AddVersion(const TClass *cl, Int_t version = -100);
153  void AddValue(const char *value, const char *tname = 0);
154  void ChildArrayIndex(Int_t index, Int_t cnt = 1);
155 
156  // this is part specially for reading of sql tables
157 
158  Long64_t DefineObjectId(Bool_t recursive = kTRUE);
159 
160  void SetObjectData(TSQLObjectData *objdata);
161  void AddObjectData(TSQLObjectData *objdata);
162  TSQLObjectData *GetObjectData(Bool_t search = false);
163 
164  virtual void Print(Option_t *option = "") const;
165  void PrintLevel(Int_t level) const;
166 
167  Bool_t ConvertToTables(TSQLFile *f, Long64_t keyid, TObjArray *cmds);
168 
169  Int_t LocateElementColumn(TSQLFile *f, TBufferSQL2 *buf, TSQLObjectData *data);
170 
171  static Bool_t UnpackTObject(TSQLFile *f, TBufferSQL2 *buf, TSQLObjectData *data, Long64_t objid, Int_t clversion);
172  static Bool_t UnpackTString(TSQLFile *f, TBufferSQL2 *buf, TSQLObjectData *data, Long64_t objid, Int_t clversion);
173  static Bool_t IsNumericType(Int_t typ);
174  static const char *GetSimpleTypeName(Int_t typ);
175  static TString MakeArrayIndex(TStreamerElement *elem, Int_t n);
176  static Int_t DefineElementColumnType(TStreamerElement *elem, TSQLFile *f);
177  static TString DefineElementColumnName(TStreamerElement *elem, TSQLFile *f, Int_t indx = 0);
178  static void AddStrBrackets(TString &s, const char *quote);
179 
180  enum ESQLTypes {
181  kSqlObject = 10001,
182  kSqlPointer = 10002,
183  kSqlVersion = 10003,
184  kSqlStreamerInfo = 10004,
185  kSqlClassStreamer = 10005,
186  kSqlElement = 10006,
187  kSqlValue = 10007,
188  kSqlArray = 10008,
189  kSqlObjectData = 10009,
190  kSqlCustomClass = 10010,
191  kSqlCustomElement = 10011
192  };
193 
194  enum ESQLColumns {
195  kColUnknown = 0,
196  kColSimple = 1,
197  kColSimpleArray = 2,
198  kColParent = 3,
199  kColObject = 4,
200  kColObjectArray = 5,
201  kColNormObject = 6,
202  kColNormObjectArray = 7,
203  kColObjectPtr = 8,
204  kColTString = 9,
205  kColRawData = 10
206  };
207 
208  enum ESQLIdType { kIdTable = 0, kIdRawTable = 1, kIdColumn = 2 };
209 
210  ClassDef(TSQLStructure, 1); // Table/structure description used internally by TBufferSQL.
211 };
212 
213 // text constants, used in SQL I/O
214 
215 namespace sqlio {
216 
217 extern Long64_t atol64(const char *value);
218 
219 extern const Int_t Ids_NullPtr;
220 extern const Int_t Ids_RootDir;
221 extern const Int_t Ids_TSQLFile;
222 extern const Int_t Ids_StreamerInfos;
223 extern const Int_t Ids_FirstKey;
224 extern const Int_t Ids_FirstObject;
225 
226 extern const char *ObjectRef;
227 extern const char *ObjectRef_Arr;
228 extern const char *ObjectPtr;
229 extern const char *ObjectInst;
230 extern const char *Version;
231 extern const char *TObjectUniqueId;
232 extern const char *TObjectBits;
233 extern const char *TObjectProcessId;
234 extern const char *TStringValue;
235 extern const char *IndexSepar;
236 extern const char *RawSuffix;
237 extern const char *ParentSuffix;
238 extern const char *ObjectSuffix;
239 extern const char *PointerSuffix;
240 extern const char *StrSuffix;
241 extern const char *LongStrPrefix;
242 
243 extern const char *Array;
244 extern const char *Bool;
245 extern const char *Char;
246 extern const char *Short;
247 extern const char *Int;
248 extern const char *Long;
249 extern const char *Long64;
250 extern const char *Float;
251 extern const char *Double;
252 extern const char *UChar;
253 extern const char *UShort;
254 extern const char *UInt;
255 extern const char *ULong;
256 extern const char *ULong64;
257 extern const char *CharStar;
258 extern const char *True;
259 extern const char *False;
260 
261 extern const char *KeysTable;
262 extern const char *KeysTableIndex;
263 extern const char *KT_Name;
264 extern const char *KT_Title;
265 extern const char *KT_Datetime;
266 extern const char *KT_Cycle;
267 extern const char *KT_Class;
268 
269 extern const char *DT_Create;
270 extern const char *DT_Modified;
271 extern const char *DT_UUID;
272 
273 extern const char *ObjectsTable;
274 extern const char *ObjectsTableIndex;
275 extern const char *OT_Class;
276 extern const char *OT_Version;
277 
278 extern const char *IdsTable;
279 extern const char *IdsTableIndex;
280 extern const char *IT_TableID;
281 extern const char *IT_SubID;
282 extern const char *IT_Type;
283 extern const char *IT_FullName;
284 extern const char *IT_SQLName;
285 extern const char *IT_Info;
286 
287 extern const char *BT_Field;
288 extern const char *BT_Value;
289 
290 extern const char *StringsTable;
291 extern const char *ST_Value;
292 
293 extern const char *ConfigTable;
294 extern const char *CT_Field;
295 extern const char *CT_Value;
296 
297 extern const char *cfg_Version;
298 extern const char *cfg_UseSufixes;
299 extern const char *cfg_ArrayLimit;
300 extern const char *cfg_TablesType;
301 extern const char *cfg_UseTransactions;
302 extern const char *cfg_UseIndexes;
303 extern const char *cfg_LockingMode;
304 extern const char *cfg_ModifyCounter;
305 }
306 
307 #endif