StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DbRead.cxx
1 /***************************************************************************
2  *
3  * $Id: DbRead.cxx,v 1.10 2016/05/24 18:02:27 dmitry Exp $
4  *
5  * Author: S. Vanyashin
6  * Updated by: R. Jeff Porter
7  ***************************************************************************
8  *
9  * Description: Temporary C-code to call the C++ DB-API
10  *
11  *
12  ***************************************************************************
13  *
14  * $Log: DbRead.cxx,v $
15  * Revision 1.10 2016/05/24 18:02:27 dmitry
16  * final fix for StDbBroker
17  *
18  * Revision 1.9 2015/05/21 18:29:06 dmitry
19  * small memory leak and type conversion warnings fixed
20  *
21  * Revision 1.8 2007/05/16 22:47:54 deph
22  * Replaced cerr with LOG_ERROR <<endm; for logger
23  *
24  * Revision 1.7 2001/01/22 18:40:24 porter
25  * Added a wrapper for StMessage so one can use it in StDbLib
26  *
27  * Revision 1.6 2000/04/13 20:22:56 porter
28  * - reconnected tableDescriptor that had been broken via St_tableDescriptor.
29  * - added unix timestamp as standard
30  * - top node returned via InitConfig will be a database type
31  *
32  * Revision 1.5 2000/01/10 20:31:16 porter
33  * modified StDbBroker to be an interface to the DB-interface, StDbLib.
34  * - old functionality is retained for the short-term & modifications
35  * are extensions
36  *
37  *
38  **************************************************************************/
39 // #include <stdio.h>
40 #include <stdlib.h>
41 // #include <string.h>
42 
43 #include "StDbLib/StDbManager.hh"
44 #include "StDbBroker.h"
45 #include "StDbLib/StDbDefs.hh"
46 #include "StDbLib/StDbConfigNode.hh"
47 #include "StDbLib/StDbTable.h"
48 #include "StDbLib/StDbTableDescriptor.h"
49 #include "StMessMgr.h"
50 
51 // modified for general access
52 
53 extern "C" void * DbRead(unsigned int *nRows,
54  unsigned int *datetime,
55  const char * tableName,
56  const char * structName,
57  unsigned int nVar,
58  unsigned int sizeOfStruct,
60  const char* database,
61  const char* tableVersion)
62 {
63 
64 
65 char validFrom[20];
66 char validTo[20];
67 char temps[128];
68 char row[128];
69 char currentDateTime[20];
70 char time[7];
71 
72 sprintf(currentDateTime,"%.8d",(int)datetime[0]);
73 sprintf(time,"%.6d",(int)datetime[1]);
74 strcat(currentDateTime,time);
75 
76 currentDateTime[19]='\0';
77 currentDateTime[18]=currentDateTime[13];
78 currentDateTime[17]=currentDateTime[12];
79 currentDateTime[16]=':';
80 currentDateTime[15]=currentDateTime[11];
81 currentDateTime[14]=currentDateTime[10];
82 currentDateTime[13]=':';
83 currentDateTime[12]=currentDateTime[9];
84 currentDateTime[11]=currentDateTime[8];
85 currentDateTime[10]=' ';
86 currentDateTime[9]=currentDateTime[7];
87 currentDateTime[8]=currentDateTime[6];
88 currentDateTime[7]='-';
89 currentDateTime[6]=currentDateTime[5];
90 currentDateTime[5]=currentDateTime[4];
91 currentDateTime[4]='-';
92 
93 //cout<<"currentDateTime: \""<<currentDateTime<<"\""<<endl;
95 
96 
97 char dbType[64];
98 char dbDomain[64];
99 StDbType type;
100 StDbDomain domain;
101 char version[128];
102 
103 if(!tableVersion)strcpy((char*)version,"default");
104  if(!database){
105  strcpy((char*)dbType,"TestScheme");
106  strcpy((char*)dbDomain,"Star");
107  type = mgr->getDbType(dbType);
108  domain = mgr->getDbDomain(dbDomain);
109  } else {
110  char* atype;
111  char* adomain;
112  if(!mgr->getDataBaseInfo(database, atype, adomain)){
113  LOG_ERROR << "StDbManager:: Database specified incorrectly" << endm;
114  *nRows=0;
115  return NULL;
116  }
117  // cout << "Returned DbType = "<<atype<<" & DbDomain= "<<adomain<< endl;
118  type = mgr->getDbType(atype);
119  domain = mgr->getDbDomain(adomain);
120 
121  }
122 
123 
124 StDbConfigNode* node=mgr->initConfig(type,domain);
125 // now try without descriptor --> can't
126 StDbTable* mtable=node->addDbTable(tableName,version);
127 //StDbTable* mtable=node->addTable(tableName,version);
128 
129  if(!mtable){
130  *nRows=0;
131  return NULL;
132  }
133 
134 
135  // --> can't do it 'cause St_base descriptor's limits
136  //_Descriptor* _d = (_Descriptor*)d;
137  //StDbTableDescriptor* dbDescr = new StDbTableDescriptor(_d,nVar,sizeOfStruct);
138  //mtable->setDescriptor((StTableDescriptorI*)dbDescr);
139 
140 mgr->setRequestTime(currentDateTime);
141 mgr->fetchDbTable(mtable);
142 
143 
144 if (mtable->GetTable()==NULL)
145  {
146  *nRows=0;
147  return NULL;
148  }
149 
150 *nRows = mtable->GetNRows();
151 
152 // cout<<"DbRead: nRows"<<*nRows<<endl;
153 
154 //fill return datetime values
155 int latestDirDate;
156 int latestDirTime;
157  int ic;
158  int i2, i3;
159 
160 //convert hhmmss from: 1999-06-17 12:48:33
161  strncpy(row,mtable->getBeginDateTime(),19); row[19] = '\0';
162 
163  strncpy(validFrom,row,19);validFrom[19]='\0';
164  //start from blank at position row[0][10]
165  ic=10;
166  for(i3=0;i3<3;i3++,++ic) {
167  for(i2=0;i2<2;i2++,++ic) {
168  temps[ic]=row[ic];
169  }
170  }
171  temps[6]='\0';
172  latestDirTime = atoi(temps);
173 
174  //get date from: 1999-06-17 12:48:33
175 
176  strncpy(temps,validFrom,10);
177  temps[4]=temps[5];
178  temps[5]=temps[6];
179  temps[6]=temps[8];
180  temps[7]=temps[9];
181  temps[8]='\0';
182 
183  latestDirDate = atoi(temps);
184 
185 int nextDirDate;
186 int nextDirTime;
187 
188 //convert hhmmss from: 1999-06-17 12:48:33
189  strncpy(row,mtable->getEndDateTime(),19); row[19] = '\0';
190  strncpy(validTo,row,19);validTo[19]='\0';
191  //start from blank at position row[0][10]
192  ic=10;
193  for(i3=0;i3<3;i3++,++ic) {
194  for(i2=0;i2<2;i2++,++ic) {
195  temps[ic]=row[ic];
196  }
197  }
198  temps[6]='\0';
199  nextDirTime = atoi(temps);
200 
201  //get date from: 1999-06-17 12:48:33
202  strncpy(temps,validTo,10);
203  temps[4]=temps[5];
204  temps[5]=temps[6];
205  temps[6]=temps[8];
206  temps[7]=temps[9];
207  temps[8]='\0';
208 
209  nextDirDate = atoi(temps);
210 
211  datetime[0] = latestDirDate;
212  datetime[1] = latestDirTime;
213  datetime[2] = nextDirDate;
214  datetime[3] = nextDirTime;
215 
216  if (datetime[2]==19691231) datetime[2]=20380101;
217 
218  void* data = mtable->GetTableCpy();
219  delete node;
220 
221 
222 return data;
223 }
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
void version(std::ostream &os=std::cout)
print HepMC version
Definition: Version.h:27
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155