23 if (
run != m_runNum) {
34 if (
start != m_runStart) {
43 if (
end != m_runEnd) {
52 if (
tag != m_runTag) {
66 this->checkConnection();
68 m_runTag.setConnection(m_env, m_conn);
69 int tagID = m_runTag.fetchID();
76 if (m_runEnd.isNull()) {
77 m_runEnd =
dh.getPlusInfTm();
81 Statement* stmt = m_conn->createStatement();
83 "SELECT iov_id FROM run_iov "
84 "WHERE tag_id = :tag_id AND "
85 "run_num = :run_num AND "
86 "run_start = :run_start ");
87 stmt->setInt(1, tagID);
88 stmt->setInt(2, m_runNum);
89 stmt->setDate(3,
dh.tmToDate(m_runStart));
91 ResultSet* rset = stmt->executeQuery();
94 m_ID = rset->getInt(1);
98 m_conn->terminateStatement(stmt);
99 }
catch (SQLException&
e) {
100 throw(std::runtime_error(
"RunIOV::fetchID: " +
e.getMessage()));
107 this->checkConnection();
112 Statement* stmt = m_conn->createStatement();
114 stmt->setSQL(
"SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
117 ResultSet* rset = stmt->executeQuery();
119 int tagID = rset->getInt(1);
120 m_runNum = rset->getInt(2);
121 Date startDate = rset->getDate(3);
122 Date endDate = rset->getDate(4);
124 m_runStart =
dh.dateToTm(startDate);
125 m_runEnd =
dh.dateToTm(endDate);
127 m_runTag.setConnection(m_env, m_conn);
128 m_runTag.setByID(tagID);
131 throw(std::runtime_error(
"RunIOV::setByID: Given tag_id is not in the database"));
134 m_conn->terminateStatement(stmt);
135 }
catch (SQLException&
e) {
136 throw(std::runtime_error(
"RunIOV::setByID: " +
e.getMessage()));
141 this->checkConnection();
144 if (this->fetchID()) {
148 m_runTag.setConnection(m_env, m_conn);
149 int tagID = m_runTag.writeDB();
154 if (m_runStart.isNull()) {
155 throw(std::runtime_error(
"RunIOV::writeDB: Must setRunStart before writing"));
158 if (m_runEnd.isNull()) {
159 m_runEnd =
dh.getPlusInfTm();
163 Statement* stmt = m_conn->createStatement();
166 "INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) "
167 "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
168 stmt->setInt(1, tagID);
169 stmt->setInt(2, m_runNum);
170 stmt->setDate(3,
dh.tmToDate(m_runStart));
171 stmt->setDate(4,
dh.tmToDate(m_runEnd));
173 stmt->executeUpdate();
175 m_conn->terminateStatement(stmt);
176 }
catch (SQLException&
e) {
177 throw(std::runtime_error(
"RunIOV::writeDB: " +
e.getMessage()));
181 if (!this->fetchID()) {
182 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
189 this->checkConnection();
192 if (!this->fetchID()) {
196 m_runTag.setConnection(m_env, m_conn);
203 if (m_runEnd.isNull()) {
204 m_runEnd =
dh.getPlusInfTm();
208 Statement* stmt = m_conn->createStatement();
210 stmt->setSQL(
"UPDATE run_iov set run_end=:1 where iov_id=:2 ");
211 stmt->setDate(1,
dh.tmToDate(m_runEnd));
212 stmt->setInt(2, m_ID);
214 stmt->executeUpdate();
216 m_conn->terminateStatement(stmt);
217 }
catch (SQLException&
e) {
218 throw(std::runtime_error(
"RunIOV::writeDB: " +
e.getMessage()));
222 if (!this->fetchID()) {
223 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
235 this->checkConnection();
237 m_runTag.setConnection(m_env, m_conn);
238 int tagID = m_runTag.fetchID();
245 if (m_runEnd.isNull()) {
246 m_runEnd =
dh.getPlusInfTm();
250 Statement* stmt = m_conn->createStatement();
252 "SELECT iov_id FROM run_iov "
253 "WHERE tag_id = :tag_id AND "
254 "run_num = :run_num ");
255 stmt->setInt(1, tagID);
256 stmt->setInt(2, m_runNum);
258 ResultSet* rset = stmt->executeQuery();
261 m_ID = rset->getInt(1);
265 m_conn->terminateStatement(stmt);
266 }
catch (SQLException&
e) {
267 throw(std::runtime_error(
"RunIOV::fetchID: " +
e.getMessage()));
274 this->checkConnection();
277 if (!this->fetchIDByRunAndTag()) {
288 if (m_runEnd.isNull()) {
289 m_runEnd =
dh.getPlusInfTm();
293 Statement* stmt = m_conn->createStatement();
295 stmt->setSQL(
"UPDATE run_iov set run_start=:1 where iov_id=:2 ");
296 stmt->setDate(1,
dh.tmToDate(m_runStart));
297 stmt->setInt(2, m_ID);
299 stmt->executeUpdate();
301 m_conn->terminateStatement(stmt);
302 }
catch (SQLException&
e) {
303 throw(std::runtime_error(
"RunIOV::writeDB: " +
e.getMessage()));
307 if (!this->fetchID()) {
308 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
315 this->checkConnection();
317 tag->setConnection(m_env, m_conn);
318 int tagID =
tag->fetchID();
320 throw(std::runtime_error(
"RunIOV::setByRun: Given tag is not in the database"));
326 Statement* stmt = m_conn->createStatement();
328 stmt->setSQL(
"SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
329 stmt->setInt(1, tagID);
330 stmt->setInt(2,
run);
332 ResultSet* rset = stmt->executeQuery();
337 m_ID = rset->getInt(1);
338 Date startDate = rset->getDate(2);
339 Date endDate = rset->getDate(3);
341 m_runStart =
dh.dateToTm(startDate);
342 m_runEnd =
dh.dateToTm(endDate);
344 throw(std::runtime_error(
"RunIOV::setByRun: Given run is not in the database"));
347 m_conn->terminateStatement(stmt);
348 }
catch (SQLException&
e) {
349 throw(std::runtime_error(
"RunIOV::setByRun: " +
e.getMessage()));
354 this->checkConnection();
359 Statement* stmt = m_conn->createStatement();
362 "SELECT iov_id FROM (SELECT iov_id FROM run_iov riov "
363 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
364 "JOIN location_def loc ON rtag.location_id = loc.def_id "
365 "WHERE loc.location = :1 AND "
366 "run_start <= to_date(:2, 'YYYY-MM-DD HH24:MI:SS') AND "
367 "run_end >= to_date(:3, 'YYYY-MM-DD HH24:MI:SS') "
368 "AND rtag.gen_tag != 'INVALID' ORDER BY iov_id DESC) "
371 stmt->setString(2,
t.str());
372 stmt->setString(3,
t.str());
374 ResultSet* rset = stmt->executeQuery();
376 int id = rset->getInt(1);
379 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): Given run is not in the database"));
384 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): Run is nonunique for given location."));
387 m_conn->terminateStatement(stmt);
388 }
catch (SQLException&
e) {
389 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): " +
e.getMessage()));
394 this->checkConnection();
399 Statement* stmt = m_conn->createStatement();
402 "SELECT iov_id FROM run_iov riov "
403 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
404 "JOIN location_def loc ON rtag.location_id = loc.def_id "
405 "WHERE loc.location = :1 AND riov.run_num = :2 "
406 "AND rtag.gen_tag != 'INVALID'");
408 stmt->setInt(2,
run);
410 ResultSet* rset = stmt->executeQuery();
412 int id = rset->getInt(1);
415 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Given run is not in the database"));
420 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Run is nonunique for given location."));
423 m_conn->terminateStatement(stmt);
424 }
catch (SQLException&
e) {
425 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): " +
e.getMessage()));
430 this->checkConnection();
432 tag->setConnection(m_env, m_conn);
433 int tagID =
tag->fetchID();
435 throw(std::runtime_error(
"RunIOV::setByRecentData: Given tag is not in the database"));
441 Statement* stmt = m_conn->createStatement();
444 "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
448 " dat on dat.iov_id = riov.iov_id "
449 "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
451 stmt->setInt(1, tagID);
452 stmt->setInt(2,
run);
454 ResultSet* rset = stmt->executeQuery();
458 m_ID = rset->getInt(1);
459 m_runNum = rset->getInt(2);
460 Date startDate = rset->getDate(3);
461 Date endDate = rset->getDate(4);
463 m_runStart =
dh.dateToTm(startDate);
464 m_runEnd =
dh.dateToTm(endDate);
466 throw(std::runtime_error(
"RunIOV::setByRecentData: No data exists for given tag and run"));
469 m_conn->terminateStatement(stmt);
470 }
catch (SQLException&
e) {
471 throw(std::runtime_error(
"RunIOV::setByRecentData: " +
e.getMessage()));
476 this->checkConnection();
481 Statement* stmt = m_conn->createStatement();
484 "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
488 " dat on dat.iov_id = riov.iov_id "
489 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
490 "JOIN location_def loc ON rtag.location_id = loc.def_id "
491 "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
494 stmt->setInt(2,
run);
496 ResultSet* rset = stmt->executeQuery();
499 int id = rset->getInt(1);
502 throw(std::runtime_error(
"RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
505 m_conn->terminateStatement(stmt);
506 }
catch (SQLException&
e) {
507 throw(std::runtime_error(
"RunIOV::setByRecentData: " +
e.getMessage()));