EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDb.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDb.cxx
1 #include "FairDb.h"
2 #include <sstream>
3 
4 static std::map<std::string,Int_t> fgTimegateTable;
5 
6 Int_t FairDb::GetTimeGate(const std::string& tableName)
7 {
8 
9  // Set default if looking up table for the first time.
10  std::map<std::string,Int_t>::iterator
11  tablePtr = fgTimegateTable.find(tableName);
12  if ( tablePtr == fgTimegateTable.end()
13  ) { FairDb::SetTimeGate(tableName,10*24*60*60); }
14 
15  cout << "-I- FairDb:: Returning time gate " << fgTimegateTable[tableName]
16  << " for " << tableName << endl;
17  return fgTimegateTable[tableName];
18 
19 }
20 
21 TString FairDb::GetValDescr(const char* tableName,
22  Bool_t isTemporary)
23 {
24 
25  TString sql;
26  sql += "create ";
27  if ( isTemporary ) { sql += "temporary "; }
28  sql += "table ";
29  sql += tableName;
30  sql += "VAL ( ";
31  sql += " SEQNO integer not null primary key,";
32  sql += " TIMESTART datetime not null,";
33  sql += " TIMEEND datetime not null,";
34  sql += " DETECTORMASK tinyint,";
35  sql += " SIMMASK tinyint,";
36  sql += " TASK integer,";
37  sql += " AGGREGATENO integer,";
38  sql += " CREATIONDATE datetime not null,";
39  sql += " INSERTDATE datetime not null ) ";
40  return sql;
41 }
42 
43 //.....................................................................
44 
45 TString FairDb::MakeDateTimeString(const ValTimeStamp& timeStamp)
46 {
47  return timeStamp.AsString("s");
48 
49 }
50 //.....................................................................
51 
52 ValTimeStamp FairDb::MakeTimeStamp(const std::string& sqlDateTime,
53  Bool_t* ok)
54 {
55 
56  struct date {
57  int year;
58  int month;
59  int day;
60  int hour;
61  int min;
62  int sec;
63  };
64  char dummy;
65 
66  static string lo = "1970-01-01 00:00:00";
67  static string hi = "2038-01-19 03:14:07";
68 
69  // Set up defaults from 0:0am today.
70  ValTimeStamp nowTS;
71  int nowDate = nowTS.GetDate();
72  date defaultDate = {nowDate/10000, nowDate/100%100, nowDate%100,0,0,0};
73  date input = defaultDate;
74 
75  istringstream in(sqlDateTime);
76  in >> input.year >> dummy >> input.month >> dummy >> input.day
77  >> input.hour >> dummy >> input.min >> dummy >> input.sec;
78 
79  if ( ok ) { *ok = kTRUE; }
80  if ( sqlDateTime < lo || sqlDateTime > hi ) {
81  if ( ok ) { *ok = kFALSE; }
82  else {
83  static int bad_date_count = 0;
84  if ( ++bad_date_count <= 20 ) {
85  const char* last = (bad_date_count == 20) ? "..Last Message.. " : "";
86 
87  cout<< "-I- FairDB:: Bad date string: " << sqlDateTime
88  << " parsed as "
89  << input.year << " "
90  << input.month << " "
91  << input.day << " "
92  << input.hour << " "
93  << input.min << " "
94  << input.sec
95  << "\n Outside range " << lo
96  << " to " << hi << last << endl;
97  }
98  }
99 
100  input = defaultDate;
101  }
102 
103  return ValTimeStamp(input.year,input.month,input.day,
104  input.hour,input.min,input.sec);
105 
106 }
107 
108 void FairDb::SetTimeGate(const std::string& tableName, Int_t timeGate)
109 {
110 
111  if ( timeGate > 15 && timeGate <= 100*24*60*60 ) {
112  fgTimegateTable[tableName] = timeGate;
113  cout << "-I- FairDb:: Setting time gate " << timeGate
114  << " for " << tableName << endl;
115  } else {
116  cout << "-I- FairDb:: Ignoring invalid time gate setting "
117  << timeGate
118  << " for " << tableName << endl;
119  }
120 }
121 
122 Bool_t FairDb::NotGlobalSeqNo(UInt_t seqNo)
123 {
124  return seqNo <= kMAXLOCALSEQNO;
125 }
126