ROOT  6.06/09
Reference Guide
TSQLObjectData.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 TSQLObjectData
14 \ingroup IO
15 
16 TSQLObjectData is used in TBufferSQL2 class in reading procedure.
17 It contains data, request from database table for one specifc
18 object for one specific class. For instance, when data for
19 class TH1 required, requests will be done to
20 TH1_ver4 and TH1_raw4 tables and result of these requests
21 will be kept in single TSQLObjectData instance.
22 */
23 
24 #include "TSQLObjectData.h"
25 
26 #include "TObjArray.h"
27 #include "TNamed.h"
28 #include "TList.h"
29 #include "TSQLRow.h"
30 #include "TSQLResult.h"
31 #include "TSQLClassInfo.h"
32 #include "TSQLStructure.h"
33 #include "TSQLStatement.h"
34 
35 /**
36 \class TSQLObjectInfo
37 \ingroup IO
38 Info (classname, version) about object in database
39 */
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 
46  TObject(),
47  fObjId(0),
48  fClassName(),
49  fVersion(0)
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 TSQLObjectInfo::TSQLObjectInfo(Long64_t objid, const char* classname, Version_t version) :
56  TObject(),
57  fObjId(objid),
58  fClassName(classname),
59  fVersion(version)
60 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 
66 {
67 }
68 
69 /**
70 \class TSQLObjectData
71 \ingroup IO
72 Keeps the data requested from the SQL server for an object.
73 */
74 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// default contrsuctor
79 
81  TObject(),
82  fInfo(0),
83  fObjId(0),
84  fOwner(kFALSE),
85  fClassData(0),
86  fBlobData(0),
87  fBlobStmt(0),
88  fLocatedColumn(-1),
89  fClassRow(0),
90  fBlobRow(0),
91  fLocatedField(0),
92  fLocatedValue(0),
93  fCurrentBlob(kFALSE),
94  fBlobPrefixName(0),
95  fBlobTypeName(0),
96  fUnpack(0)
97 {
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// normal contrsuctor,
102 
104  Long64_t objid,
105  TSQLResult* classdata,
106  TSQLRow* classrow,
107  TSQLResult* blobdata,
108  TSQLStatement* blobstmt) :
109  TObject(),
110  fInfo(sqlinfo),
111  fObjId(objid),
112  fOwner(kFALSE),
113  fClassData(classdata),
114  fBlobData(blobdata),
115  fBlobStmt(blobstmt),
116  fLocatedColumn(-1),
117  fClassRow(classrow),
118  fBlobRow(0),
119  fLocatedField(0),
120  fLocatedValue(0),
121  fCurrentBlob(kFALSE),
122  fBlobPrefixName(0),
123  fBlobTypeName(0),
124  fUnpack(0)
125 {
126  // take ownership if no special row from data pool is provided
127  if ((fClassData!=0) && (fClassRow==0)) {
128  fOwner = kTRUE;
130  }
131 
132  ShiftBlobRow();
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// destructor of TSQLObjectData object
137 
139 {
140  if ((fClassData!=0) && fOwner) delete fClassData;
141  if (fClassRow!=0) delete fClassRow;
142  if (fBlobRow!=0) delete fBlobRow;
143  if (fBlobData!=0) delete fBlobData;
144  if (fUnpack!=0) { fUnpack->Delete(); delete fUnpack; }
145  if (fBlobStmt!=0) delete fBlobStmt;
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// return number of columns in class table result
150 
152 {
153  if (fClassData!=0) return fClassData->GetFieldCount();
154  return 0;
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// get name of class table column
159 
161 {
162  if (fClassData!=0) return fClassData->GetFieldName(n);
163  return 0;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// locate column of that name in results
168 
169 Bool_t TSQLObjectData::LocateColumn(const char* colname, Bool_t isblob)
170 {
171  if (fUnpack!=0) {
172  fUnpack->Delete();
173  delete fUnpack;
174  fUnpack = 0;
175  }
176 
177  fLocatedField = 0;
178  fLocatedValue = 0;
180 
181  if ((fClassData==0) || (fClassRow==0)) return kFALSE;
182 
183 // Int_t numfields = GetNumClassFields();
184 
185  Int_t ncol = fInfo->FindColumn(colname, kFALSE);
186  if (ncol>0) {
187  fLocatedColumn = ncol;
190  }
191 
192 
193 /* for (Int_t ncol=1;ncol<numfields;ncol++) {
194  const char* fieldname = GetClassFieldName(ncol);
195  if (strcmp(colname, fieldname)==0) {
196  fLocatedColumn = ncol;
197  fLocatedField = fieldname;
198  fLocatedValue = fClassRow->GetField(ncol);
199  break;
200  }
201  }
202 */
203 
204  if (fLocatedField==0) return kFALSE;
205 
206  if (!isblob) return kTRUE;
207 
208  if ((fBlobRow==0) && (fBlobStmt==0)) return kFALSE;
209 
211 
213 
214  return kTRUE;
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// shift cursor to next blob value
219 
221 {
222  if (fBlobStmt!=0) {
223  Bool_t res = fBlobStmt->NextResultRow();
224  if (!res) { delete fBlobStmt; fBlobStmt = 0; }
225  return res;
226  }
227 
228  delete fBlobRow;
229  fBlobRow = fBlobData ? fBlobData->Next() : 0;
230  return fBlobRow!=0;
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// extract from curent blob row value and names identifiers
235 
237 {
238  const char* name = 0;
239 
240  Bool_t hasdata = kFALSE;
241 
242  if (fBlobStmt!=0) {
243  name = fBlobStmt->GetString(0);
245  hasdata = kTRUE;
246  }
247 
248  if (!hasdata) {
249  if (fBlobRow!=0) {
251  name = fBlobRow->GetField(0);
252  }
253  }
254 
255  if (name==0) {
256  fBlobPrefixName = 0;
257  fBlobTypeName = 0;
258  return kFALSE;
259  }
260 
261  const char* separ = strstr(name, ":"); //SQLNameSeparator()
262 
263  if (separ==0) {
264  fBlobPrefixName = 0;
266  } else {
268  separ+=strlen(":"); //SQLNameSeparator()
269  fBlobTypeName = separ;
270  }
271 
272 // if (gDebug>4)
273 // Info("ExtractBlobValues","Prefix:%s Type:%s",
274 // (fBlobPrefixName ? fBlobPrefixName : "null"),
275 // (fBlobTypeName ? fBlobTypeName : "null"));
276 
277  return kTRUE;
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// add emulated data
282 /// this used to place version or TObject raw data, read from normal tables
283 
284 void TSQLObjectData::AddUnpack(const char* tname, const char* value)
285 {
286  TNamed* str = new TNamed(tname, value);
287  if (fUnpack==0) {
288  fUnpack = new TObjArray();
289  fBlobPrefixName = 0;
290  fBlobTypeName = str->GetName();
291  fLocatedValue = str->GetTitle();
292  }
293 
294  fUnpack->Add(str);
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 /// emulate integer value in raw data
299 
300 void TSQLObjectData::AddUnpackInt(const char* tname, Int_t value)
301 {
302  TString sbuf;
303  sbuf.Form("%d", value);
304  AddUnpack(tname, sbuf.Data());
305 }
306 
307 ////////////////////////////////////////////////////////////////////////////////
308 /// shift to next column or next row in blob data
309 
311 {
312  Bool_t doshift = kTRUE;
313 
314  if (fUnpack!=0) {
315  TObject* prev = fUnpack->First();
316  fUnpack->Remove(prev);
317  delete prev;
318  fUnpack->Compress();
319  if (fUnpack->GetLast()>=0) {
320  TNamed* curr = (TNamed*) fUnpack->First();
321  fBlobPrefixName = 0;
322  fBlobTypeName = curr->GetName();
323  fLocatedValue = curr->GetTitle();
324  return;
325  }
326  delete fUnpack;
327  fUnpack = 0;
328  doshift = kFALSE;
329  }
330 
331  if (fCurrentBlob) {
332  if (doshift) ShiftBlobRow();
334  } else
335  if (fClassData!=0) {
336  if (doshift) fLocatedColumn++;
340  } else {
341  fLocatedField = 0;
342  fLocatedValue = 0;
343  }
344  }
345 }
346 
347 ////////////////////////////////////////////////////////////////////////////////
348 /// checks if data type corresponds to that stored in raw table
349 
350 Bool_t TSQLObjectData::VerifyDataType(const char* tname, Bool_t errormsg)
351 {
352  if (tname==0) {
353  if (errormsg)
354  Error("VerifyDataType","Data type not specified");
355  return kFALSE;
356  }
357 
358  // here maybe type of column can be checked
359  if (!IsBlobData()) return kTRUE;
360 
361  if (gDebug>4)
362  if ((fBlobTypeName==0) && errormsg) {
363  Error("VerifyDataType","fBlobTypeName is null");
364  return kFALSE;
365  }
366 
367 
369  TString v2(tname);
370 
371 // if (strcmp(fBlobTypeName,tname)!=0) {
372  if (v1!=v2) {
373  if (errormsg)
374  Error("VerifyDataType","Data type missmatch %s - %s", fBlobTypeName, tname);
375  return kFALSE;
376  }
377 
378  return kTRUE;
379 }
380 
381 ////////////////////////////////////////////////////////////////////////////////
382 /// prepare to read data from raw table
383 
385 {
386  if (!ExtractBlobValues()) return kFALSE;
387 
389 
390  return kTRUE;
391 }
392 
393 //===================================================================================
394 
395 //________________________________________________________________________
396 //
397 // TSQLObjectDataPool contains list (pool) of data from single class table
398 // for differents objects, all belonging to the same key.
399 // This is typical situation when list of objects stored as single key.
400 // To optimize reading of such data, one query is submitted and results of that
401 // query kept in TSQLObjectDataPool object
402 //
403 //________________________________________________________________________
404 
405 
407 
408 ////////////////////////////////////////////////////////////////////////////////
409 
411  TObject(),
412  fInfo(0),
413  fClassData(0),
414  fIsMoreRows(kTRUE),
415  fRowsPool(0)
416 {
417 }
418 
419 ////////////////////////////////////////////////////////////////////////////////
420 
422  TObject(),
423  fInfo(info),
424  fClassData(data),
425  fIsMoreRows(kTRUE),
426  fRowsPool(0)
427 {
428 }
429 
430 ////////////////////////////////////////////////////////////////////////////////
431 /// Destructor of TSQLObjectDataPool class
432 /// Deletes not used rows and class data table
433 
435 {
436  if (fClassData!=0) delete fClassData;
437  if (fRowsPool!=0) {
438  fRowsPool->Delete();
439  delete fRowsPool;
440  }
441 }
442 
443 ////////////////////////////////////////////////////////////////////////////////
444 /// Returns single sql row with object data for that class
445 
447 {
448  if (fClassData==0) return 0;
449 
450  Long64_t rowid;
451 
452  if (fRowsPool!=0) {
454  while (link!=0) {
455  TSQLRow* row = (TSQLRow*) link->GetObject();
456  rowid = sqlio::atol64(row->GetField(0));
457  if (rowid==objid) {
458  fRowsPool->Remove(link);
459  return row;
460  }
461 
462  link = link->Next();
463  }
464  }
465 
466  while (fIsMoreRows) {
467  TSQLRow* row = fClassData->Next();
468  if (row==0)
470  else {
471  rowid = sqlio::atol64(row->GetField(0));
472  if (rowid==objid) return row;
473  if (fRowsPool==0) fRowsPool = new TList();
474  fRowsPool->Add(row);
475  }
476  }
477 
478  return 0;
479 }
480 
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
const char * fBlobPrefixName
! name prefix in current blob row
XML object keeper class.
An array of TObjects.
Definition: TObjArray.h:39
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
Info (classname, version) about object in database.
long long Long64_t
Definition: RtypesCore.h:69
const Double_t * v1
Definition: TArcBall.cxx:33
void ShiftToNextValue()
shift to next column or next row in blob data
virtual ~TSQLObjectInfo()
short Version_t
Definition: RtypesCore.h:61
TSQLStatement * fBlobStmt
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:527
TSQLResult * fClassData
! results with request to selected table
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:328
ClassImp(TSQLObjectInfo) TSQLObjectInfo
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:652
Contains information about tables specific to one class and version.
Definition: TSQLClassInfo.h:48
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
const char * fLocatedValue
const char * fBlobTypeName
! name type (without prefix) in current blob row
TSQLClassInfo * fInfo
virtual const char * GetString(Int_t)
Definition: TSQLStatement.h:91
TSQLObjectData is used in TBufferSQL2 class in reading procedure.
const char * Data() const
Definition: TString.h:349
Long64_t atol64(const char *value)
ClassImp(RooList) TObjOptLink *RooList TObjLink * link
Find the link corresponding to the named object in this list.
Definition: RooList.cxx:48
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TSQLResult * fBlobData
Int_t GetNumClassFields()
return number of columns in class table result
TObject * First() const
Return the object in the first slot.
Definition: TObjArray.cxx:470
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
virtual Bool_t NextResultRow()=0
virtual ~TSQLObjectData()
destructor of TSQLObjectData object
A doubly linked list.
Definition: TList.h:47
virtual ~TSQLObjectDataPool()
Destructor of TSQLObjectDataPool class Deletes not used rows and class data table.
Bool_t ShiftBlobRow()
shift cursor to next blob value
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:674
Bool_t VerifyDataType(const char *tname, Bool_t errormsg=kTRUE)
checks if data type corresponds to that stored in raw table
TObjArray * fUnpack
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2321
virtual const char * GetField(Int_t field)=0
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Bool_t IsBlobData() const
virtual TObjLink * FirstLink() const
Definition: TList.h:101
virtual Int_t GetFieldCount()=0
Bool_t LocateColumn(const char *colname, Bool_t isblob=kFALSE)
locate column of that name in results
TSQLRow * GetObjectRow(Long64_t objid)
Returns single sql row with object data for that class.
Bool_t PrepareForRawData()
prepare to read data from raw table
TSQLResult * fClassData
void AddUnpack(const char *tname, const char *value)
add emulated data this used to place version or TObject raw data, read from normal tables ...
TList * fRowsPool
! pool of extrcted, but didnot used rows
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
const char * GetClassFieldName(Int_t n)
get name of class table column
Bool_t ExtractBlobValues()
extract from curent blob row value and names identifiers
void AddUnpackInt(const char *tname, Int_t value)
emulate integer value in raw data
virtual void Add(TObject *obj)
Definition: TList.h:81
Bool_t fIsMoreRows
! indicates if class data has not yet read rows
TSQLRow * fClassRow
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
TSQLRow * fBlobRow
void Add(TObject *obj)
Definition: TObjArray.h:75
virtual const char * GetFieldName(Int_t field)=0
virtual void Compress()
Remove empty slots from array.
Definition: TObjArray.cxx:308
const Bool_t kTRUE
Definition: Rtypes.h:91
float value
Definition: math.cpp:443
Int_t FindColumn(const char *name, Bool_t sqlname=kFALSE)
Search for column of that name.
const Int_t n
Definition: legend1.C:16
const char * fLocatedField
virtual TSQLRow * Next()=0