CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LMFSeqDat.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <sstream>
3 #include <limits.h>
6 
7 using namespace std;
8 using namespace oracle::occi;
9 
11 {
12  setClassName("LMFSeqDat");
13 
14  m_runIOV = RunIOV();
15  m_intFields["seq_num"] = 0;
16  Tm t;
17  t = t.plusInfinity();
18  m_stringFields["seq_start"] = t.str();
19  m_stringFields["seq_stop"] = t.str();
20  m_intFields["vmin"] = 1;
21  m_intFields["vmax"] = 0;
22 }
23 
25  setClassName("LMFSeqDat");
26 
27  m_runIOV = RunIOV();
28  m_intFields["seq_num"] = 0;
29  Tm t;
30  t = t.plusInfinity();
31  m_stringFields["seq_start"] = t.str();
32  m_stringFields["seq_stop"] = t.str();
33  m_intFields["vmin"] = 1;
34  m_intFields["vmax"] = 0;
35 }
36 
37 LMFSeqDat::LMFSeqDat(oracle::occi::Environment* env,
38  oracle::occi::Connection* conn) : LMFUnique(env, conn) {
39  setClassName("LMFSeqDat");
40 
41  m_runIOV = RunIOV();
42  m_intFields["seq_num"] = 0;
43  Tm t;
44  t = t.plusInfinity();
45  m_stringFields["seq_start"] = t.str();
46  m_stringFields["seq_stop"] = t.str();
47  m_intFields["vmin"] = 1;
48  m_intFields["vmax"] = 0;
49 }
50 
52 {
53 }
54 
56  Tm t;
57  t.setToString(getString("seq_stop"));
58  return t;
59 }
60 
62 {
63  return m_runIOV;
64 }
65 
67 {
68  if (iov != m_runIOV) {
69  m_ID = 0;
70  m_runIOV = iov;
71  }
72  return *this;
73 }
74 
75 std::string LMFSeqDat::fetchIdSql(Statement *stmt)
76 {
77  int runIOVID = m_runIOV.getID();
78  if (!runIOVID) {
79  if (m_debug) {
80  std::cout << m_className << ": RunIOV not set" << endl;
81  }
82  return "";
83  }
84 
85  if (m_debug) {
86  std::cout << "Run IOV ID: " << runIOVID << std::endl;
87  std::cout << "SEQ # : " << getSequenceNumber() << std::endl;
88  std::cout << "Versions : " << getVmin() << " - " << getVmax() << endl;
89  }
90  std::string sql = "SELECT SEQ_ID FROM LMF_SEQ_DAT "
91  "WHERE "
92  "RUN_IOV_ID = :1 AND "
93  "SEQ_NUM = :2 AND "
94  "VMIN = :3 ";
95  if (getVmax() > 0) {
96  sql += "AND VMAX = :4";
97  } else {
98  sql += "ORDER BY VMAX DESC";
99  }
100  stmt->setSQL(sql);
101  stmt->setInt(1, runIOVID);
102  stmt->setInt(2, getSequenceNumber());
103  stmt->setInt(3, getVmin());
104  if (getVmax() > 0) {
105  stmt->setInt(4, getVmax());
106  }
107  return sql;
108 }
109 
110 std::string LMFSeqDat::setByIDSql(Statement *stmt, int id) {
111  std::string sql = "SELECT RUN_IOV_ID, SEQ_NUM, SEQ_START, SEQ_STOP, "
112  "VMIN, VMAX FROM LMF_SEQ_DAT WHERE SEQ_ID = :1";
113  stmt->setSQL(sql);
114  stmt->setInt(1, id);
115  return sql;
116 }
117 
119  DateHandler dh(m_env, m_conn);
120  int runIOVID = rset->getInt(1);
121  setInt("seq_num", rset->getInt(2));
122  Date startDate = rset->getDate(3);
123  Date endDate = rset->getDate(4);
124  setInt("vmin", rset->getInt(5));
125  setInt("vmax", rset->getInt(6));
126 
127  setString("seq_start", dh.dateToTm( startDate ).str());
128  setString("seq_stop", dh.dateToTm( endDate ).str());
129 
131  m_runIOV.setByID(runIOVID);
132 }
133 
134 bool LMFSeqDat::isValid() const {
135  bool ret = true;
136  if (getSequenceStart().isNull()) {
137  ret = false;
138  }
139  if ((getSequenceStop().str().length() > 0) &&
140  (getSequenceStop().microsTime() < getSequenceStart().microsTime())) {
141  ret = false;
142  }
143  if (getSequenceStop() == Tm().plusInfinity()) {
144  ret = false;
145  }
146  return ret;
147 }
148 
150 {
151  // Validate the data, use infinity-till convention
152  DateHandler dh(m_env, m_conn);
153 
154  if (!isValid()) {
155  dump();
156  throw(std::runtime_error("LMFSeqDat::writeDB: not valid"));
157  }
158 
159  if (getSequenceStop().str().length() == 0) {
161  }
162  int runIOVID = m_runIOV.getID();
163  if (runIOVID == 0) {
164  throw(std::runtime_error("LMFSeqDat::writeDB: RunIOV not set"));
165  }
166  std::string sp = sequencePostfix(getSequenceStart());
167  std::string sql = "INSERT INTO LMF_SEQ_DAT (SEQ_ID, RUN_IOV_ID, SEQ_NUM, "
168  "SEQ_START, SEQ_STOP, VMIN, VMAX) "
169  "VALUES (SEQ_ID_" + sp + "_SQ.NextVal, :1, :2, :3, :4, :5, :6)";
170  cout << sql << endl;
171  stmt->setSQL(sql);
172  stmt->setInt(1, runIOVID);
173  stmt->setInt(2, getSequenceNumber());
174  stmt->setDate(3, dh.tmToDate(getSequenceStart()));
175  stmt->setDate(4, dh.tmToDate(getSequenceStop()));
176  stmt->setInt(5, getVmin());
177  stmt->setInt(6, getVmax());
178  return sql;
179 }
180 
182  throw(std::runtime_error)
183 {
184  // get the RunIOV
186  int runIOVID = m_runIOV.getID();
187  m_runIOV.setByID(runIOVID);
188 
189  if (m_runIOV.getID() == 0) {
190  throw(std::runtime_error("LMFSeqDat: Given RunIOV does not exist in DB"));
191  }
192 
193 }
194 
195 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::string sql,
196  std::string method)
197  throw(std::runtime_error)
198 {
199  std::vector<std::string> pars;
200  return fetchByRunIOV(pars, sql, method);
201 }
202 
203 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(int par,
204  std::string sql,
205  std::string method)
206  throw(std::runtime_error)
207 {
208  std::vector<std::string> pars;
209  std::stringstream ss;
210  ss << "I" << par;
211  pars.push_back(ss.str());
212  return fetchByRunIOV(pars, sql, method);
213 }
214 
215 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::vector<std::string> pars,
216  std::string sql,
217  std::string method)
218  throw(std::runtime_error)
219 {
220  std::map<int, LMFSeqDat> l;
221  this->checkConnection();
222  try {
223  Statement *stmt = m_conn->createStatement();
224  stmt->setSQL(sql);
225  for (unsigned int i = 0; i < pars.size(); i++) {
226  if (pars[i][0] == 'I') {
227  stmt->setInt(i + 1, atoi(pars[i].c_str() + 1));
228  } else if (pars[i][0] == 'S') {
229  stmt->setString(i + 1, pars[i].c_str() + 1);
230  } else {
231  throw(std::runtime_error(m_className + "::" + method + ": " +
232  "Invalid type"));
233  }
234  }
235  ResultSet *rset = stmt->executeQuery();
236  while (rset->next()) {
237  int seq_id = rset->getInt(1);
238  LMFSeqDat s;
239  s.setConnection(m_env, m_conn);
240  s.setByID(seq_id);
241  l[s.getSequenceNumber()] = s;
242  }
243  m_conn->terminateStatement(stmt);
244  } catch (SQLException &e) {
245  throw(std::runtime_error(m_className + "::" + method + ": " +
246  e.getMessage()));
247  }
248  return l;
249 }
250 
252  LMFSeqDat ret;
253  std::map<int, LMFSeqDat> m =
254  fetchByRunIOV("SELECT SEQ_ID FROM LMF_SEQ_DAT WHERE SEQ_ID = "
255  "(SELECT MAX(SEQ_ID) FROM LMF_SEQ_DAT)", "fetchLast");
256  if (m.size() > 0) {
257  ret = m.begin()->second;
258  }
259  return ret;
260 }
261 
263  return fetchLast().getRunIOV();
264 }
265 
266 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov) {
267  int runIOVID = iov.getID();
268  return fetchByRunIOV(runIOVID,
269  "SELECT SEQ_ID FROM LMF_SEQ_DAT WHERE RUN_IOV_ID = :1",
270  "fetchByRunIOV");
271 }
272 
273 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov,
274  const LMFColor &col) {
275  int runIOVID = iov.getID();
276  int colorId = col.getID();
277  std::vector<std::string> pars;
278  std::stringstream ss;
279  ss << "I" << runIOVID;
280  pars.push_back(ss.str());
281  ss.str(std::string());
282  ss << "I" << colorId;
283  pars.push_back(ss.str());
284  return fetchByRunIOV(pars,
285  "SELECT S.SEQ_ID FROM LMF_SEQ_DAT S JOIN LMF_RUN_IOV R"
286  " ON S.SEQ_ID = R.SEQ_ID WHERE RUN_IOV_ID = :1 AND "
287  " COLOR_ID = :2",
288  "fetchByRunIOVAndColor");
289 }
290 
291 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunNumber(int runno) {
292  return fetchByRunIOV(runno,
293  "SELECT SEQ_ID FROM LMF_SEQ_DAT D JOIN RUN_IOV R ON "
294  "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1",
295  "fetchByRunNumber");
296 }
297 
299  return fetchByRunNumber(runno, taken_at.str());
300 }
301 
302 LMFSeqDat LMFSeqDat::fetchByRunNumber(int runno, std::string taken_at) {
303  std::map<int, LMFSeqDat> l;
304  std::vector<std::string> pars;
305  std::stringstream ss;
306  ss << "I" << runno;
307  pars.push_back(ss.str());
308  ss.str(std::string());
309  ss << "S" << taken_at;
310  pars.push_back(ss.str());
311  std::string q = "SELECT SEQ_ID FROM LMF_SEQ_DAT D JOIN RUN_IOV R ON "
312  "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1 AND "
313  "SEQ_START >= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS') "
314  "AND SEQ_STOP <= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS')";
315  l = fetchByRunIOV(pars, q, "fetchByRunNumberAt");
316  LMFSeqDat ret;
317  if (l.size() == 1) {
318  std::map<int, LMFSeqDat>::const_iterator x = l.begin();
319  ret = x->second;
320  } else if (l.size() > 1) {
321  std::cout << "WARNING: Your query returned more than one result. "
322  << std::endl;
323  std::cout << " This was not expected. Please check the DB!!!"
324  << std::endl;
325  std::cout << "Your query: " << std::endl << q << std::endl;
326  std::cout << "Your parameters: " << std::endl;
327  for (unsigned int i = 0; i < pars.size(); i++) {
328  std::cout << i << ": " << pars[i] << std::endl;
329  }
330  }
331  return ret;
332 }
333 
std::string m_className
Definition: LMFUnique.h:98
int i
Definition: DBlmapReader.cc:9
LMFUnique & setInt(std::string key, int value)
Definition: LMFUnique.cc:33
oracle::occi::Environment * m_env
Definition: IDBObject.h:38
std::map< int, LMFSeqDat > fetchByRunNumber(int runno)
Definition: LMFSeqDat.cc:291
Tm getSequenceStop() const
Definition: LMFSeqDat.cc:55
oracle::occi::Connection * m_conn
Definition: IDBObject.h:39
LMFSeqDat & setRunIOV(const RunIOV &iov)
Definition: LMFSeqDat.cc:66
int getID() const
Definition: LMFUnique.h:51
void fetchParentIDs()
Definition: LMFSeqDat.cc:181
std::string fetchIdSql(Statement *stmt)
Definition: LMFSeqDat.cc:75
void setByID(int id)
Definition: RunIOV.cc:152
std::map< int, LMFSeqDat > fetchByRunIOV(RunIOV &iov)
Definition: LMFSeqDat.cc:266
std::string writeDBSql(Statement *stmt)
Definition: LMFSeqDat.cc:149
LMFSeqDat fetchLast()
Definition: LMFSeqDat.cc:251
int getVmax() const
Definition: LMFSeqDat.h:49
int getSequenceNumber() const
Definition: LMFSeqDat.h:41
oracle::occi::ResultSet ResultSet
Definition: LMFUnique.h:19
virtual void setClassName(std::string s)
Definition: LMFUnique.h:96
virtual void dump() const
Definition: LMFUnique.cc:106
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:22
void setByID(int id)
Definition: LMFUnique.cc:280
std::string sequencePostfix(Tm t)
Definition: LMFUnique.cc:10
LMFSeqDat & setSequenceStop(Tm end)
Definition: LMFSeqDat.h:31
std::map< std::string, std::string > m_stringFields
Definition: LMFUnique.h:101
RunIOV m_runIOV
Definition: LMFSeqDat.h:79
tuple iov
Definition: o2o.py:307
oracle::occi::Statement Statement
Definition: LMFUnique.h:20
void setToString(const std::string s)
Definition: Tm.cc:172
Tm getSequenceStart() const
Definition: LMFSeqDat.h:42
oracle::occi::Date tmToDate(const Tm &inTm) const
Definition: DateHandler.cc:20
LMFUnique & setString(std::string key, std::string value)
Definition: LMFUnique.cc:15
Tm getPlusInfTm() const
Definition: DateHandler.h:15
RunIOV getRunIOV() const
Definition: LMFSeqDat.cc:61
RunIOV fetchLastRun()
Definition: LMFSeqDat.cc:262
void getParameters(ResultSet *rset)
Definition: LMFSeqDat.cc:118
std::string str() const
Definition: Tm.cc:82
int getID()
Definition: RunIOV.h:36
tuple cout
Definition: gather_cfg.py:41
std::string getString(std::string fieldname) const
Definition: LMFUnique.cc:188
char m_debug
Definition: LMFUnique.h:99
void setConnection(oracle::occi::Environment *env, oracle::occi::Connection *conn)
Definition: IDBObject.h:23
string s
Definition: asciidump.py:422
Definition: RunIOV.h:13
~LMFSeqDat()
Definition: LMFSeqDat.cc:51
bool isValid() const
Definition: LMFSeqDat.cc:134
Tm dateToTm(oracle::occi::Date &date) const
Definition: DateHandler.cc:31
Definition: Tm.h:14
std::string setByIDSql(Statement *stmt, int id)
Definition: LMFSeqDat.cc:110
static Tm plusInfinity()
Definition: Tm.h:47
int getVmin() const
Definition: LMFSeqDat.h:48
std::map< std::string, int > m_intFields
Definition: LMFUnique.h:103
const double par[8 *NPar][4]