StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbModifier.cxx
1 /***************************************************************************
2  *
3  * $Id: StDbModifier.cxx,v 1.11 2016/05/25 20:57:11 dmitry Exp $
4  *
5  * Author: Masashi Kaneta, updated by R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Class to act between Root-Cint files and database
9  *
10  ***************************************************************************
11  *
12  * $Log: StDbModifier.cxx,v $
13  * Revision 1.11 2016/05/25 20:57:11 dmitry
14  * coverity - resource leakage
15  *
16  * Revision 1.10 2015/05/15 19:56:09 dmitry
17  * more cleanup
18  *
19  * Revision 1.9 2015/05/15 19:47:16 dmitry
20  * proper delete added before overwrite
21  *
22  * Revision 1.8 2015/05/15 18:34:39 dmitry
23  * now deallocating memory in destructor of StDbModifier + cleanup
24  *
25  * Revision 1.7 2007/08/20 18:21:29 deph
26  * New Version of Load Balancer
27  *
28  * Revision 1.6 2007/05/16 22:48:10 deph
29  * Replaced cerr with LOG_ERROR <<endm; for logger
30  *
31  * Revision 1.5 2005/09/07 22:04:02 deph
32  * update to correct padding issue for packed tables
33  *
34  * Revision 1.4 2003/09/02 17:57:49 perev
35  * gcc 3.2 updates + WarnOff
36  *
37  * Revision 1.3 2002/03/15 22:08:07 porter
38  * fixed retval in modifier
39  *
40  * Revision 1.2 2002/03/13 22:14:53 porter
41  * added variable length table defautls for simplifying writes of l3 counters to db
42  *
43  * Revision 1.1 2000/08/15 22:51:52 porter
44  * Added Root2DB class from Masashi Kaneta
45  * + made code more robust against requesting data from non-existent databases
46  *
47  **************************************************************************/
48 #ifdef __ROOT__
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 
53 #include "TROOT.h"
54 #include "TInterpreter.h"
55 
56 #ifndef __STDB_STANDALONE__
57 #include "Stiostream.h"
58 #include "StMessMgr.h"
59 #else
60 #define LOG_DEBUG cout
61 #define LOG_INFO cout
62 #define LOG_WARN cout
63 #define LOG_ERROR cerr
64 #define LOG_FATAL cerr
65 #define LOG_QA cout
66 #define endm "\n"
67 #endif
68 
69 
70 #include "TTable.h"
71 
72 #include "StDbManager.hh"
73 #include "StDbConfigNode.hh"
74 #include "StDbTable.h"
75 #include "StDbDefs.hh"
76 #include "StDbTableDescriptor.h"
77 
78 #include "StDbModifier.h"
79 
80 
81 ClassImp(StDbModifier)
82 
83 //_____________________________________________________________________________
84  StDbModifier::StDbModifier() : fDbName(0), fDebug(0), fTableName(0), funixTime(0), fTimestamp(0), fVersionName(0),
85  fOutputFileName(0), fInputFileName(0), fFlavorName(0)
86 {
87  // constructor of StDbModifier
88 
89 
90  // fTimestamp = new char[30];
91  // strcpy(fTimestamp,"2038-01-01 04:59:59");
92  // Timestamp of the data requested.
93  // To get recent one, here, it is assigned as end of UNIX time.
94 
95  //fDebug = 0; // set No-debug mode.
96 
97  //fDbName = 0; // set Database name on DB server as brank.
98  //fTableName = 0; // set Table name on DB server as brank.
99  // If either fDbName or fTableName is still 0
100  // in ReadDataFromBD() and WriteDataToDB(),
101  // the program will be terminated.
102  //fVersionName = 0;
103 
104  //fFlavorName = 0;
105 
106  fOutputFileName = new char[200];
107  strcpy(fOutputFileName,"./database_data.C");
108  // default name of a file to be filled data from database.
109 
110  fInputFileName = new char[200];
111  strcpy(fInputFileName,"./database_data.C");
112  // default name of a file to be read data from ascii file.
113 }
114 
115 //_____________________________________________________________________________
116 StDbModifier::~StDbModifier()
117 {
118  // destructor of StDbModifier
119  delete [] fTimestamp;
120  delete [] fDbName;
121  delete [] fTableName;
122  delete [] fVersionName;
123  delete [] fFlavorName;
124  delete [] fOutputFileName;
125  delete [] fInputFileName;
126 }
127 
128 //_____________________________________________________________________________
129 Int_t StDbModifier::ReadDataFromDB()
130 {
131  // This funciton will connect database server, get informaion of table and
132  // make a file that contains data of table in database.
133 
134  if( funixTime==0 && !(fTimestamp)) {
135  cout<< " No timestamp specified " << endl;
136  return 0;
137  }
138 
139  if ( fDbName == 0 ){
140  cout << " StDbModifier; Set Database name by " << endl;
141  cout << " StDbModifier::setDbName(TString dbname)";
142  cout << endl;
143  return 0;
144  }
145  if ( fTableName == 0 ){
146  cout << " StDbModifier; Set table name by " << endl;
147  cout << " StDbModifier::setTableName(TString tablename)";
148  cout << endl;
149  return 0;
150  }
151 
152  StDbManager* mgr = StDbManager::Instance(); // Get the singleton manager
153  if ( fDebug == 1 ){
154  bool isVerbose = true;
155  mgr->setVerbose(isVerbose); // Set verbose mode for debuging for fDebug=1
156  }
157 
158  StDbConfigNode* configNode = mgr -> initConfig(fDbName); // Connect to the db & get an empty container
159 
160  StDbTable* dbtable;
161  if ( fVersionName == 0 ){
162  dbtable = configNode -> addDbTable(fTableName);
163  // Add a table to the container with descriptor given by Database with wsing version name as "default"
164  }else{
165  dbtable = configNode -> addDbTable(fTableName,fVersionName);
166  // Add a table to the container with descriptor given by Database with wsing version name specified .
167  }
168  if ( dbtable == 0 ){ // If table asigned by fTableName does not exist in Dababase
169  cout << " No Table : " << fTableName << endl; // program is stoped and exit from this function.
170  return 0; //
171  }
172 
173  if ( fFlavorName != 0 ){
174  dbtable -> setFlavor(fFlavorName);
175  cout << "Flavor is set as " << fFlavorName << " by StDbTable::setFlavor." << endl;
176  }else{
177  cout << "Flavor is NOT assigned. Default value is set as 'ofl'. " << endl;
178  dbtable -> setFlavor("ofl");
179  }
180 
181 
182  if(funixTime) {
183  mgr->setRequestTime(funixTime);
184  } else {
185  mgr->setRequestTime(fTimestamp);
186  }
187 
188  // mgr -> setRequestTime(fTimestamp); // Set the request time
189  mgr -> fetchDbTable(dbtable); // Fetch the data from Database
190 
191  void* cstruct = dbtable -> GetTableCpy(); // Get pointer of table and copy to c-structure
192  Int_t nrows = dbtable -> GetNRows(); // Get number of raws in the table
193 
194  TTable* table = TTable::New(fTableName,fTableName,cstruct,nrows);
195  // Create new TTable object for c-structure
196 
197  ofstream ofs(fOutputFileName); // Open a file
198  table -> SavePrimitive(ofs,0); // Write information of c-structure from object of TTable to the file
199  ofs.close(); // Close the file
200 
201  return 1;
202 }
203 
204 
205 //_____________________________________________________________________________
206 Int_t StDbModifier::WriteDataToDB()
207 {
208  // This funciton will make a table as object of TTable.
209  // Data of the table will be read from a file that is generated from
210  // StDbModifier::ReadDataFromDB().
211  // After data is read, connection to database server is made and data will
212  // be stored in database.
213 
214 
215  if( funixTime==0 && !(fTimestamp)) {
216  cout<< " No timestamp specified " << endl;
217  return 0;
218  }
219 
220  if ( fDbName == 0 ){
221  cout << " StDbModifier; Set Database name by " << endl;
222  cout << " StDbModifier::setDbName(TString dbname)";
223  cout << endl;
224  return 0;
225  }
226  if ( fTableName == 0 ){
227  cout << " StDbModifier; Set table name by " << endl;
228  cout << " StDbModifier::setTableName(TString tablename)";
229  cout << endl;
230  return 0;
231  }
232 
233  TString command; // Make a string to load Root C macro generated
234  command = ".L "; // by ReadDataFromDB()
235  command += fInputFileName;
236 
237  if ( fDebug == 1 ) LOG_INFO<<" LoadTable: "<<(const char*)command<<endm;
238 
239  gInterpreter->ProcessLine(command); // Load the file in CINT
240  TTable* table = (TTable*) gInterpreter->Calc("CreateTable()"); // execute comand in the file
241  command.ReplaceAll(".L ",".U "); // chage comand .L to .U
242  gInterpreter->ProcessLine(command); // unload file from CINT
243 
244  void* cstruct = table -> GetArray(); // Get pointer of table and copy to c-structure
245  Int_t nrows = table -> GetNRows(); // Get number of raws in the table
246  //MPD - below is need TTable info to correct c-array 8 byte padding issue
247  //could have broke it out - but I chose to keep all the TTable (root) stuff together
248 
249  Int_t rowSize = table -> GetRowSize(); // Get the size (in bytes) of each row - fixes gap/padding problem
250 // Int_t nCols = table -> GetNumberOfColumns();
251 
253  TD->storeRowSize(rowSize);
254  //table -> Print(0,2); //uncomment for debugging
255 
256 
257 
258  StDbManager* mgr = StDbManager::Instance(); // Get the singleton manager
259  if ( fDebug == 1 ){
260  bool isVerbose = true;
261  mgr->setVerbose(isVerbose); // Set verbose mode for debuging for fDebug=1
262  }
263  StDbConfigNode* configNode = mgr -> initConfig(fDbName); // Connect to the db & get an empty container
264 
265  StDbTable* dbtable;
266  if ( fVersionName == 0 ){
267  dbtable = configNode -> addDbTable(fTableName);
268  // Add a table to the container with descriptor given by Database with wsing version name as "default"
269  }else{
270  dbtable = configNode -> addDbTable(fTableName,fVersionName);
271  // Add a table to the container with descriptor given by Database with wsing version name specified .
272  }
273 
274 
275 
276  if ( dbtable == 0 ){ // If table asigned by fTableName does not exist in Dababase,
277  cout << " No Table : " << fTableName << endl; // program is stoped and exit from this function.
278  delete TD;
279  return 0; //
280  }
281 
282  if ( fFlavorName != 0 ){
283  cout << "set Flavor" << endl;
284  dbtable -> setFlavor(fFlavorName);
285  cout << "Flavor is set as " << fFlavorName << " by StDbTable::setFlavor." << endl;
286  }else{
287  cout << "Flavor is NOT assigned. Default value is set as 'ofl'. " << endl;
288  dbtable -> setFlavor("ofl");
289  }
290 
291  int dbnrows=dbtable->GetNRows();
292  int* eidList=0;
293  int tmp;
294  int* dbeidList=dbtable->getElementID(tmp);
295  if(nrows>dbnrows){
296  eidList=new int[nrows];
297  int i;
298  for(i=0;i<dbnrows;i++)eidList[i]=dbeidList[i];
299  for(i=dbnrows;i<nrows;i++)eidList[i]=eidList[dbnrows-1]+i;
300  }
301 
302 
303  dbtable -> SetTable((char*)cstruct,nrows,eidList); // Put data in local table on memory
304  // dbtable -> SetNRows(nrows); // Set number of rows on table in database
305 
306  // mgr -> setStoreTime(fTimestamp); // Set the time stamp
307  if(funixTime) {
308  mgr->setStoreTime(funixTime);
309  } else {
310  mgr->setStoreTime(fTimestamp);
311  }
312 
313  int retVal=0;
314  if(mgr -> storeDbTable(dbtable)) retVal=1; // Fetch the data
315  delete [] eidList;
316  delete TD;
317 
318  return retVal;
319 }
320 
321 //_____________________________________________________________________________
322 void StDbModifier::SetDateTime(const char* timestamp)
323 {
324  delete [] fTimestamp;
325  fTimestamp = new char[strlen(timestamp)+1];
326  strcpy(fTimestamp,timestamp);
327 }
328 
329 void StDbModifier::SetTime(unsigned int time){ funixTime=time;}
330 
331 //_____________________________________________________________________________
332 void StDbModifier::SetDbName(const char* dbname)
333 {
334  delete [] fDbName;
335  fDbName = new char[strlen(dbname)+1];
336  strcpy(fDbName,dbname);
337 }
338 
339 //_____________________________________________________________________________
340 void StDbModifier::SetInputFileName(const char* inputfilename)
341 {
342  delete [] fInputFileName;
343  fInputFileName = new char[strlen(inputfilename)+1];
344  strcpy(fInputFileName,inputfilename);
345 }
346 
347 //_____________________________________________________________________________
348 void StDbModifier::SetOutputFileName(const char* outputfilename)
349 {
350  delete [] fOutputFileName;
351  fOutputFileName = new char[strlen(outputfilename)+1];
352  strcpy(fOutputFileName,outputfilename);
353 }
354 
355 //_____________________________________________________________________________
356 void StDbModifier::SetTableName(const char* tablename)
357 {
358  delete [] fTableName;
359  fTableName = new char[strlen(tablename)+1];
360  strcpy(fTableName,tablename);
361 }
362 
363 //_____________________________________________________________________________
364 void StDbModifier::SetVersionName(const char* versionname)
365 {
366  delete [] fVersionName;
367  fVersionName = new char[strlen(versionname)+1];
368  strcpy(fVersionName,versionname);
369 }
370 
371 //_____________________________________________________________________________
372 void StDbModifier::SetFlavor(const char* flavorname)
373 {
374  delete [] fFlavorName;
375  fFlavorName = new char[strlen(flavorname)+1];
376  strcpy(fFlavorName,flavorname);
377  cout << " Flavor is set " << flavorname << endl;
378  cout << " Flavor is set " << fFlavorName << endl;
379 }
380 
381 #endif
static TTable * New(const Char_t *name, const Char_t *type, void *array, UInt_t size)
This static method creates a new TTable object if provided.
Definition: TTable.cxx:1515
Definition: TTable.h:48
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155