StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbBroker.h
1 /***************************************************************************
2  *
3  * $Id: StDbBroker.h,v 1.33 2016/05/24 17:44:16 dmitry Exp $
4  *
5  * Author: S. Vanyashin, V. Perevoztchikov
6  * Updated by: R. Jeff Porter
7  ***************************************************************************
8  *
9  * Description: Offline Interface from the Offline Maker interface to the
10  * Database interface.
11  *
12  ***************************************************************************
13  *
14  * $Log: StDbBroker.h,v $
15  * Revision 1.33 2016/05/24 17:44:16 dmitry
16  * first batch of fixes for Coverity findings
17  *
18  * Revision 1.32 2011/11/28 17:03:07 dmitry
19  * dbv override support in StDbLib,StDbBroker,St_db_Maker
20  *
21  * Revision 1.31 2011/02/10 17:31:01 dmitry
22  * added an option to blacklist domains
23  *
24  * Revision 1.30 2008/01/15 20:37:44 deph
25  * Removed DbFill and corresponding calls from StDbBroker
26  *
27  * Revision 1.29 2007/09/10 02:36:08 perev
28  * StDbBroker::Release added
29  *
30  * Revision 1.28 2005/08/29 21:43:15 fisyak
31  * replace UInt_t to Int_t for m_runNumber to avoid problem with undefined run no.
32  *
33  * Revision 1.27 2004/07/14 18:46:51 perev
34  * UInt=>Int for new ROOT
35  *
36  * Revision 1.26 2003/01/08 19:43:10 perev
37  * CleanUp
38  *
39  * Revision 1.25 2002/01/15 17:15:36 porter
40  * moved timestamp translation to a separate method
41  *
42  * Revision 1.24 2001/10/30 20:43:29 porter
43  * timestamp set for failure on query by runNumber
44  *
45  * Revision 1.23 2001/10/26 16:36:17 porter
46  * more runNumber implementation
47  *
48  * Revision 1.22 2001/10/26 15:44:02 porter
49  * add query by runNumber
50  *
51  * Revision 1.21 2001/10/24 04:05:56 porter
52  * added zombie designation per Victor's suggestion
53  *
54  * Revision 1.20 2001/09/13 16:54:54 porter
55  * propogate falvor by table through the brokery
56  *
57  * Revision 1.19 2001/01/22 18:40:25 porter
58  * Added a wrapper for StMessage so one can use it in StDbLib
59  *
60  * Revision 1.18 2000/08/15 22:53:14 porter
61  * Added 2 write methods.
62  * - 1 works once "list" is requested from database
63  * - 1 works just by specifying the full path from which
64  * the code extracts the database name.
65  *
66  * Revision 1.17 2000/06/30 02:00:42 porter
67  * fixed memory leak introduced when making sure top level returned to
68  * offline is always a database type name
69  *
70  * Revision 1.16 2000/06/14 13:39:05 didenko
71  * Add ClassDef/ClassImp
72  *
73  * Revision 1.15 2000/04/25 18:27:48 porter
74  * Added flavor and production time as query fields to pass to db-api
75  *
76  * Revision 1.14 2000/04/14 14:46:41 fine
77  * new method for Victor has been introduced
78  *
79  * Revision 1.13 2000/04/13 20:22:57 porter
80  * - reconnected tableDescriptor that had been broken via St_tableDescriptor.
81  * - added unix timestamp as standard
82  * - top node returned via InitConfig will be a database type
83  *
84  * Revision 1.12 2000/03/26 16:47:13 fine
85  * Adjusted to ROOT 2.24
86  *
87  * Revision 1.11 2000/02/28 15:24:20 porter
88  * add more StDbLib methods to broker: this time, StDbManager::closeAllConnections()
89  *
90  * Revision 1.10 2000/01/31 17:11:18 porter
91  * fix break caused by the interaction design between
92  * 'StRoot/St_base/tableDescriptor.h' & 'StDbBroker::Descriptor'
93  * Now StDbBroker::Descriptor==tableDescriptor_st
94  * And StDbBroker::GetTableDescriptor() returns abstract StTableDescriptorI*
95  * Interface to StDbLib is (and was) handle correctly.
96  * StDbBroker is now tied to StRoot/St_base via tableDescriptor.h
97  * No problems would have occured if St_base interactions were based
98  * on StTableDesciptorI in the first place.
99  *
100  * Revision 1.9 2000/01/27 20:30:40 porter
101  * cleaned up dtor & error logic
102  *
103  * Revision 1.8 2000/01/14 14:49:10 porter
104  * set verbose level for checking, added $Id & $Logs, & made node container
105  * more robust for interactions with StDbLib
106  *
107  * Revision 1.7 2000/01/10 20:31:16 porter
108  * modified StDbBroker to be an interface to the DB-interface, StDbLib.
109  * - old functionality is retained for the short-term & modifications
110  * are extensions
111  *
112  *
113  **************************************************************************/
114 //
115 
116 #ifndef STAR_StDbBroker
117 #define STAR_StDbBroker
118 
119 #include "Rtypes.h"
120 #include "dbNodeArray.h"
121 #include "dbConfig.h"
122 //
123 // --> had to remove independence from 'St_base'
124 // since St_base doesn't know interface
125 //
126 // Now Broker uses StDbLib's descriptor interface
127 // and St_base's concrete descriptor.
128 //
129 //#include "tableDescriptor.h"
130 #include "St_tableDescriptor.h"
131 
132 #include <map>
133 #include <utility>
134 
135 class StDbConfigNode;
136 class StDbManager;
137 class StTableDescriptorI;
138 class StDbTable;
139 
140 /* needed for GetComments only */
141 class TTable;
142 /*this is a temporary quick-and-dirty class for db access*/
143 
144 class StDbBroker {
145  public:
146  enum EColumnType {kNAN, kFloat, kInt, kLong, kShort, kDouble, kUInt
147  ,kULong, kUShort, kUChar, kChar };
148 
150 
151 //--> for Sasha's (now obsolete?) Fill & Use routines
153  char fColumnName[32]; /* The name of this data-member: */
154  unsigned int fIndexArray[3]; /* The array of the sizes for each dimen*/
155  unsigned int fOffset; /* The first byte in the row of this column */
156  unsigned int fSize; /* The full size of the selected column in bytes */
157  unsigned int fTypeSize; /* The type size of the selected column in byte */
158  unsigned int fDimensions;/* The number of the dimensions for array */
159  Int_t fType; /* The data type of the selected column */
160 };
161 
162  protected:
163  StDbTable* m_node = 0;
164  oldDescriptor *m_descriptor = 0;
165  Descriptor *mdescriptor = 0;
166  Char_t * m_structName = 0; //name of the struct type used in this TTable
167  Char_t * m_tableName = 0; //name of this instance of TTable
168  UInt_t m_sizeOfStruct = 0;// byte size of this struct
169  UInt_t m_nElements = 0; // Number of variables in the structure
170  UInt_t m_nRows = 0; // number of rows in the table
171 
172  Int_t m_DateTime[2]; // Current DateTime
173 
174  Int_t m_BeginDate = 0; // begin date: 2021 05 31
175  Int_t m_BeginTime = 0; // begin date: HH MM SS
176  Int_t m_EndDate = 0; // end date
177  Int_t m_EndTime = 0; // end time
178 
179  UInt_t m_beginTimeStamp = 0; // unix beginTime
180  UInt_t m_endTimeStamp = 0; // unix endTime
181  UInt_t m_requestTimeStamp = 0; // unix requestTime
182 
183  Int_t m_runNumber = 0; // run number of queries of runlog
184 
185  char* m_tableVersion = 0; // name of the version of the table
186  char* m_database = 0; // name of the database for this table
187  char* m_ParentType = 0; // named dbType when "top" db is domain-level
188 
189  int m_isVerbose = 0;
190  dbNodeArray *m_Nodes = 0;
191  StDbConfigNode* m_Tree = 0;
192 
193  char* m_flavor = 0;
194  unsigned int m_prodTime = 0;
195  std::map<std::pair<char*,char*>,unsigned int> m_prodTimeOverride; // DBV override for specific subsystems
196 
197  dbConfig_st* buildConfig(int& numRows);
198  int buildNodes(StDbConfigNode* node, int pID);
199 
200  Bool_t m_isZombie = false;
201 
202  void makeDateTime(const char* dateTime, Int_t & iDate, Int_t & iTime);
203 
204  public:
205 
206  StDbBroker();
207  virtual ~StDbBroker();
208 
209  // int Init(const char *dbname);// return 0 if OK
210 
211  void * Use();
212  void SetTableFlavor(const char* flavor, int tabID, int parID);
213  void * Use(int tabID, int parID);
214  bool UseRunLog(StDbTable* table);
215 
216  char **GetComments(St_Table *parentTable);
217  // void Fill(void * pArray, const char **ElementComment);
218 
219  // Write Into Database methods
220  // with tabID -> assumes StDbBroker::InitConfig() has been called
221  // which returns the list of table names with unique tabID association.
222  Int_t WriteToDb(void* pArray, int tabID);
223 
224  // with fullPath -> find the database from the path. Table information
225  // is requested from db but over-ridden by input options (idList).
226  // This method will be slow for writing several tables but speed of writes
227  // should not be an issue.
228  Int_t WriteToDb(void* pArray, const char* fullPath, int* idList=0);
229 
230  StDbTable* findTable(const char* databaseName);
231 
232  UInt_t GetNRows() {return m_nRows; }
233  Int_t GetBeginDate() {return m_BeginDate; }
234  Int_t GetBeginTime() {return m_BeginTime; }
235 const char *GetFlavor();
236  Int_t GetEndDate() {return m_EndDate; }
237  Int_t GetEndTime() {return m_EndTime; }
238  UInt_t GetRequestTimeStamp() {return m_requestTimeStamp; }
239  UInt_t GetBeginTimeStamp() {return m_beginTimeStamp; }
240  UInt_t GetEndTimeStamp() {return m_endTimeStamp; }
241  UInt_t GetProdTime() {return m_prodTime;}
242  Bool_t IsZombie() {return m_isZombie; }
243 
244 
245  StTableDescriptorI* GetTableDescriptor();
246 
247  void loadOldDescriptor(){};
248  static const Char_t * GetTypeName( EColumnType type) {
249  switch (type)
250  {
251  case kFloat: return "float";
252  case kInt: return "int";
253  case kLong: return "long";
254  case kShort: return "short";
255  case kDouble: return "double";
256  case kChar: return "char";
257  //temporarily
258  case kUInt: return "int";
259  case kULong: return "long";
260  case kUShort: return "short";
261  case kUChar: return "char";
262 
263  case kNAN: return "";
264  default: return "";
265  }
266  };
267 
268  void SetDateTime(Int_t date,Int_t time);
269  void SetRunNumber(Int_t runNumber) {m_runNumber=runNumber;};
270  void SetDictionary(UInt_t nElements, Descriptor *D)
271  {m_nElements=nElements; mdescriptor = D;}
272  void SetDictionary(Descriptor *D)
273  {if (D) { mdescriptor = D;m_nElements=D->NumberOfColumns();}}
274  void SetTableName(const Char_t *table_name)
275  {if(m_tableName) delete [] m_tableName;
276  m_tableName=new char[strlen(table_name)+1];
277  strcpy(m_tableName,table_name);};
278  void SetStructName(const Char_t *struct_name)
279  {if(m_structName) delete [] m_structName;
280  m_structName=new char[strlen(struct_name)+1];
281  strcpy(m_structName,struct_name);};
282  void SetVersionName(const char *version)
283  {if(m_tableVersion) delete [] m_tableVersion;
284  m_tableVersion= new char[strlen(version)+1];
285  strcpy(m_tableVersion,version);}
286  void SetDataBaseName(const char *dbName)
287  {if(m_database) delete [] m_database;
288  m_database= new char[strlen(dbName)+1];
289  strcpy(m_database,dbName);}
290 
291  void SetStructSize(UInt_t size) {m_sizeOfStruct=size; };
292  void SetNRows(UInt_t nRows) {m_nRows = nRows; }
293  void SetBeginDate(Int_t BeginDate) {m_BeginDate = BeginDate;}
294  void SetBeginTime(Int_t BeginTime) {m_BeginTime = BeginTime;}
295  void SetEndDate(Int_t EndDate) {m_EndDate = EndDate; }
296  void SetEndTime(Int_t EndTime) {m_EndTime = EndTime; }
297  void SetRequestTimeStamp(UInt_t utime) {m_requestTimeStamp = utime; }
298  void SetBeginTimeStamp(UInt_t utime) {m_beginTimeStamp = utime; }
299  void SetEndTimeStamp(UInt_t utime) {m_endTimeStamp = utime; }
300  void SetProdTime(UInt_t ptime);
301  void AddProdTimeOverride(UInt_t ptime, char* dbType = 0, char* dbDomain = 0); // DBV override
302  void SetFlavor(const char* flavor);
303  void SetZombie(Bool_t zombie) { m_isZombie=true; }
304 
305  void addBlacklistedDomain(const char* domainName);
306 
307  static int DbInit(const char *); //Sasha's dbInit
308  void setVerbose(int isVerbose) { m_isVerbose = isVerbose; }
309  void printStatistics();
310  void CloseAllConnections();
311  void Release();
312 
313  //-> here's all the real stuff now
314  dbConfig_st* InitConfig(const char* configName, int& numRows, char* versionName=0);
315  StDbManager* mgr;
316 
317  ClassDef(StDbBroker,0)
318 };
319 
320 // The old C-functions from Sasha's prototype & 1st integration
321 //extern "C" void DbFill(unsigned int *, //datetime[4]
322 // const char *, //tableName
323 // const char *, //StructName
324 // unsigned int, //nVar
325 // StDbBroker::oldDescriptor *d,
326 // const char **, //Comments
327 // unsigned int, //nRows
328 // unsigned int, //sizeOfStruct
329 // void *); //pData
330 
331 extern "C" void *DbUse(unsigned int*, //&nRows,
332  unsigned int *, //datetime[4]
333  const char *, //tableName
334  const char *, //StructName
335  unsigned int, //nVar
336  unsigned int, //sizeOfStruct
338 
339 extern "C" void *DbRead(unsigned int*, //&nRows,
340  unsigned int *, //datetime[4]
341  const char *, //tableName
342  const char *, //StructName
343  unsigned int, //nVar
344  unsigned int, //sizeOfStruct
346  const char*, // database Name
347  const char*); // versionName
348 
349 extern "C" int DbInit(const char *); //dbInit
350 
351 #endif
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
Definition: TTable.h:48