ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TKeySQL.cxx
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 /**
13 \class TKeySQL
14 \ingroup IO
15 
16 TKeySQL represents metainforamtion about object, which was written to
17 SQL database. It keeps object id, which used to locate object data
18 from database tables.
19 */
20 
21 
22 #include "TKeySQL.h"
23 
24 #include "TROOT.h"
25 #include "TClass.h"
26 #include "TBrowser.h"
27 #include "Riostream.h"
28 
29 #include "TSQLResult.h"
30 #include "TBufferSQL2.h"
31 #include "TSQLStructure.h"
32 #include "TSQLFile.h"
33 #include <stdlib.h>
34 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// default constructor
39 
41  TKey(),
42  fKeyId(-1),
43  fObjId(-1)
44 {
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
49 
50 TKeySQL::TKeySQL(TDirectory* mother, const TObject* obj, const char* name, const char* title) :
51  TKey(mother),
52  fKeyId(-1),
53  fObjId(-1)
54 {
55  if (name) SetName(name); else
56  if (obj!=0) {SetName(obj->GetName()); fClassName=obj->ClassName();}
57  else SetName("Noname");
58 
59  if (title) SetTitle(title);
60 
61  StoreKeyObject((void*)obj, obj ? obj->IsA() : 0);
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
66 
67 TKeySQL::TKeySQL(TDirectory* mother, const void* obj, const TClass* cl, const char* name, const char* title) :
68  TKey(mother),
69  fKeyId(-1),
70  fObjId(-1)
71 {
72  if (name && *name) SetName(name);
73  else SetName(cl ? cl->GetName() : "Noname");
74 
75  if (title) SetTitle(title);
76 
77  StoreKeyObject(obj, cl);
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Create TKeySQL object, which correponds to single entry in keys table
82 
84  const char* name, const char* title,
85  const char* keydatetime, Int_t cycle, const char* classname) :
86  TKey(mother),
87  fKeyId(keyid),
88  fObjId(objid)
89 {
90  SetName(name);
91  if (title) SetTitle(title);
92  TDatime dt(keydatetime);
93  fDatime = dt;
94  fCycle = cycle;
95  fClassName = classname;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// TKeySQL destructor
100 
102 {
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Compares keydata with provided and return kTRUE if key was modified
107 /// Used in TFile::StreamKeysForDirectory() method to verify data for that keys
108 /// should be updated
109 
110 Bool_t TKeySQL::IsKeyModified(const char* keyname, const char* keytitle, const char* keydatime, Int_t cycle, const char* classname)
111 {
112  Int_t len1 = (GetName()==0) ? 0 : strlen(GetName());
113  Int_t len2 = (keyname==0) ? 0 : strlen(keyname);
114  if (len1!=len2) return kTRUE;
115  if ((len1>0) && (strcmp(GetName(), keyname)!=0)) return kTRUE;
116 
117  len1 = (GetTitle()==0) ? 0 : strlen(GetTitle());
118  len2 = (keytitle==0) ? 0 : strlen(keytitle);
119  if (len1!=len2) return kTRUE;
120  if ((len1>0) && (strcmp(GetTitle(), keytitle)!=0)) return kTRUE;
121 
122  const char* tm = GetDatime().AsSQLString();
123  len1 = (tm==0) ? 0 : strlen(tm);
124  len2 = (keydatime==0) ? 0 : strlen(keydatime);
125  if (len1!=len2) return kTRUE;
126  if ((len1>0) && (strcmp(tm, keydatime)!=0)) return kTRUE;
127 
128  if (cycle!=GetCycle()) return kTRUE;
129 
130  len1 = (GetClassName()==0) ? 0 : strlen(GetClassName());
131  len2 = (classname==0) ? 0 : strlen(classname);
132  if (len1!=len2) return kTRUE;
133  if ((len1>0) && (strcmp(GetClassName(), classname)!=0)) return kTRUE;
134 
135  return kFALSE;
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Removes key from current directory
140 /// Note: TKeySQL object is not deleted. You still have to call "delete key"
141 
142 void TKeySQL::Delete(Option_t * /*option*/)
143 {
144  TSQLFile* f = (TSQLFile*) GetFile();
145 
146  if (f!=0)
148 
149  fMotherDir->GetListOfKeys()->Remove(this);
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// return sql id of parent directory
154 
156 {
157  return GetMotherDir() ? GetMotherDir()->GetSeekDir() : 0;
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Stores object, associated with key, into data tables
162 
163 void TKeySQL::StoreKeyObject(const void* obj, const TClass* cl)
164 {
165  TSQLFile* f = (TSQLFile*) GetFile();
166 
167  fCycle = GetMotherDir()->AppendKey(this);
168 
169  fKeyId = f->DefineNextKeyId();
170 
171  fObjId = f->StoreObjectInTables(fKeyId, obj, cl);
172 
173  if (cl) fClassName = cl->GetName();
174 
175  if (GetDBObjId()>=0) {
176  fDatime.Set();
177  if (!f->WriteKeyData(this)) {
178  // cannot add entry to keys table
179  Error("StoreKeyObject","Cannot write data to key tables");
180  // delete everything relevant for that key
182  fObjId = -1;
183  }
184  }
185 
186  if (GetDBObjId()<0)
187  GetMotherDir()->GetListOfKeys()->Remove(this);
188  // fix me !!! One should delete object by other means
189  // delete this;
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// To read an object from the file.
194 /// The object associated to this key is read from the file into memory.
195 /// Before invoking this function, obj has been created via the
196 /// default constructor.
197 
199 {
200  if (tobj==0) return 0;
201 
202  void* res = ReadKeyObject(tobj, 0);
203 
204  return res==0 ? 0 : 1;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Read object derived from TObject class
209 /// If it is not TObject or in case of error, return 0
210 
212 {
213  TObject* tobj = (TObject*) ReadKeyObject(0, TObject::Class());
214 
215  if (tobj!=0) {
216  if (gROOT->GetForceStyle()) tobj->UseCurrentStyle();
217  if (tobj->IsA() == TDirectoryFile::Class()) {
218  TDirectoryFile *dir = (TDirectoryFile*) tobj;
219  dir->SetName(GetName());
220  dir->SetTitle(GetTitle());
221  dir->SetSeekDir(GetDBKeyId());
222  dir->SetMother(fMotherDir);
223  dir->ReadKeys();
224  fMotherDir->Append(dir);
225  }
226  }
227 
228  return tobj;
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// Read object derived from TObject class
233 /// If it is not TObject or in case of error, return 0
234 
235 TObject* TKeySQL::ReadObjWithBuffer(char * /*bufferRead*/)
236 {
237  TObject* tobj = (TObject*) ReadKeyObject(0, TObject::Class());
238 
239  if (tobj!=0) {
240  if (gROOT->GetForceStyle()) tobj->UseCurrentStyle();
241  if (tobj->IsA() == TDirectoryFile::Class()) {
242  TDirectoryFile *dir = (TDirectoryFile*) tobj;
243  dir->SetName(GetName());
244  dir->SetTitle(GetTitle());
245  dir->SetSeekDir(GetDBKeyId());
246  dir->SetMother(fMotherDir);
247  dir->ReadKeys();
248  fMotherDir->Append(dir);
249  }
250  }
251 
252  return tobj;
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Read object of any type from SQL database
257 
258 void* TKeySQL::ReadObjectAny(const TClass* expectedClass)
259 {
260  return ReadKeyObject(0, expectedClass);
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Read object, associated with key, from database
265 
266 void* TKeySQL::ReadKeyObject(void* obj, const TClass* expectedClass)
267 {
268  TSQLFile* f = (TSQLFile*) GetFile();
269 
270  if ((GetDBKeyId()<=0) || (f==0)) return obj;
271 
273 
274  TClass* cl = 0;
275 
276  void* res = buffer.SqlReadAny(GetDBKeyId(), GetDBObjId(), &cl, obj);
277 
278  if ((cl==0) || (res==0)) return 0;
279 
280  Int_t delta = 0;
281 
282  if (expectedClass!=0) {
283  delta = cl->GetBaseClassOffset(expectedClass);
284  if (delta<0) {
285  if (obj==0) cl->Destructor(res);
286  return 0;
287  }
288  if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
289  //we cannot mix a compiled class with an emulated class in the inheritance
290  Warning("XmlReadAny",
291  "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
292  cl->GetName(),expectedClass->GetName());
293  }
294  }
295 
296  return ((char*)res) + delta;
297 }
EState GetState() const
Definition: TClass.h:442
void * SqlReadAny(Long64_t keyid, Long64_t objid, TClass **cl, void *obj=0)
Recreate object from sql structure.
Long64_t fKeyId
! key identifier in KeysTables
Definition: TKeySQL.h:35
tuple buffer
Definition: tree.py:99
long long Long64_t
Definition: RtypesCore.h:69
TDirectory * GetMotherDir() const
Definition: TKey.h:87
void SetSeekDir(Long64_t v)
void Set()
Set Date/Time to current time as reported by the system.
Definition: TDatime.cxx:286
Long64_t GetDBDirId() const
return sql id of parent directory
Definition: TKeySQL.cxx:155
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
void * ReadKeyObject(void *obj, const TClass *expectedClass)
Read object, associated with key, from database.
Definition: TKeySQL.cxx:266
const char Option_t
Definition: RtypesCore.h:62
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
Long64_t GetDBObjId() const
Definition: TKeySQL.h:49
Short_t GetCycle() const
Return cycle number associated to this key.
Definition: TKey.cxx:561
Long64_t fObjId
! stored object identifer
Definition: TKeySQL.h:36
#define gROOT
Definition: TROOT.h:344
TFile * GetFile() const
Returns file to which key belong.
Definition: TKey.cxx:569
virtual const char * GetClassName() const
Definition: TKey.h:77
int Int_t
Definition: RtypesCore.h:41
Long64_t StoreObjectInTables(Long64_t keyid, const void *obj, const TClass *cl)
Store object in database. Return stored object id or -1 if error.
Definition: TSQLFile.cxx:2532
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:753
TKeySQL()
default constructor
Definition: TKeySQL.cxx:40
TDirectory * fMotherDir
!pointer to mother directory
Definition: TKey.h:53
TFile * f
Int_t GetBaseClassOffset(const TClass *toBase, void *address=0, bool isDerivedObject=true)
Definition: TClass.cxx:2625
Bool_t IsKeyModified(const char *keyname, const char *keytitle, const char *keydatime, Int_t cycle, const char *classname)
Compares keydata with provided and return kTRUE if key was modified Used in TFile::StreamKeysForDirec...
Definition: TKeySQL.cxx:110
void StoreKeyObject(const void *obj, const TClass *cl)
Stores object, associated with key, into data tables.
Definition: TKeySQL.cxx:163
void Class()
Definition: Class.C:29
ClassImp(TKeySQL)
virtual TObject * ReadObjWithBuffer(char *bufferRead)
Read object derived from TObject class If it is not TObject or in case of error, return 0...
Definition: TKeySQL.cxx:235
virtual Long64_t GetSeekDir() const
Definition: TDirectory.h:163
TString fClassName
Object Class name.
Definition: TKey.h:48
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
TDatime fDatime
Date/Time of insertion in file.
Definition: TKey.h:43
Long64_t DefineNextKeyId()
Returns next possible key identifier.
Definition: TSQLFile.cxx:1775
virtual const char * GetTitle() const
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition: TKey.cxx:1522
Bool_t WriteKeyData(TKeySQL *key)
Add entry into keys table.
Definition: TSQLFile.cxx:1708
virtual ~TKeySQL()
TKeySQL destructor.
Definition: TKeySQL.cxx:101
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:32
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
A ROOT file is structured in Directories (like a file system).
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:675
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:4959
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:151
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
void DeleteKeyFromDB(Long64_t keyid)
Remove key with specified id from keys table also removes all objects data, related to this table...
Definition: TSQLFile.cxx:1629
virtual Int_t Read(const char *name)
Read contents of object with specified name from the current directory.
Definition: TKeySQL.h:31
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:127
Converts data to SQL statements or read data from SQL tables.
Definition: TBufferSQL2.h:36
virtual void SetName(const char *newname)
Set the name for directory If the directory name is changed after the directory was written once...
Short_t fCycle
Cycle number.
Definition: TKey.h:45
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
virtual Int_t ReadKeys(Bool_t forceRead=kTRUE)
Read the linked list of keys.
Describe directory structure in memory.
Definition: TDirectory.h:44
void dir(char *path=0)
Definition: rootalias.C:30
virtual void * ReadObjectAny(const TClass *expectedClass)
Read object of any type from SQL database.
Definition: TKeySQL.cxx:258
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual TObject * ReadObj()
Read object derived from TObject class If it is not TObject or in case of error, return 0...
Definition: TKeySQL.cxx:211
Mother of all ROOT objects.
Definition: TObject.h:58
TKeySQL represents metainforamtion about object, which was written to SQL database.
Definition: TKeySQL.h:22
Long64_t GetDBKeyId() const
Definition: TKeySQL.h:48
const TDatime & GetDatime() const
Definition: TKey.h:83
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
TObject * obj
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:192
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition: TDatime.cxx:149
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:39
virtual void Delete(Option_t *option="")
Removes key from current directory Note: TKeySQL object is not deleted.
Definition: TKeySQL.cxx:142
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904