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
32
33////////////////////////////////////////////////////////////////////////////////
34/// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
35
36TKeySQL::TKeySQL(TDirectory *mother, const TObject *obj, const char *name, const char *title)
37 : TKey(mother)
38{
39 if (name)
41 else if (obj) {
42 SetName(obj->GetName());
43 fClassName = obj->ClassName();
44 } else
45 SetName("Noname");
46
47 if (title)
48 SetTitle(title);
49
50 StoreKeyObject((void *)obj, obj ? obj->IsA() : nullptr);
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// Creates TKeySQL and convert obj data to TSQLStructure via TBufferSQL2
55
56TKeySQL::TKeySQL(TDirectory *mother, const void *obj, const TClass *cl, const char *name, const char *title)
57 : TKey(mother)
58{
59 if (name && *name)
61 else
62 SetName(cl ? cl->GetName() : "Noname");
63
64 if (title)
65 SetTitle(title);
66
67 StoreKeyObject(obj, cl);
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Create TKeySQL object, which corresponds to single entry in keys table
72
73TKeySQL::TKeySQL(TDirectory *mother, Long64_t keyid, Long64_t objid, const char *name, const char *title,
74 const char *keydatetime, Int_t cycle, const char *classname)
75 : TKey(mother), fKeyId(keyid), fObjId(objid)
76{
78 if (title)
79 SetTitle(title);
81 fDatime = dt;
82 fCycle = cycle;
83 fClassName = classname;
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Compares keydata with provided and return kTRUE if key was modified
88/// Used in TFile::StreamKeysForDirectory() method to verify data for that keys
89/// should be updated
90
91Bool_t TKeySQL::IsKeyModified(const char *keyname, const char *keytitle, const char *keydatime, Int_t cycle,
92 const char *classname)
93{
94 Int_t len1 = !GetName() ? 0 : strlen(GetName());
96 if (len1 != len2)
97 return kTRUE;
98 if ((len1 > 0) && (strcmp(GetName(), keyname) != 0))
99 return kTRUE;
100
101 len1 = !GetTitle() ? 0 : strlen(GetTitle());
102 len2 = !keytitle ? 0 : strlen(keytitle);
103 if (len1 != len2)
104 return kTRUE;
105 if ((len1 > 0) && (strcmp(GetTitle(), keytitle) != 0))
106 return kTRUE;
107
108 const char *tm = GetDatime().AsSQLString();
109 len1 = !tm ? 0 : strlen(tm);
110 len2 = !keydatime ? 0 : strlen(keydatime);
111 if (len1 != len2)
112 return kTRUE;
113 if ((len1 > 0) && (strcmp(tm, keydatime) != 0))
114 return kTRUE;
115
116 if (cycle != GetCycle())
117 return kTRUE;
118
119 len1 = !GetClassName() ? 0 : strlen(GetClassName());
120 len2 = !classname ? 0 : strlen(classname);
121 if (len1 != len2)
122 return kTRUE;
123 if ((len1 > 0) && (strcmp(GetClassName(), classname) != 0))
124 return kTRUE;
125
126 return kFALSE;
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Removes key from current directory
131/// Note: TKeySQL object is not deleted. You still have to call "delete key"
132
133void TKeySQL::Delete(Option_t * /*option*/)
134{
135 TSQLFile *f = (TSQLFile *)GetFile();
136
137 if (f)
138 f->DeleteKeyFromDB(GetDBKeyId());
139
140 fMotherDir->GetListOfKeys()->Remove(this);
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// return sql id of parent directory
145
147{
148 return GetMotherDir() ? GetMotherDir()->GetSeekDir() : 0;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Stores object, associated with key, into data tables
153
154void TKeySQL::StoreKeyObject(const void *obj, const TClass *cl)
155{
156 TSQLFile *f = (TSQLFile *)GetFile();
157
158 fCycle = GetMotherDir()->AppendKey(this);
159
160 fKeyId = f->DefineNextKeyId();
161
162 fObjId = f->StoreObjectInTables(fKeyId, obj, cl);
163
164 if (cl)
165 fClassName = cl->GetName();
166
167 if (GetDBObjId() >= 0) {
168 fDatime.Set();
169 if (!f->WriteKeyData(this)) {
170 // cannot add entry to keys table
171 Error("StoreKeyObject", "Cannot write data to key tables");
172 // delete everything relevant for that key
173 f->DeleteKeyFromDB(GetDBKeyId());
174 fObjId = -1;
175 }
176 }
177
178 if (GetDBObjId() < 0)
179 GetMotherDir()->GetListOfKeys()->Remove(this);
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// To read an object from the file.
184/// The object associated to this key is read from the file into memory.
185/// Before invoking this function, obj has been created via the
186/// default constructor.
187
189{
190 if (!tobj)
191 return 0;
192
193 void *res = ReadKeyObject(tobj, nullptr);
194
195 return !res ? 0 : 1;
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Read object derived from TObject class
200/// If it is not TObject or in case of error, return 0
201
203{
205
206 if (tobj) {
207 if (gROOT->GetForceStyle())
208 tobj->UseCurrentStyle();
209 if (tobj->IsA() == TDirectoryFile::Class()) {
211 dir->SetName(GetName());
212 dir->SetTitle(GetTitle());
213 dir->SetSeekDir(GetDBKeyId());
214 dir->SetMother(fMotherDir);
215 dir->ReadKeys();
216 fMotherDir->Append(dir);
217 }
218 }
219
220 return tobj;
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Read object derived from TObject class
225/// If it is not TObject or in case of error, return 0
226
227TObject *TKeySQL::ReadObjWithBuffer(char * /*bufferRead*/)
228{
230
231 if (tobj) {
232 if (gROOT->GetForceStyle())
233 tobj->UseCurrentStyle();
234 if (tobj->IsA() == TDirectoryFile::Class()) {
236 dir->SetName(GetName());
237 dir->SetTitle(GetTitle());
238 dir->SetSeekDir(GetDBKeyId());
239 dir->SetMother(fMotherDir);
240 dir->ReadKeys();
241 fMotherDir->Append(dir);
242 }
243 }
244
245 return tobj;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Read object of any type from SQL database
250
252{
253 void *res = ReadKeyObject(nullptr, expectedClass);
254
255 if (res && (expectedClass == TDirectoryFile::Class())) {
256 TDirectoryFile *dir = (TDirectoryFile *)res;
257 dir->SetName(GetName());
258 dir->SetTitle(GetTitle());
259 dir->SetSeekDir(GetDBKeyId());
260 dir->SetMother(fMotherDir);
261 dir->ReadKeys();
262 fMotherDir->Append(dir);
263 }
264
265 return res;
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Read object, associated with key, from database
270
272{
273 TSQLFile *f = (TSQLFile *)GetFile();
274
275 if ((GetDBKeyId() <= 0) || !f)
276 return obj;
277
279
280 buffer.InitMap();
281
282 TClass *cl = nullptr;
283
284 void *res = buffer.SqlReadAny(GetDBKeyId(), GetDBObjId(), &cl, obj);
285
286 if (!cl || !res)
287 return nullptr;
288
289 Int_t delta = 0;
290
291 if (expectedClass) {
293 if (delta < 0) {
294 if (!obj)
295 cl->Destructor(res);
296 return nullptr;
297 }
298 if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
299 // we cannot mix a compiled class with an emulated class in the inheritance
300 Warning("XmlReadAny", "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
301 cl->GetName(), expectedClass->GetName());
302 }
303 }
304
305 return ((char *)res) + delta;
306}
#define f(i)
Definition RSha256.hxx:104
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:411
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:84
EState GetState() const
Definition TClass.h:501
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5439
Int_t GetBaseClassOffset(const TClass *toBase, void *address=nullptr, bool isDerivedObject=true)
Definition TClass.cxx:2796
@ kEmulated
Definition TClass.h:128
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition TDatime.h:37
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).
static TClass * Class()
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:229
virtual Int_t AppendKey(TKey *)
Definition TDirectory.h:185
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:259
virtual TList * GetListOfKeys() const
Definition TDirectory.h:224
void * ReadObjectAny(const TClass *expectedClass) final
Read object of any type from SQL database.
Definition TKeySQL.cxx:251
void * ReadKeyObject(void *obj, const TClass *expectedClass)
Read object, associated with key, from database.
Definition TKeySQL.cxx:271
TObject * ReadObj() final
Read object derived from TObject class If it is not TObject or in case of error, return 0.
Definition TKeySQL.cxx:202
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:133
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:91
Long64_t GetDBKeyId() const
Definition TKeySQL.h:45
Long64_t GetDBDirId() const
return sql id of parent directory
Definition TKeySQL.cxx:146
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:227
Long64_t GetDBObjId() const
Definition TKeySQL.h:46
Int_t Read(TObject *obj) final
To read an object from the file.
Definition TKeySQL.cxx:188
void StoreKeyObject(const void *obj, const TClass *cl)
Stores object, associated with key, into data tables.
Definition TKeySQL.cxx:154
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
const char * GetTitle() const override
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition TKey.cxx:1523
TFile * GetFile() const
Returns file to which key belong.
Definition TKey.cxx:587
TDatime fDatime
Date/Time of insertion in file.
Definition TKey.h:42
virtual const char * GetClassName() const
Definition TKey.h:75
const TDatime & GetDatime() const
Definition TKey.h:81
Short_t fCycle
Cycle number.
Definition TKey.h:44
TDirectory * GetMotherDir() const
Definition TKey.h:85
Short_t GetCycle() const
Return cycle number associated to this key.
Definition TKey.cxx:579
TDirectory * fMotherDir
!pointer to mother directory
Definition TKey.h:52
TString fClassName
Object Class name.
Definition TKey.h:47
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
static TClass * Class()
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual TClass * IsA() const
Definition TObject.h:246
Access an SQL db via the TFile interface.
Definition TSQLFile.h:30