10 using namespace oracle::occi;
31 if ( run != m_runNum) {
54 if (start != m_runStart) {
71 if (end != m_runEnd) {
88 if (tag != m_runTag) {
104 throw(std::runtime_error)
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));
138 m_ID = rset->getInt(1);
142 m_conn->terminateStatement(stmt);
144 throw(std::runtime_error(
"RunIOV::fetchID: "+e.getMessage()));
153 throw(std::runtime_error)
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");
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);
184 throw(std::runtime_error(
"RunIOV::setByID: "+e.getMessage()));
191 throw(std::runtime_error)
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);
228 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
232 if (!this->fetchID()) {
233 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
241 throw(std::runtime_error)
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);
273 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
277 if (!this->fetchID()) {
278 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
285 throw(std::runtime_error)
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);
317 m_ID = rset->getInt(1);
321 m_conn->terminateStatement(stmt);
323 throw(std::runtime_error(
"RunIOV::fetchID: "+e.getMessage()));
331 throw(std::runtime_error)
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);
363 throw(std::runtime_error(
"RunIOV::writeDB: "+e.getMessage()));
367 if (!this->fetchID()) {
368 throw(std::runtime_error(
"RunIOV::writeDB: Failed to write"));
377 throw(std::runtime_error)
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);
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);
413 throw(std::runtime_error(
"RunIOV::setByRun: "+e.getMessage()));
420 throw(std::runtime_error)
422 this->checkConnection();
427 Statement* stmt = m_conn->createStatement();
429 stmt->setSQL(
"SELECT iov_id FROM run_iov riov "
430 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
431 "JOIN location_def loc ON rtag.location_id = loc.def_id "
432 "WHERE loc.location = :1 AND riov.run_num = :2 "
433 "AND rtag.gen_tag != 'INVALID'");
434 stmt->setString(1, location);
435 stmt->setInt(2,
run);
439 int id = rset->getInt(1);
442 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Given run is not in the database"));
447 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): Run is nonunique for given location."));
450 m_conn->terminateStatement(stmt);
452 throw(std::runtime_error(
"RunIOV::setByRun(loc, run): "+e.getMessage()));
458 throw(std::runtime_error)
460 this->checkConnection();
462 tag->setConnection(m_env, m_conn);
463 int tagID =
tag->fetchID();
465 throw(std::runtime_error(
"RunIOV::setByRecentData: Given tag is not in the database"));
471 Statement* stmt = m_conn->createStatement();
473 stmt->setSQL(
"SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
475 "JOIN "+dataTable+
" dat on dat.iov_id = riov.iov_id "
476 "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
478 stmt->setInt(1, tagID);
479 stmt->setInt(2,
run);
485 m_ID = rset->getInt(1);
486 m_runNum = rset->getInt(2);
487 Date startDate = rset->getDate(3);
488 Date endDate = rset->getDate(4);
490 m_runStart = dh.
dateToTm( startDate );
493 throw(std::runtime_error(
"RunIOV::setByRecentData: No data exists for given tag and run"));
496 m_conn->terminateStatement(stmt);
498 throw(std::runtime_error(
"RunIOV::setByRecentData: "+e.getMessage()));
508 throw(std::runtime_error)
510 this->checkConnection();
515 Statement* stmt = m_conn->createStatement();
517 stmt->setSQL(
"SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
519 "JOIN "+dataTable+
" dat on dat.iov_id = riov.iov_id "
520 "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
521 "JOIN location_def loc ON rtag.location_id = loc.def_id "
522 "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
524 stmt->setString(1, location);
525 stmt->setInt(2,
run);
531 int id = rset->getInt(1);
534 throw(std::runtime_error(
"RunIOV::setByRecentData(datatable, loc, run): Given run is not in the database"));
538 m_conn->terminateStatement(stmt);
540 throw(std::runtime_error(
"RunIOV::setByRecentData: "+e.getMessage()));
oracle::occi::SQLException SQLException
void setRunNumber(run_t run)
void setRunTag(RunTag tag)
void setByRun(RunTag *tag, run_t run)
oracle::occi::Date tmToDate(const Tm &inTm) const
void setRunStart(Tm start)
oracle::occi::ResultSet ResultSet
run_t getRunNumber() const
void setByRecentData(std::string dataTable, RunTag *tag, run_t run=(unsigned int)-1)
Tm dateToTm(oracle::occi::Date &date) const