StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbTable.h
1 /***************************************************************************
2  *
3  * $Id: StDbTable.h,v 1.26 2005/09/07 22:04:02 deph Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Class that holds data, descriptor, & db-address
9  * & performs streamer of db-data into data-memory
10  *
11  ***************************************************************************
12  *
13  * $Log: StDbTable.h,v $
14  * Revision 1.26 2005/09/07 22:04:02 deph
15  * update to correct padding issue for packed tables
16  *
17  * Revision 1.25 2004/01/15 00:02:25 fisyak
18  * Replace ostringstream => StString, add option for alpha
19  *
20  * Revision 1.24 2003/09/16 22:44:18 porter
21  * got rid of all ostrstream objects; replaced with StString+string.
22  * modified rules.make and added file stdb_streams.h for standalone compilation
23  *
24  * Revision 1.23 2003/09/02 17:57:49 perev
25  * gcc 3.2 updates + WarnOff
26  *
27  * Revision 1.22 2003/04/11 22:47:36 porter
28  * Added a fast multi-row write model specifically needed by the daqEventTag
29  * writer. Speed increased from about 100Hz to ~3000Hz. It is only invoked if
30  * the table is marked as Non-Indexed (daqTags & scalers). For non-indexed tables
31  * which include binary stored data (we don't have any yet), the fast writer has
32  * to invoke a slower buffer so that the rates are a bit slower (~500Hz at 50 rows/insert).
33  *
34  * Revision 1.21 2003/02/12 22:12:45 porter
35  * moved warning message about null columns (checked in 2 days ago) from the
36  * depths of the mysql coding into the StDbTable code. This suppresses confusing
37  * warnings from tables that have had elements removed but their storage columns
38  * still exist in the database.
39  *
40  * Revision 1.20 2003/01/10 04:19:21 porter
41  * added feature of getting timestamp list (but no data) for a table.
42  * fixed 2 features sometimes used in online in query-by-whereclause.
43  * removed a stray 'cout' in a routine that is rarely accessed
44  *
45  * Revision 1.19 2002/01/30 15:40:48 porter
46  * changed limits on flavor tag & made defaults retrieving more readable
47  *
48  * Revision 1.18 2001/12/21 04:54:46 porter
49  * sped up table definition for emc and changed some ostrstream usage for
50  * insure tests
51  *
52  * Revision 1.17 2001/10/24 04:05:20 porter
53  * added long long type to I/O and got rid of obsolete dataIndex table
54  *
55  * Revision 1.16 2001/02/09 23:06:25 porter
56  * replaced ostrstream into a buffer with ostrstream creating the
57  * buffer. The former somehow clashed on Solaris with CC5 iostream (current .dev)
58  *
59  * Revision 1.15 2001/01/22 18:38:00 porter
60  * Update of code needed in next year running. This update has little
61  * effect on the interface (only 1 method has been changed in the interface).
62  * Code also preserves backwards compatibility so that old versions of
63  * StDbLib can read new table structures.
64  * -Important features:
65  * a. more efficient low-level table structure (see StDbSql.cc)
66  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
67  * c. environment variable override KEYS for each database
68  * d. StMessage support & clock-time logging diagnostics
69  * -Cosmetic features
70  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
71  * f. removed codes that have been obsolete for awhile (e.g. db factories)
72  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
73  * and mysqlAccessor became StDbSql)
74  *
75  * Revision 1.14 2000/06/30 01:57:02 porter
76  * fixed a delete bug & small memory leak found by Akio via Insure++ ,
77  * updated SetTable() method for containing idList, corrected enumeration
78  * map to rhic domain for Conditions_rhic database
79  *
80  * Revision 1.13 2000/06/02 13:37:37 porter
81  * built up list of minor changes:
82  * - made buffer more robust for certain null inputs
83  * - fixed small leak in StDbTables & restructure call to createMemory
84  * - added dbRhic as a database domain in StDbDefs
85  * - added setUser() in StDbManager
86  * - added more diagnostic printouts in mysqlAccessor.cc
87  *
88  * Revision 1.12 2000/04/25 18:26:03 porter
89  * added flavor & production time as settable query fields in
90  * table &/or node. Associated SQL updated in mysqlAccessor.
91  * Flavor key supports "+" as an OR symbol.
92  *
93  * Revision 1.11 2000/02/15 20:27:45 porter
94  * Some updates to writing to the database(s) via an ensemble (should
95  * not affect read methods & haven't in my tests.
96  * - closeAllConnections(node) & closeConnection(table) method to mgr.
97  * - 'NullEntry' version to write, with setStoreMode in table;
98  * - updated both StDbTable's & StDbTableDescriptor's copy-constructor
99  *
100  * Revision 1.10 2000/01/27 05:54:35 porter
101  * Updated for compiling on CC5 + HPUX-aCC + KCC (when flags are reset)
102  * Fixed reConnect()+transaction model mismatch
103  * added some in-code comments
104  *
105  * Revision 1.9 2000/01/19 20:20:07 porter
106  * - finished transaction model needed by online
107  * - fixed CC5 compile problem in StDbNodeInfo.cc
108  * - replace TableIter class by StDbTableIter to prevent name problems
109  *
110  * Revision 1.8 2000/01/10 20:37:55 porter
111  * expanded functionality based on planned additions or feedback from Online work.
112  * update includes:
113  * 1. basis for real transaction model with roll-back
114  * 2. limited SQL access via the manager for run-log & tagDb
115  * 3. balance obtained between enumerated & string access to databases
116  * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose
117  * 5. restructured Node model for better XML support
118  *
119  * Revision 1.7 1999/12/03 19:02:01 porter
120  * modified descriptor to accept tableDescriptor once this St_base object
121  * has been updated to have longer name lengths.
122  *
123  * Revision 1.6 1999/10/19 14:30:39 porter
124  * modifications relevant to use with StDbBroker and future merging with
125  * "params" database structure + some docs + suppressing diagnostics messages
126  *
127  * Revision 1.5 1999/09/30 02:06:10 porter
128  * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor)
129  * allow multiple rows (StDbTable), & Added the comment sections at top of
130  * each header and src file
131  *
132  **************************************************************************/
133 #ifndef StDbTable_HH
134 #define StDbTable_HH
135 
136 #include "StDbNode.hh"
137 #include "StDbTime.h"
138 //#include "typeAcceptor.hh"
139 #include "StTableDescriptorI.h"
140 //#include "StDbBufferI.h"
141 #include "StDbStoreInfo.hh"
142 #include <string.h>
143 
144 class StDbBufferI;
145 class StDbBuffer;
146 class typeAcceptor;
147 
148 #ifdef __ROOT__
149 #include "TROOT.h"
150 #endif
151 
152 class StDbTable : public StDbNode {
153 
154 protected:
155 
156 char* mflavor;
157 bool mdefaultFlavor;
158 unsigned int mprodTime;
159 
162 StDbTime mendTime;
163 StDbTime mendStoreTime; // for writing null endTimes
164 
165 // DB storage information
166 char* mstructName;
167 char* melementName;
168 bool mIsBaseLine;
169 bool mIsBinary;
170 bool mIsIndexed;
171 char* mdataTable;
172 
173 int mschemaID;
174 int* melementID;
175 
178 
181 StTableDescriptorI* mdescriptor;
182 
184 char* mdata;
185 int mrows;
186 int mrowNumber;
187 bool mhasData;
188 int mrowsRequested; // for query by where clause
189 unsigned int* mtimeVals;
190 
191  virtual void ReadElement(char*& ptr, char* name, int length, StTypeE type, StDbBuffer* buff);
192  virtual void WriteElement(char* ptr, char* name, int length, StTypeE type, StDbBuffer* buff);
193  virtual void PassOutElement(char* ptr, char* name, int length, StTypeE type, typeAcceptor* accept);
194  virtual void PassInElement(char* ptr, char* name, int length, StTypeE type, typeAcceptor* accept);
195  virtual void getElementSpecs(int elementNum, char*& c, char*& name, unsigned int& length,StTypeE& type);
196 
197  bool createMemory();
198  bool createMemory(int nrows);
199  char* duplicateData();
200  void init();
201  void printNoDataReturned(const char* elementName);
202 
203 
204 public:
205 
206  StDbTable(const char* tableName);
207  StDbTable(const char* tableName, int schemaID);
208  StDbTable(StDbTable& table);
209 
210  virtual ~StDbTable(){ if(melementID) delete [] melementID;
211  if(mdescriptor)delete mdescriptor;
212  if(mdata) delete [] mdata;
213  if(mdataTable) delete [] mdataTable;
214  if(mstructName) delete [] mstructName;
215  if(melementName) delete [] melementName;
216  if(mtimeVals) delete [] mtimeVals;
217  if(mflavor) delete [] mflavor;
218  };
219 
220  virtual bool IsTable() const;
221  virtual void setNodeInfo(StDbNode* node);
222  virtual unsigned int getTableSize() const;
223  virtual char* getCstructName();
224  virtual char* printCstructName();
225  virtual void setCstructName(const char* name);
226  virtual void setDataTable(const char* name);
227  virtual char* getDataTable();
228  virtual char* printDataTable();
229 
230  // flavor is "ofl", "onl", "sim", .... it is an char[16] array.
231  // 16 char is enough to later have "ofl|onl" type syntax
232 
233  virtual char* getFlavor();
234  virtual char* printFlavor();
235  virtual void setFlavor(const char* flavor);
236  virtual bool defaultFlavor() const;
237  virtual void setDefaultFlavor();
238 
239  virtual unsigned int getProdTime();
240  virtual void setProdTime(unsigned int ptime);
241 
242  virtual unsigned int getEndTime() const ;
243  virtual char* getEndDateTime();
244  virtual void setEndTime(unsigned int time);
245  virtual void setEndTime(const char* time);
246 
247  virtual unsigned int getBeginTime() const ;
248  virtual char* getBeginDateTime();
249  virtual void setBeginTime(unsigned int time) ;
250  virtual void setBeginTime(const char* time);
251 
252  virtual unsigned int getEndStoreTime() const;
253  virtual char* getEndStoreDateTime();
254  virtual void setEndStoreTime(unsigned int time);
255  virtual void setEndStoreTime(const char* time);
256 
257  virtual int* getElementID(int& nrows);
258  virtual int getRowID(int rowNumber) const ;
259  virtual char* getElementName();
260  virtual char* printElementName();
261  virtual void setElementName(const char* ename);
262  virtual void setElementID(int* elements, int nrows);
263 
264  virtual void setBaseLine(bool baseLine);
265  virtual void setIndexed(bool indexed);
266  virtual void setBinary(bool abinary);
267  virtual bool IsBaseLine() const;
268  virtual bool IsIndexed() const;
269  virtual bool IsBinary() const;
270 
271  virtual int getSchemaID() const ;
272  virtual void setSchemaID(int id) ;
273 
274  // storage of dataIDs
275  virtual void addWrittenRows(int* dataID,int numRows,bool canRollBack=false);
276  virtual int* getWrittenRows(int& numRows);
277  virtual void commitData();
278  virtual void clearStoreInfo();
279  virtual unsigned int* getTimeValues();
280  virtual unsigned int getMaxTime();
281  virtual void setTimeValues(unsigned int* timeValues);
282 
283  // c-struct descriptor & schema
284  // set by 1st call to db
285 
286  virtual StTableDescriptorI* getDescriptorCpy() const;
287  virtual StTableDescriptorI* getDescriptor();
288  virtual void setDescriptor(StTableDescriptorI* descriptor);
289  virtual bool hasDescriptor() const;
290  void checkDescriptor();
291 
292  // access to date via this table or c-struct
293 
294  virtual StDbTable* Clone();
295  virtual char* GetTable();
296  virtual void* GetTableCpy();
297  virtual void SetTable(char* data, int nrows, int* idList=0);
298  virtual void AddRows(char* data, int nrows);
299  // virtual void AddRowsAt(int rowNumber, char* data, int nrows);
300  virtual int GetNRows() const;
301  virtual void setRowNumber(int row=0);
302  virtual bool hasData() const;
303  virtual void* getDataValue(const char* name,int rowNumber=0);
304 
305  // memory management for query by where clause
306  virtual int getRowLimit() const;
307  virtual void setRowLimit(int nrows);
308  virtual void addNRows(int newRows);
309 
311  virtual void addNElements(int* elements, int newRows);
312  virtual void resizeNumRows(int nrows);
313  virtual void resizeElementID(int numRows);
314 
315  // methods for reading & writing to Db & to file
316  virtual void StreamAccessor(typeAcceptor* accept, bool isReading);
317  virtual void dbStreamer(typeAcceptor* accept, bool isReading);
318  virtual void StreamAccessor(StDbBufferI* buff, bool isReading);
319  virtual void dbStreamer(StDbBufferI* buff, bool isReading);
320  virtual void dbStreamerWrite(StDbBufferI* buff);
321  virtual void dbTableStreamer(StDbBufferI* buff, const char* name, bool isReading);
322 #ifdef __ROOT__
323  ClassDef(StDbTable,0)
324 #endif
325 };
326 
327 inline bool StDbTable::IsTable() const { return true; };
328 inline unsigned int StDbTable::getTableSize() const {
329 if(mhasDescriptor && !mdescriptor->getTrowSize()) return mdescriptor->getTotalSizeInBytes();
330 if(mhasDescriptor) return mdescriptor->getTrowSize();
331 return 0;
332 }
333 inline char* StDbTable::printCstructName() { return mstructName; }
334 inline char* StDbTable::printDataTable() { return mdataTable; }
335 inline char* StDbTable::printFlavor() { return mflavor; }
336 inline bool StDbTable::defaultFlavor() const {return mdefaultFlavor;}
337 inline void StDbTable::setProdTime(unsigned int ptime) { mprodTime=ptime; }
338 inline unsigned int StDbTable::getProdTime() { return mprodTime; }
339 inline unsigned int StDbTable::getEndTime() const {return mendTime.munixTime; }
340 inline char* StDbTable::getEndDateTime() { return mendTime.mdateTime; }
341 inline void StDbTable::setEndTime(unsigned int time){ mendTime.munixTime=time;}
342 inline void StDbTable::setEndTime(const char* time){mendTime.setDateTime(time);}
343 inline unsigned int StDbTable::getBeginTime() const {return mbeginTime.munixTime; }
344 inline char* StDbTable::getBeginDateTime() {return mbeginTime.mdateTime; }
345 inline void StDbTable::setBeginTime(unsigned int time){mbeginTime.munixTime = time; }
346 inline void StDbTable::setBeginTime(const char* time){ mbeginTime.setDateTime(time); }
347 inline unsigned int StDbTable::getEndStoreTime() const { return mendStoreTime.munixTime; }
348 inline char* StDbTable::getEndStoreDateTime() { return mendStoreTime.mdateTime; }
349 inline void StDbTable::setEndStoreTime(unsigned int time) {mendStoreTime.munixTime = time; }
350 inline void StDbTable::setEndStoreTime(const char* time){ mendStoreTime.setDateTime(time); }
351 inline int* StDbTable::getElementID(int& nrows) { nrows = mrows; return melementID; }
352 
353 inline int StDbTable::getRowID(int rowNumber) const {
354  if(rowNumber<mrows)return melementID[rowNumber];
355  return 0;
356 }
357 inline unsigned int* StDbTable::getTimeValues(){ return mtimeVals; }
358 inline void StDbTable::setTimeValues(unsigned int* timeValues){
359  if(mtimeVals) delete [] mtimeVals;
360  mtimeVals=timeValues;
361 }
362 inline unsigned int StDbTable::getMaxTime() {
363  unsigned int retVal=0;
364  for(int i=0; i<mrows;i++)if(mtimeVals[i]>retVal)retVal=mtimeVals[i];
365  return retVal;
366 }
367 inline char* StDbTable::printElementName() { return melementName; }
368 inline int StDbTable::getSchemaID() const { return mschemaID; }
369 inline void StDbTable::setSchemaID(int id) {mschemaID = id; }
370 inline StTableDescriptorI* StDbTable::getDescriptor() { return mdescriptor; }
371 inline bool StDbTable::hasDescriptor() const { return mhasDescriptor; }
372 inline StDbTable* StDbTable::Clone() {return (new StDbTable(*this));}
373 inline int StDbTable::GetNRows() const { return mrows; }
374 inline void StDbTable::setRowLimit(int nrows) { mrowsRequested=nrows; };
375 inline int StDbTable::getRowLimit() const { return mrowsRequested; };
376 inline void StDbTable::setRowNumber(int row){if(row < mrows)mrowNumber = row;}
377 inline bool StDbTable::hasData() const { return mhasData; };
378 inline void StDbTable::setBinary(bool abinary) { mIsBinary=abinary; }
379 inline void StDbTable::setBaseLine(bool baseLine) { mIsBaseLine=baseLine; }
380 inline void StDbTable::setIndexed(bool indexed) { mIsIndexed=indexed; }
381 inline bool StDbTable::IsBinary() const { return mIsBinary; }
382 inline bool StDbTable::IsBaseLine() const { return mIsBaseLine; }
383 inline bool StDbTable::IsIndexed() const { return mIsIndexed; }
384 
385 #endif
StDbStoreInfo mstoredData
for rolling back stores
Definition: StDbTable.h:177
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
bool mhasDescriptor
c-struct descriptor information
Definition: StDbTable.h:180
StDbTime mbeginTime
validity interval
Definition: StDbTable.h:161
char * mdata
data &amp; num of rows
Definition: StDbTable.h:184