31 if ( run != m_runNum) {
54 if (start != m_runStart) {
71 if (end != m_runEnd) {
88 if (tag != m_runTag) {
111 this->checkConnection();
113 m_runTag.setConnection(m_env, m_conn);
114 int tagID = m_runTag.fetchID();
121 if (m_runEnd.isNull()) {
126 Statement* stmt = m_conn->createStatement();
127 stmt->setSQL(
"SELECT iov_id FROM run_iov " 128 "WHERE tag_id = :tag_id AND " 129 "run_num = :run_num AND " 130 "run_start = :run_start " );
131 stmt->setInt(1, tagID);
132 stmt->setInt(2, m_runNum);
133 stmt->setDate(3, dh.
tmToDate(m_runStart));
135 ResultSet* rset = stmt->executeQuery();
138 m_ID = rset->getInt(1);
142 m_conn->terminateStatement(stmt);
143 }
catch (SQLException &
e) {
144 throw(std::runtime_error(
"RunIOV::fetchID: "+e.getMessage()));
155 this->checkConnection();
160 Statement* stmt = m_conn->createStatement();
162 stmt->setSQL(
"SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
165 ResultSet* rset = stmt->executeQuery();
167 int tagID = rset->getInt(1);
168 m_runNum = rset->getInt(2);
169 Date startDate = rset->getDate(3);
170 Date endDate = rset->getDate(4);
172 m_runStart = dh.
dateToTm( startDate );
175 m_runTag.setConnection(m_env, m_conn);
176 m_runTag.setByID(tagID);
179 throw(std::runtime_error(
"RunIOV::setByID: Given tag_id is not in the database"));
182 m_conn->terminateStatement(stmt);
183 }
catch (SQLException &
e) {
184 throw(std::runtime_error(
"RunIOV::setByID: "+e.getMessage()));
193 this->checkConnection();
196 if (this->fetchID()) {
200 m_runTag.setConnection(m_env, m_conn);
201 int tagID = m_runTag.writeDB();
206 if (m_runStart.isNull()) {
207 throw(std::runtime_error(
"RunIOV::writeDB: Must setRunStart before writing"));
210 if (m_runEnd.isNull()) {
215 Statement* stmt = m_conn->createStatement();
217 stmt->setSQL(
"INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) " 218 "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
219 stmt->setInt(1, tagID);
220 stmt->setInt(2, m_runNum);
221 stmt->setDate(3, dh.
tmToDate(m_runStart));
222 stmt->setDate(4, dh.
tmToDate(m_runEnd));
224 stmt->executeUpdate();
226 m_conn->terminateStatement(stmt);
227 }
catch (SQLException &
e) {
228 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
232 if (!this->fetchID()) {
233 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
243 this->checkConnection();
246 if(!this->fetchID()){
251 m_runTag.setConnection(m_env, m_conn);
258 if (m_runEnd.isNull()) {
263 Statement* stmt = m_conn->createStatement();
265 stmt->setSQL(
"UPDATE run_iov set run_end=:1 where iov_id=:2 " );
266 stmt->setDate(1, dh.
tmToDate(m_runEnd));
267 stmt->setInt(2, m_ID);
269 stmt->executeUpdate();
271 m_conn->terminateStatement(stmt);
272 }
catch (SQLException &
e) {
273 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
277 if (!this->fetchID()) {
278 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
292 this->checkConnection();
294 m_runTag.setConnection(m_env, m_conn);
295 int tagID = m_runTag.fetchID();
302 if (m_runEnd.isNull()) {
307 Statement* stmt = m_conn->createStatement();
308 stmt->setSQL(
"SELECT iov_id FROM run_iov " 309 "WHERE tag_id = :tag_id AND " 310 "run_num = :run_num " );
311 stmt->setInt(1, tagID);
312 stmt->setInt(2, m_runNum);
314 ResultSet* rset = stmt->executeQuery();
317 m_ID = rset->getInt(1);
321 m_conn->terminateStatement(stmt);
322 }
catch (SQLException &
e) {
323 throw(std::runtime_error(
"RunIOV::fetchID: "+e.getMessage()));
333 this->checkConnection();
336 if(!this->fetchIDByRunAndTag()){
348 if (m_runEnd.isNull()) {
353 Statement* stmt = m_conn->createStatement();
355 stmt->setSQL(
"UPDATE run_iov set run_start=:1 where iov_id=:2 " );
356 stmt->setDate(1, dh.
tmToDate(m_runStart));
357 stmt->setInt(2, m_ID);
359 stmt->executeUpdate();
361 m_conn->terminateStatement(stmt);
362 }
catch (SQLException &
e) {
363 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
367 if (!this->fetchID()) {
368 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
379 this->checkConnection();
381 tag->setConnection(m_env, m_conn);
382 int tagID =
tag->fetchID();
384 throw(std::runtime_error(
"RunIOV::setByRun: Given tag is not in the database"));
390 Statement* stmt = m_conn->createStatement();
392 stmt->setSQL(
"SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
393 stmt->setInt(1, tagID);
394 stmt->setInt(2,
run);
396 ResultSet* rset = stmt->executeQuery();
401 m_ID = rset->getInt(1);
402 Date startDate = rset->getDate(2);
403 Date endDate = rset->getDate(3);
405 m_runStart = dh.
dateToTm( startDate );
408 throw(std::runtime_error(
"RunIOV::setByRun: Given run is not in the database"));
411 m_conn->terminateStatement(stmt);
412 }
catch (SQLException &
e) {
413 throw(std::runtime_error(
"RunIOV::setByRun: "+e.getMessage()));
420 this->checkConnection();
425 Statement* stmt = m_conn->createStatement();
427 stmt->setSQL(
"SELECT iov_id FROM (SELECT iov_id FROM run_iov riov " 428 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id " 429 "JOIN location_def loc ON rtag.location_id = loc.def_id " 430 "WHERE loc.location = :1 AND " 431 "run_start <= to_date(:2, 'YYYY-MM-DD HH24:MI:SS') AND " 432 "run_end >= to_date(:3, 'YYYY-MM-DD HH24:MI:SS') " 433 "AND rtag.gen_tag != 'INVALID' ORDER BY iov_id DESC) " 435 stmt->setString(1, location);
436 stmt->setString(2,
t.str());
437 stmt->setString(3,
t.str());
439 ResultSet* rset = stmt->executeQuery();
441 int id = rset->getInt(1);
444 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): Given run is not in the database"));
449 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): Run is nonunique for given location."));
452 m_conn->terminateStatement(stmt);
453 }
catch (SQLException &
e) {
454 throw(std::runtime_error(
"RunIOV::setByTime(loc, run): " + e.getMessage()));
461 this->checkConnection();
466 Statement* stmt = m_conn->createStatement();
468 stmt->setSQL(
"SELECT iov_id FROM run_iov riov " 469 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id " 470 "JOIN location_def loc ON rtag.location_id = loc.def_id " 471 "WHERE loc.location = :1 AND riov.run_num = :2 " 472 "AND rtag.gen_tag != 'INVALID'");
473 stmt->setString(1, location);
474 stmt->setInt(2,
run);
476 ResultSet* rset = stmt->executeQuery();
478 int id = rset->getInt(1);
481 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Given run is not in the database"));
486 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Run is nonunique for given location."));
489 m_conn->terminateStatement(stmt);
490 }
catch (SQLException &
e) {
491 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): "+e.getMessage()));
499 this->checkConnection();
501 tag->setConnection(m_env, m_conn);
502 int tagID =
tag->fetchID();
504 throw(std::runtime_error(
"RunIOV::setByRecentData: Given tag is not in the database"));
510 Statement* stmt = m_conn->createStatement();
512 stmt->setSQL(
"SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end " 514 "JOIN "+dataTable+
" dat on dat.iov_id = riov.iov_id " 515 "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
517 stmt->setInt(1, tagID);
518 stmt->setInt(2,
run);
520 ResultSet* rset = stmt->executeQuery();
524 m_ID = rset->getInt(1);
525 m_runNum = rset->getInt(2);
526 Date startDate = rset->getDate(3);
527 Date endDate = rset->getDate(4);
529 m_runStart = dh.
dateToTm( startDate );
532 throw(std::runtime_error(
"RunIOV::setByRecentData: No data exists for given tag and run"));
535 m_conn->terminateStatement(stmt);
536 }
catch (SQLException &
e) {
537 throw(std::runtime_error(
"RunIOV::setByRecentData: "+e.getMessage()));
549 this->checkConnection();
554 Statement* stmt = m_conn->createStatement();
556 stmt->setSQL(
"SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end " 558 "JOIN "+dataTable+
" dat on dat.iov_id = riov.iov_id " 559 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id " 560 "JOIN location_def loc ON rtag.location_id = loc.def_id " 561 "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
563 stmt->setString(1, location);
564 stmt->setInt(2,
run);
566 ResultSet* rset = stmt->executeQuery();
570 int id = rset->getInt(1);
573 throw(std::runtime_error(
"RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
577 m_conn->terminateStatement(stmt);
578 }
catch (SQLException &
e) {
579 throw(std::runtime_error(
"RunIOV::setByRecentData: "+e.getMessage()));
int updateEndTimeDB() noexcept(false)
void setByTime(std::string location, const Tm &t) noexcept(false)
void setByID(int id) noexcept(false) override
void setRunStart(const Tm &start)
int fetchIDByRunAndTag() noexcept(false)
int updateStartTimeDB() noexcept(false)
void setRunTag(const RunTag &tag)
void setRunNumber(run_t run)
void setByRun(RunTag *tag, run_t run) noexcept(false)
oracle::occi::Date tmToDate(const Tm &inTm) const
int writeDB() noexcept(false)
void setRunEnd(const Tm &end)
run_t getRunNumber() const
int fetchID() noexcept(false) override
Tm dateToTm(oracle::occi::Date &date) const
void setByRecentData(std::string dataTable, RunTag *tag, run_t run=(unsigned int)-1) noexcept(false)