8 #include "StDbManager.hh"
10 #include "StDbConfigNode.hh"
11 #include "StDbTable.h"
12 #include "StDbXmlReader.h"
13 #include "StDbXmlWriter.h"
14 #include "StDbElementIndex.hh"
30 const char *cvsRevision=
"$Revision: 1.1 $";
31 const char *keyToEverything =
"jas";
32 const char *dbTimeFormat[] = {
42 enum EEmcDbAction action = GetDB;
46 char *dbExpTime = NULL;
48 char *dbFlavor =
"ofl";
49 char *dbComment = getenv(
"USER");
50 char *dbName =
"Calibrations_eemc";
51 int dataOnlyMode =
false;
52 int debugMode =
false;
55 getDbIO(
const char *preNode,
const char *node,
const int ndata)
59 if (!strncmp(node,
"eemcPMTconf" ,EEmcDbMaxDbPathLen))
61 else if(!strncmp(node,
"eemcADCconf" ,EEmcDbMaxDbPathLen))
63 else if(!strncmp(node,
"eemcBoxTconf",EEmcDbMaxDbPathLen))
65 else if(!strncmp(node,
"eemcPMTcal" ,EEmcDbMaxDbPathLen))
67 else if(!strncmp(node,
"eemcPMTname" ,EEmcDbMaxDbPathLen))
69 else if(!strncmp(node,
"eemcPMTped" ,EEmcDbMaxDbPathLen))
71 else if(!strncmp(node,
"eemcPMTstat" ,EEmcDbMaxDbPathLen))
73 else if(!strncmp(node,
"eemcPIXcal" ,EEmcDbMaxDbPathLen))
75 else if(!strncmp(node,
"eemcPIXname" ,EEmcDbMaxDbPathLen))
78 else if(!strncmp(node,
"eemcCrateConf" ,EEmcDbMaxDbPathLen))
81 else if(!strncmp(node,
"eemcVaria" ,EEmcDbMaxDbPathLen))
85 else if(!strncmp(node,
"eemcPMTchar" ,EEmcDbMaxDbPathLen))
87 else if(!strncmp(node,
"eemcCWchar" ,EEmcDbMaxDbPathLen))
90 else if(!strncmp(node,
"eemcHVsys" ,EEmcDbMaxDbPathLen))
92 else if(!strncmp(node,
"eemcHVtemp" ,EEmcDbMaxDbPathLen))
95 else if(!strncmp(node,
"eemcRunConfig",EEmcDbMaxDbPathLen))
98 else if(!strncmp(node,
"cdev" ,EEmcDbMaxDbPathLen) ||
99 (!strncmp(preNode,
"online",EEmcDbMaxDbPathLen)) && (
100 (!strncmp(node,
"ETOW" ,EEmcDbMaxDbPathLen)) ||
101 (!strncmp(node,
"ESMD" ,EEmcDbMaxDbPathLen)) ||
102 (!strncmp(node,
"HVsys" ,EEmcDbMaxDbPathLen)) ||
103 (!strncmp(node,
"testBlobS" ,EEmcDbMaxDbPathLen)) ) )
107 fprintf(stderr,
"table %s/%s unknown to %s\n",preNode,node,argv0);
113 fprintf(stderr,
"table %s unknown to %s\n",node,argv0);
124 if(node==NULL)
return items;
125 for(
int i=0;i<level;i++) fprintf(out,
"\t");
127 if(level>0) fprintf(out,
"%s/\n",node->printName());
128 if( node->hasData() ) {
131 while( ( t = ti->next() ) ) {
132 for(
int i=0;i<=level;i++) fprintf(out,
"\t");
133 fprintf(out,
"%s",t->printName());
134 fprintf(out,
":%s",t->getCstructName());
139 (void) printTree(out,node->getFirstChildNode(),level+1);
140 (void) printTree(out,node->getNextNode() ,level );
149 FILE *null = fopen(
"/dev/null",
"w");
151 time_t tUnix=getTimeStamp(dbTimeFormat,dbTime);
153 fprintf(out,
"\nRec BeginDate (BNL) UnixTime ValidPeriod Comment\n");
155 mgr->setRequestTime(tUnix);
156 int ret = mgr->fetchDbTable(dbTab);
160 io->setData(dbTab->GetTable());
163 time_t tdiff= dbTab->getEndTime()- dbTab->getBeginTime();
164 int sec = tdiff % 60; tdiff /= 60;
165 int min = tdiff % 60; tdiff /= 60;
166 int hour = tdiff % 24; tdiff /= 24;
168 char *tstr =
new char[32];
169 time_t bt = dbTab->getBeginTime();
171 strftime(tstr,32,dbTimeFormat[0],localtime(&bt));
172 fprintf(out,
"%-3d %.20s %d %6dday(s) %02d:%02d:%02d %s\n",
173 nRec, tstr, dbTab->getBeginTime(),
174 day,hour,min,sec, io->getComment());
176 tOld = dbTab->getBeginTime();
179 tUnix= dbTab->getEndTime();
180 if(tUnix>EEmcDbMaxUnixTime)
break;
189 const char *eTime=
"entryTime";
190 const char *vKey =
"versionKey";
198 Db<<
"select "<< vKey <<
"," << eTime <<
" from Nodes where nodeType='Config'"<<endsql;
200 while(Db.Output(&buff)){
201 buff.ReadArray(var,len,vKey);
202 fprintf(out,
"%s : ",*var);
203 buff.ReadArray(var,len,eTime);
204 fprintf(out,
"%s ",fmtSqlTime(*var));
214 const int vLen = strlen(cvsRevision);
215 static char *vStr =
new char[vLen];
216 strncpy(vStr,cvsRevision+1,vLen-1);
224 usage(
const char *message=NULL)
226 if (message) fprintf(stderr,
"%s: %s\n",argv0,message);
227 fprintf(stderr,
"usage: %s --path <path> [EXTRA_OPTIONS]\n",argv0);
228 fprintf(stderr,
" -D|--database <name> : data base to use (default %s)\n",dbName);
229 fprintf(stderr,
" -p|--path <path> : full path to the table\n");
230 fprintf(stderr,
" -t|--time <time> : sets query time (default: now)\n");
231 fprintf(stderr,
" -x|--expire <time> : sets query expiration time (default: forever)\n");
232 fprintf(stderr,
" -F|--flavor : set database flavor (default ofl)\n");
233 fprintf(stderr,
" -f|--file <file> : set file name for I/O (default: stdin/stdout)\n");
234 fprintf(stderr,
" -g|-r|--get|--read : get (read) data from database (default)\n");
235 fprintf(stderr,
" -s|-w|--set|--write : set (write) data to database\n");
236 fprintf(stderr,
" -c|--comment <txt> : set db comment (default user id)\n");
237 fprintf(stderr,
" -T|--tree : print config tree\n");
238 fprintf(stderr,
" -H|--history : print time line for node\n");
239 fprintf(stderr,
" -C|--config : print available config versions\n");
240 fprintf(stderr,
" -d|--dataonly : don't write #node/table line, just the data\n");
241 fprintf(stderr,
" -v|--verbose : set verbose mode on\n");
242 fprintf(stderr,
" -q|--quiet : set quiet mode on\n");
244 fprintf(stderr,
" -h|--help : this short help\n");
245 fprintf(stderr,
" supported time formats (cf. man date):\n");
246 for(
int i=0; dbTimeFormat[i]!=NULL ; i++ ) {
250 strftime(tmpstr,MLEN,dbTimeFormat[i],gmtime(&t));
251 fprintf(stderr,
" %-20s e.g.: %s\n",dbTimeFormat[i],tmpstr);
253 fprintf(stderr,
"%s\n",printVersion());
254 if(message) exit(-1);
262 main(
int argc,
char *argv[])
268 static struct option optLong[] = {
269 {
"database" , 1 , 0 ,
'D' },
270 {
"path" , 1 , 0 ,
'p' },
271 {
"time" , 1 , 0 ,
't' },
272 {
"expiration" , 1 , 0 ,
'x' },
273 {
"flavor" , 1 , 0 ,
'F' },
274 {
"file" , 1 , 0 ,
'f' },
275 {
"get" , 0 , (
int *)&action , GetDB },
276 {
"read" , 0 , (
int *)&action , GetDB },
277 {
"set" , 0 , (
int *)&action , SetDB },
278 {
"write" , 0 , (
int *)&action , SetDB },
279 {
"tree" , 0 , (
int *)&action , PrintTree },
280 {
"history" , 0 , (
int *)&action , PrintHistory },
281 {
"config" , 0 , (
int *)&action , PrintConfig },
282 {
"comment" , 1 , 0 ,
'c' },
283 {
"verbose" , 0 , &verboseMode ,
true},
284 {
"quiet" , 0 , &verboseMode ,
false},
285 {
"dataonly" , 0 , &dataOnlyMode ,
true},
286 {
"debug" , 0 , &debugMode ,
true},
287 {
"help" , 0 , 0 ,
'h' },
303 argv0 = strrchr(argv[0],
'/');
304 argv0 = ( argv0 == NULL ) ? argv[0] : ++argv0 ;
306 while((optChar = getopt_long(argc,argv,
"D:p:t:x:F:f:c:grswTHCdvqh",optLong,&optInd)) != EOF) {
309 case 'D' : dbName = optarg;
break;
310 case 'p' : dbPath = optarg;
break;
311 case 't' : dbTime = optarg;
break;
312 case 'x' : dbExpTime = optarg;
break;
313 case 'F' : dbFlavor = optarg;
break;
314 case 'f' : dbFile = optarg;
break;
316 case 'g' : action = GetDB;
break;
318 case 's' : action = SetDB;
break;
319 case 'T' : action = PrintTree;
break;
320 case 'H' : action = PrintHistory;
break;
321 case 'C' : action = PrintConfig;
break;
322 case 'c' : dbComment = optarg;
break;
323 case 'd' : dataOnlyMode=
true;
break;
324 case 'v' : verboseMode =
true;
break;
325 case 'q' : quietMode =
true;
break;
326 case 'h' : usage();
return(0);
break;
327 default : usage(
"unknown option");
break;
330 if(!dbPath && action!=PrintConfig ) usage(
"database path missing");
340 mgr->setVerbose(verboseMode);
341 mgr->setQuiet(quietMode);
344 if( ! mgr->findDefaultServer() ) {
345 fprintf(stderr,
"cannot find the default DB server\n");
350 file = fopen(dbFile,(action==SetDB) ?
"r" :
"w");
352 fprintf(stderr,
"%s: fopen '%s' failed, %s\n",argv0,dbFile,strerror(errno));
356 file = (action==SetDB) ? stdin : stdout;
361 node = node=strsep(&dbPath,
"/");
363 dbNode = mgr->initConfig(dbName,node);
365 if(verboseMode || !quietMode)
366 fprintf (file,
"DATABASE TREE:\n");
367 fprintf (file,
"%s/\n",node);
368 if(printTree(file,dbNode)<=0) {
369 fprintf (stderr,
"config %s is empty\n",node);
376 if(verboseMode || !quietMode)
377 fprintf(file,
"DATABASE VERSIONS:\n");
378 printConfig(file,mgr);
387 while( (node=strsep(&dbPath,
"/")) != NULL ) {
389 mgr->initConfig(dbName,node) : dbNode->findConfigNode(node);
392 printf(
"found database node: %s\n",node);
396 if(!node) usage(
"invalid path");
398 dbTab = dbNode->findTable(node);
399 if(!dbTab) { fprintf(stderr,
"%s: table %s not found\n",argv0,node);
return (-1); }
401 dprintf(
"found table: %s\n",node);
403 int nrows=dbTab->GetNRows();
407 if(dbIO==NULL)
return(-1);
409 char keyLine[EEmcDbMaxDbPathLen];
411 char keyBase[EEmcDbMaxKeyLength];
412 time_t tUnix = getTimeStamp(dbTimeFormat,dbTime);
413 time_t tExpUnix = (dbExpTime!=NULL) ? getTimeStamp(dbTimeFormat,dbExpTime) : 0;
415 dbTab->setFlavor(dbFlavor);
418 printHistory(file,mgr,dbTab,dbIO);
422 fgets (keyLine,EEmcDbMaxDbPathLen-1,file);
423 sprintf(keyBase,EEmcDbKeyFormat,dbNode->printName(),node);
424 printf(
"AAA=%s=\n",keyBase);
425 if(strstr(keyLine,keyBase)==0 && strcmp(keyLine,keyToEverything) ) {
426 fprintf(stderr,
"signature mismatch: data file key '%s', required '%s'\n",
431 dbIO->setComment(dbComment);
433 ndata = dbIO->read(file);
435 fprintf(stderr,
"%s: reading file %s failed\n",argv0,dbFile);
439 dbTab->
SetTable(dbIO->getData(),ndata, dbIO->getIndices());
441 dbTab->
SetTable(dbIO->getData(),ndata);
444 dbTab->setEndStoreTime(tExpUnix);
445 fprintf(stderr,
"%s: setting end time %ld\n",argv0,tExpUnix);
449 mgr->setStoreTime(tUnix);
450 if(! mgr->storeDbTable(dbTab) ) {
451 fprintf(stderr,
"%s: storing table %s failed\n",argv0,node);
457 mgr ->setRequestTime(tUnix);
458 if( ! mgr->fetchDbTable(dbTab) ) {
459 fprintf(stderr,
"%s: problem with fetch for time stamp %ld / %s",argv0,tUnix,ctime(&tUnix));
460 fprintf(stderr,
" There is no record for this table over the time period :\n");
461 fprintf(stderr,
" BeginDate = %s\t",dbTab->getBeginDateTime());
462 fprintf(stderr,
" EndDate = %s\n",dbTab->getEndDateTime());
463 fprintf(stderr,
" BeginTimeUnix = %d\t",dbTab->getBeginTime());
464 fprintf(stderr,
" EndTimeUnix = %d\n",dbTab->getEndTime());
467 if(! dbTab->GetTable() ) {
468 fprintf(stderr,
"no data found in table %s\n",node);
471 dbIO->setData(dbTab->GetTable());
473 if(verboseMode || !quietMode)
474 fprintf(stderr,
"BNL Time Stamp Range from: %s to: %s \n",
475 fmtSqlTime(dbTab->getBeginDateTime()),fmtSqlTime(dbTab->getEndDateTime()));
477 fprintf(file,EEmcDbKeyFormat,dbNode->printName(),node);
480 ndata = dbIO->write(file);
481 dbComment = dbIO->getComment();
482 if(verboseMode || !quietMode)
483 fprintf(stderr,
"COMMENT: %s\n",dbComment);
490 dprintf(
"database access successful %d\n",ndata);
492 fprintf(stderr,
"%s: table %s access failed\n",argv0,node);
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc'd version of data for StRoot
static StDbManager * Instance()
strdup(..) is not ANSI