Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
16TKeySQL represents meta-information about object, which was written to
17SQL database. It keeps object id, which used to locate object data
18from 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
37TKeySQL::TKeySQL(TDirectory *mother, const TObject *obj, const char *name, const char *title)
38 : TKey(mother)
39{
40 if (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
57TKeySQL::TKeySQL(TDirectory *mother, const void *obj, const TClass *cl, const char *name, const char *title)
58 : TKey(mother)
59{
60 if (name && *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
74TKeySQL::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{
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
92Bool_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
134void TKeySQL::Delete(Option_t * /*option*/)
135{
136 TSQLFile *f = (TSQLFile *)GetFile();
137
138 if (f != 0)
139 f->DeleteKeyFromDB(GetDBKeyId());
140
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
155void 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)
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
228TObject *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
252void *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
272void *TKeySQL::ReadKeyObject(void *obj, const TClass *expectedClass)
273{
274 TSQLFile *f = (TSQLFile *)GetFile();
275
276 if ((GetDBKeyId() <= 0) || !f)
277 return obj;
278
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}
#define f(i)
Definition RSha256.hxx:104
const Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:404
void InitMap() override
Create the fMap container and initialize them with the null object.
Converts data to SQL statements or read data from SQL tables.
Definition TBufferSQL2.h:27
void * SqlReadAny(Long64_t keyid, Long64_t objid, TClass **cl, void *obj=nullptr)
Recreate object from sql structure.
@ kRead
Definition TBuffer.h:73
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
EState GetState() const
Definition TClass.h:485
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5386
Int_t GetBaseClassOffset(const TClass *toBase, void *address=0, bool isDerivedObject=true)
Definition TClass.cxx:2789
@ kEmulated
Definition TClass.h:125
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition TDatime.h:37
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition TDatime.cxx:152
void Set()
Set Date/Time to current time as reported by the system.
Definition TDatime.cxx:289
A ROOT file is structured in Directories (like a file system).
Int_t ReadKeys(Bool_t forceRead=kTRUE) override
Read the linked list of keys.
void SetSeekDir(Long64_t v) override
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Long64_t GetSeekDir() const
Definition TDirectory.h:228
virtual Int_t AppendKey(TKey *)
Definition TDirectory.h:184
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
void SetName(const char *newname) override
Set the name for directory If the directory name is changed after the directory was written once,...
virtual void SetMother(TObject *mother)
Definition TDirectory.h:258
virtual TList * GetListOfKeys() const
Definition TDirectory.h:223
TKeySQL represents meta-information about object, which was written to SQL database.
Definition TKeySQL.h:19
void * ReadObjectAny(const TClass *expectedClass) final
Read object of any type from SQL database.
Definition TKeySQL.cxx:252
void * ReadKeyObject(void *obj, const TClass *expectedClass)
Read object, associated with key, from database.
Definition TKeySQL.cxx:272
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
Long64_t fKeyId
! key identifier in KeysTables
Definition TKeySQL.h:33
TKeySQL()
Definition TKeySQL.h:26
Long64_t fObjId
! stored object identifier
Definition TKeySQL.h:34
void Delete(Option_t *option="") final
Removes key from current directory Note: TKeySQL object is not deleted.
Definition TKeySQL.cxx:134
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
Long64_t GetDBKeyId() const
Definition TKeySQL.h:45
Long64_t GetDBDirId() const
return sql id of parent directory
Definition TKeySQL.cxx:147
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
Long64_t GetDBObjId() const
Definition TKeySQL.h:46
Int_t Read(TObject *obj) final
To read an object from the file.
Definition TKeySQL.cxx:189
void StoreKeyObject(const void *obj, const TClass *cl)
Stores object, associated with key, into data tables.
Definition TKeySQL.cxx:155
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
TFile * GetFile() const
Returns file to which key belong.
Definition TKey.cxx:585
TDatime fDatime
Date/Time of insertion in file.
Definition TKey.h:42
virtual const char * GetClassName() const
Definition TKey.h:76
virtual const char * GetTitle() const
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition TKey.cxx:1532
const TDatime & GetDatime() const
Definition TKey.h:82
Short_t fCycle
Cycle number.
Definition TKey.h:44
TDirectory * GetMotherDir() const
Definition TKey.h:86
Short_t GetCycle() const
Return cycle number associated to this key.
Definition TKey.cxx:577
TDirectory * fMotherDir
!pointer to mother directory
Definition TKey.h:52
TString fClassName
Object Class name.
Definition TKey.h:47
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition TList.cxx:822
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:429
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:200
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition TObject.cxx:787
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:949
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
Access an SQL db via the TFile interface.
Definition TSQLFile.h:30