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