ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PgPostCalBankIterator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PgPostCalBankIterator.cc
2 #include "PgPostApplication.h"
3 #include "PgPostBankManager.h"
4 #include "PgPostBankWrapper.h"
5 
6 #include <pdbcalbase/PdbApplication.h>
7 #include <pdbcalbase/PdbBankID.h>
8 #include <pdbcalbase/PdbCalBank.h>
9 
10 #include <phool/PHTimeStamp.h> // for PHTimeStamp, operator<<
11 #include <phool/phool.h>
12 
13 #include <RDBC/TSQLConnection.h>
14 #include <RDBC/TSQLResultSet.h>
15 #include <RDBC/TSQLStatement.h>
16 
17 #include <TString.h>
18 
19 #include <cstdlib>
20 #include <iostream>
21 #include <sstream>
22 #include <utility>
23 
24 
25 using namespace std;
26 
27 //_____________________________________________________________________________
29  : fBM(bm)
30  , fApplication(0)
31  , fDBName("")
32  , fTableName("")
33  , fIsValid(false)
34  , fBankID(-1)
35  , fSQLStatement(0)
36  , fSQLResultSet(0)
37 {
39  if (!fApplication)
40  {
41  cout << PHWHERE << "dynamic_cast failed, exiting" << endl;
42  exit(1);
43  }
44 }
45 
46 //_____________________________________________________________________________
48 {
49  delete fSQLResultSet;
50  delete fSQLStatement;
51 }
52 
53 //_____________________________________________________________________________
54 bool PgPostCalBankIterator::init(const string& fulldbname, const PdbBankID& bankid)
55 {
56  fDBName = fulldbname;
57  fBankID = bankid;
58 
59  if (!fApplication->startRead())
60  {
61  std::cerr << PHWHERE << "Cannot start a read transaction"
62  << std::endl;
63  return false;
64  }
65 
66  TSQLConnection* con = fApplication->getConnection();
67  if (!con)
68  {
69  std::cout << PHWHERE << " Cannot get TSQLConnection, exiting" << std::endl;
70  exit(1);
71  }
72 
73  fSQLStatement = con->CreateStatement();
74 
75  std::ostringstream query;
76 
77  query << "select * from " << fulldbname;
78 
79  if (fBankID.getInternalValue() != -1)
80  {
81  query << " where bankID = "
82  << bankid.getInternalValue();
83  }
84 
85  fSQLResultSet = fSQLStatement->ExecuteQuery(query.str().c_str());
86 
87  fIsValid = false;
88 
89  if (fSQLResultSet)
90  {
91  fIsValid = true;
92  }
93 
94  return fIsValid;
95 }
96 
97 //_____________________________________________________________________________
99 {
100  return fIsValid;
101 }
102 
103 //_____________________________________________________________________________
104 PdbCalBank*
106 {
107  if (!fIsValid)
108  {
109  return 0;
110  }
111 
112  //
113  // Search the complete database for the next banks with
114  // matching validity range(s).
115  //
116 
117  ValPeriod insert(0, 0);
118  ValPeriod end(0, 0);
119  ValPeriod start(0, 0);
120 
121  const TimeMap::iterator it = fTimeMap.find("InsertTime");
122 
123  bool insertIntervalGiven = (it != fTimeMap.end());
124  if (insertIntervalGiven)
125  {
126  insert = it->second;
127  }
128 
129  const TimeMap::iterator it2 = fTimeMap.find("EndVal");
130  bool endIntervalGiven = (it2 != fTimeMap.end());
131  if (endIntervalGiven)
132  {
133  end = it2->second;
134  }
135 
136  const TimeMap::iterator it3 = fTimeMap.find("StartVal");
137  bool startIntervalGiven = (it3 != fTimeMap.end());
138  if (startIntervalGiven)
139  {
140  start = it3->second;
141  }
142 
143  while (fSQLResultSet->Next())
144  {
145  if (insertIntervalGiven)
146  {
147  // skip banks not corresponding to required insert interval
148  time_t inserttime = fSQLResultSet->GetLong(2);
149  if (inserttime >= insert.end() ||
150  inserttime < insert.start())
151  {
152  continue;
153  }
154  }
155  if (startIntervalGiven)
156  {
157  // skip banks not corresponding to required start interval
158  time_t starttime = fSQLResultSet->GetLong(3);
159  if (starttime > start.end() ||
160  starttime < start.start())
161  {
162  continue;
163  }
164  }
165 
166  if (endIntervalGiven)
167  {
168  // skip banks not corresponding to required end interval
169  time_t endtime = fSQLResultSet->GetLong(4);
170  if (endtime > end.end() ||
171  endtime < end.start())
172  {
173  continue;
174  }
175  }
176 
177  // Found a good match
178  PdbCalBank* bank = static_cast<PdbCalBank*>(fSQLResultSet->GetObject(7));
179  PgPostBankWrapper* bw = new PgPostBankWrapper(bank);
180  bw->setBankID(fSQLResultSet->GetInt(1));
181  bw->setInsertTime(fSQLResultSet->GetLong(2));
182  bw->setStartValTime(fSQLResultSet->GetLong(3));
183  bw->setEndValTime(fSQLResultSet->GetLong(4));
184  bw->setDescription(fSQLResultSet->GetString(5).Data());
185  bw->setUserName(fSQLResultSet->GetString(6).Data());
186  bw->setTableName(fTableName.c_str());
187  return bw;
188  }
189 
190  fIsValid = false;
191  return 0;
192 }
193 
194 //_____________________________________________________________________________
195 void PgPostCalBankIterator::print(std::ostream& os) const
196 {
197  TimeMap::const_iterator it;
198 
199  os << "PgPostCalBankIterator for database "
200  << fDBName << " and BankID " << fBankID.getInternalValue()
201  << " (table=" << fTableName << ")"
202  << std::endl;
203 
204  for (it = fTimeMap.begin(); it != fTimeMap.end(); ++it)
205  {
206  os << it->first << "=[" << PHTimeStamp(it->second.start())
207  << "," << PHTimeStamp(it->second.end()) << "]" << std::endl;
208  }
209 }
210 
211 //_____________________________________________________________________________
213 {
214  fBankID = id;
215 }
216 
217 //_____________________________________________________________________________
219  const PHTimeStamp& max)
220 {
221  fTimeMap["EndVal"] = ValPeriod(min, max);
222 }
223 
224 //_____________________________________________________________________________
226  const PHTimeStamp& max)
227 {
228  fTimeMap["InsertTime"] = ValPeriod(min, max);
229 }
230 
231 //_____________________________________________________________________________
233  const PHTimeStamp& max)
234 {
235  fTimeMap["StartVal"] = ValPeriod(min, max);
236 }