Logo ROOT  
Reference Guide
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 meta-information 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 #include "TKeySQL.h"
22 
23 #include "TROOT.h"
24 #include "TClass.h"
25 
26 #include "TSQLResult.h"
27 #include "TBufferSQL2.h"
28 #include "TSQLStructure.h"
29 #include "TSQLFile.h"
30 #include <cstdlib>
31 
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
36 
37 TKeySQL::TKeySQL(TDirectory *mother, const TObject *obj, const char *name, const char *title)
38  : TKey(mother)
39 {
40  if (name)
41  SetName(name);
42  else if (obj) {
43  SetName(obj->GetName());
44  fClassName = obj->ClassName();
45  } else
46  SetName("Noname");
47 
48  if (title)
49  SetTitle(title);
50 
51  StoreKeyObject((void *)obj, obj ? obj->IsA() : 0);
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
56 
57 TKeySQL::TKeySQL(TDirectory *mother, const void *obj, const TClass *cl, const char *name, const char *title)
58  : TKey(mother)
59 {
60  if (name && *name)
61  SetName(name);
62  else
63  SetName(cl ? cl->GetName() : "Noname");
64 
65  if (title)
66  SetTitle(title);
67 
68  StoreKeyObject(obj, cl);
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Create TKeySQL object, which corresponds to single entry in keys table
73 
74 TKeySQL::TKeySQL(TDirectory *mother, Long64_t keyid, Long64_t objid, const char *name, const char *title,
75  const char *keydatetime, Int_t cycle, const char *classname)
76  : TKey(mother), fKeyId(keyid), fObjId(objid)
77 {
78  SetName(name);
79  if (title)
80  SetTitle(title);
81  TDatime dt(keydatetime);
82  fDatime = dt;
83  fCycle = cycle;
84  fClassName = classname;
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Compares keydata with provided and return kTRUE if key was modified
89 /// Used in TFile::StreamKeysForDirectory() method to verify data for that keys
90 /// should be updated
91 
92 Bool_t TKeySQL::IsKeyModified(const char *keyname, const char *keytitle, const char *keydatime, Int_t cycle,
93  const char *classname)
94 {
95  Int_t len1 = (GetName() == 0) ? 0 : strlen(GetName());
96  Int_t len2 = (keyname == 0) ? 0 : strlen(keyname);
97  if (len1 != len2)
98  return kTRUE;
99  if ((len1 > 0) && (strcmp(GetName(), keyname) != 0))
100  return kTRUE;
101 
102  len1 = (GetTitle() == 0) ? 0 : strlen(GetTitle());
103  len2 = (keytitle == 0) ? 0 : strlen(keytitle);
104  if (len1 != len2)
105  return kTRUE;
106  if ((len1 > 0) && (strcmp(GetTitle(), keytitle) != 0))
107  return kTRUE;
108 
109  const char *tm = GetDatime().AsSQLString();
110  len1 = (tm == 0) ? 0 : strlen(tm);
111  len2 = (keydatime == 0) ? 0 : strlen(keydatime);
112  if (len1 != len2)
113  return kTRUE;
114  if ((len1 > 0) && (strcmp(tm, keydatime) != 0))
115  return kTRUE;
116 
117  if (cycle != GetCycle())
118  return kTRUE;
119 
120  len1 = (GetClassName() == 0) ? 0 : strlen(GetClassName());
121  len2 = (classname == 0) ? 0 : strlen(classname);
122  if (len1 != len2)
123  return kTRUE;
124  if ((len1 > 0) && (strcmp(GetClassName(), classname) != 0))
125  return kTRUE;
126 
127  return kFALSE;
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Removes key from current directory
132 /// Note: TKeySQL object is not deleted. You still have to call "delete key"
133 
134 void TKeySQL::Delete(Option_t * /*option*/)
135 {
136  TSQLFile *f = (TSQLFile *)GetFile();
137 
138  if (f != 0)
139  f->DeleteKeyFromDB(GetDBKeyId());
140 
141  fMotherDir->GetListOfKeys()->Remove(this);
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// return sql id of parent directory
146 
148 {
149  return GetMotherDir() ? GetMotherDir()->GetSeekDir() : 0;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Stores object, associated with key, into data tables
154 
155 void TKeySQL::StoreKeyObject(const void *obj, const TClass *cl)
156 {
157  TSQLFile *f = (TSQLFile *)GetFile();
158 
159  fCycle = GetMotherDir()->AppendKey(this);
160 
161  fKeyId = f->DefineNextKeyId();
162 
163  fObjId = f->StoreObjectInTables(fKeyId, obj, cl);
164 
165  if (cl)
166  fClassName = cl->GetName();
167 
168  if (GetDBObjId() >= 0) {
169  fDatime.Set();
170  if (!f->WriteKeyData(this)) {
171  // cannot add entry to keys table
172  Error("StoreKeyObject", "Cannot write data to key tables");
173  // delete everything relevant for that key
174  f->DeleteKeyFromDB(GetDBKeyId());
175  fObjId = -1;
176  }
177  }
178 
179  if (GetDBObjId() < 0)
180  GetMotherDir()->GetListOfKeys()->Remove(this);
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// To read an object from the file.
185 /// The object associated to this key is read from the file into memory.
186 /// Before invoking this function, obj has been created via the
187 /// default constructor.
188 
190 {
191  if (tobj == 0)
192  return 0;
193 
194  void *res = ReadKeyObject(tobj, 0);
195 
196  return res == 0 ? 0 : 1;
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Read object derived from TObject class
201 /// If it is not TObject or in case of error, return 0
202 
204 {
205  TObject *tobj = (TObject *)ReadKeyObject(0, TObject::Class());
206 
207  if (tobj) {
208  if (gROOT->GetForceStyle())
209  tobj->UseCurrentStyle();
210  if (tobj->IsA() == TDirectoryFile::Class()) {
211  TDirectoryFile *dir = (TDirectoryFile *)tobj;
212  dir->SetName(GetName());
213  dir->SetTitle(GetTitle());
214  dir->SetSeekDir(GetDBKeyId());
215  dir->SetMother(fMotherDir);
216  dir->ReadKeys();
217  fMotherDir->Append(dir);
218  }
219  }
220 
221  return tobj;
222 }
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// Read object derived from TObject class
226 /// If it is not TObject or in case of error, return 0
227 
228 TObject *TKeySQL::ReadObjWithBuffer(char * /*bufferRead*/)
229 {
230  TObject *tobj = (TObject *)ReadKeyObject(0, TObject::Class());
231 
232  if (tobj) {
233  if (gROOT->GetForceStyle())
234  tobj->UseCurrentStyle();
235  if (tobj->IsA() == TDirectoryFile::Class()) {
236  TDirectoryFile *dir = (TDirectoryFile *)tobj;
237  dir->SetName(GetName());
238  dir->SetTitle(GetTitle());
239  dir->SetSeekDir(GetDBKeyId());
240  dir->SetMother(fMotherDir);
241  dir->ReadKeys();
242  fMotherDir->Append(dir);
243  }
244  }
245 
246  return tobj;
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Read object of any type from SQL database
251 
252 void *TKeySQL::ReadObjectAny(const TClass *expectedClass)
253 {
254  void *res = ReadKeyObject(0, expectedClass);
255 
256  if (res && (expectedClass == TDirectoryFile::Class())) {
257  TDirectoryFile *dir = (TDirectoryFile *)res;
258  dir->SetName(GetName());
259  dir->SetTitle(GetTitle());
260  dir->SetSeekDir(GetDBKeyId());
261  dir->SetMother(fMotherDir);
262  dir->ReadKeys();
263  fMotherDir->Append(dir);
264  }
265 
266  return res;
267 }
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 /// Read object, associated with key, from database
271 
272 void *TKeySQL::ReadKeyObject(void *obj, const TClass *expectedClass)
273 {
274  TSQLFile *f = (TSQLFile *)GetFile();
275 
276  if ((GetDBKeyId() <= 0) || !f)
277  return obj;
278 
279  TBufferSQL2 buffer(TBuffer::kRead, f);
280 
281  buffer.InitMap();
282 
283  TClass *cl = nullptr;
284 
285  void *res = buffer.SqlReadAny(GetDBKeyId(), GetDBObjId(), &cl, obj);
286 
287  if (!cl || !res)
288  return nullptr;
289 
290  Int_t delta = 0;
291 
292  if (expectedClass != 0) {
293  delta = cl->GetBaseClassOffset(expectedClass);
294  if (delta < 0) {
295  if (obj == 0)
296  cl->Destructor(res);
297  return 0;
298  }
299  if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
300  // we cannot mix a compiled class with an emulated class in the inheritance
301  Warning("XmlReadAny", "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
302  cl->GetName(), expectedClass->GetName());
303  }
304  }
305 
306  return ((char *)res) + delta;
307 }
TDirectoryFile::ReadKeys
Int_t ReadKeys(Bool_t forceRead=kTRUE) override
Read the linked list of keys.
Definition: TDirectoryFile.cxx:1337
TBufferSQL2::SqlReadAny
void * SqlReadAny(Long64_t keyid, Long64_t objid, TClass **cl, void *obj=nullptr)
Recreate object from sql structure.
Definition: TBufferSQL2.cxx:132
TKeySQL
Definition: TKeySQL.h:19
TKeySQL::Delete
void Delete(Option_t *option="") final
Removes key from current directory Note: TKeySQL object is not deleted.
Definition: TKeySQL.cxx:134
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TKeySQL::fKeyId
Long64_t fKeyId
! key identifier in KeysTables
Definition: TKeySQL.h:33
TDirectory::GetListOfKeys
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:168
f
#define f(i)
Definition: RSha256.hxx:122
TSQLFile.h
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TBuffer::kRead
@ kRead
Definition: TBuffer.h:73
TKeySQL::fObjId
Long64_t fObjId
! stored object identifier
Definition: TKeySQL.h:34
TKeySQL::Read
Int_t Read(TObject *obj) final
To read an object from the file.
Definition: TKeySQL.cxx:189
TKey::GetMotherDir
TDirectory * GetMotherDir() const
Definition: TKey.h:86
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TKeySQL::GetDBKeyId
Long64_t GetDBKeyId() const
Definition: TKeySQL.h:45
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TKeySQL::TKeySQL
TKeySQL()
Definition: TKeySQL.h:26
TSQLStructure.h
TClass.h
TKey::fMotherDir
TDirectory * fMotherDir
!pointer to mother directory
Definition: TKey.h:52
TBufferIO::InitMap
void InitMap() override
Create the fMap container and initialize them with the null object.
Definition: TBufferIO.cxx:129
TKeySQL::GetDBDirId
Long64_t GetDBDirId() const
return sql id of parent directory
Definition: TKeySQL.cxx:147
TKeySQL::ReadObj
TObject * ReadObj() final
Read object derived from TObject class If it is not TObject or in case of error, return 0.
Definition: TKeySQL.cxx:203
bool
TKeySQL::GetDBObjId
Long64_t GetDBObjId() const
Definition: TKeySQL.h:46
TDatime::Set
void Set()
Set Date/Time to current time as reported by the system.
Definition: TDatime.cxx:289
TKeySQL::StoreKeyObject
void StoreKeyObject(const void *obj, const TClass *cl)
Stores object, associated with key, into data tables.
Definition: TKeySQL.cxx:155
TBufferSQL2
Definition: TBufferSQL2.h:27
TROOT.h
TClass::kEmulated
@ kEmulated
Definition: TClass.h:124
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TObject::UseCurrentStyle
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:717
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TDirectory::Append
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:191
TKeySQL::IsKeyModified
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:92
TKey::GetClassName
virtual const char * GetClassName() const
Definition: TKey.h:76
TDatime::AsSQLString
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition: TDatime.cxx:152
TKeySQL::ReadObjectAny
void * ReadObjectAny(const TClass *expectedClass) final
Read object of any type from SQL database.
Definition: TKeySQL.cxx:252
TKey::GetFile
TFile * GetFile() const
Returns file to which key belong.
Definition: TKey.cxx:585
TKey::fClassName
TString fClassName
Object Class name.
Definition: TKey.h:47
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TDirectory::AppendKey
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:130
TKeySQL::ReadObjWithBuffer
TObject * ReadObjWithBuffer(char *bufferRead) final
Read object derived from TObject class If it is not TObject or in case of error, return 0.
Definition: TKeySQL.cxx:228
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TKey
Definition: TKey.h:28
TKey::fDatime
TDatime fDatime
Date/Time of insertion in file.
Definition: TKey.h:42
TDirectory::GetSeekDir
virtual Long64_t GetSeekDir() const
Definition: TDirectory.h:173
TSQLResult.h
TKey::fCycle
Short_t fCycle
Cycle number.
Definition: TKey.h:44
TKey::GetDatime
const TDatime & GetDatime() const
Definition: TKey.h:82
TList::Remove
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:821
TClass
Definition: TClass.h:80
TKeySQL::ReadKeyObject
void * ReadKeyObject(void *obj, const TClass *expectedClass)
Read object, associated with key, from database.
Definition: TKeySQL.cxx:272
TDirectoryFile::SetSeekDir
void SetSeekDir(Long64_t v) override
Definition: TDirectoryFile.h:118
TClass::Destructor
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5238
TObject
Definition: TObject.h:37
TKey::GetCycle
Short_t GetCycle() const
Return cycle number associated to this key.
Definition: TKey.cxx:577
TClass::GetBaseClassOffset
Int_t GetBaseClassOffset(const TClass *toBase, void *address=0, bool isDerivedObject=true)
Definition: TClass.cxx:2748
name
char name[80]
Definition: TGX11.cxx:110
TDirectory
Definition: TDirectory.h:40
TClass::GetState
EState GetState() const
Definition: TClass.h:439
TBufferSQL2.h
TKey::GetTitle
virtual const char * GetTitle() const
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition: TKey.cxx:1532
TDatime
Definition: TDatime.h:37
TKeySQL.h
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TDirectory::SetName
void SetName(const char *newname) override
Set the name for directory If the directory name is changed after the directory was written once,...
Definition: TDirectory.cxx:1227
Class
void Class()
Definition: Class.C:29
TDirectoryFile
Definition: TDirectoryFile.h:32
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
TDirectory::SetMother
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:202
TSQLFile
Definition: TSQLFile.h:30
gROOT
#define gROOT
Definition: TROOT.h:406
int