Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:12

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0005 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
0006 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0007 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 RunIOV::RunIOV() {
0013   m_conn = nullptr;
0014   m_ID = 0;
0015   m_runNum = 0;
0016   m_runStart = Tm();
0017   m_runEnd = Tm();
0018 }
0019 
0020 RunIOV::~RunIOV() {}
0021 
0022 void RunIOV::setRunNumber(run_t run) {
0023   if (run != m_runNum) {
0024     m_ID = 0;
0025     m_runNum = run;
0026   }
0027 }
0028 
0029 void RunIOV::setID(int id) { m_ID = id; }
0030 
0031 run_t RunIOV::getRunNumber() const { return m_runNum; }
0032 
0033 void RunIOV::setRunStart(const Tm& start) {
0034   if (start != m_runStart) {
0035     m_ID = 0;
0036     m_runStart = start;
0037   }
0038 }
0039 
0040 Tm RunIOV::getRunStart() const { return m_runStart; }
0041 
0042 void RunIOV::setRunEnd(const Tm& end) {
0043   if (end != m_runEnd) {
0044     m_ID = 0;
0045     m_runEnd = end;
0046   }
0047 }
0048 
0049 Tm RunIOV::getRunEnd() const { return m_runEnd; }
0050 
0051 void RunIOV::setRunTag(const RunTag& tag) {
0052   if (tag != m_runTag) {
0053     m_ID = 0;
0054     m_runTag = tag;
0055   }
0056 }
0057 
0058 RunTag RunIOV::getRunTag() const { return m_runTag; }
0059 
0060 int RunIOV::fetchID() noexcept(false) {
0061   // Return from memory if available
0062   if (m_ID) {
0063     return m_ID;
0064   }
0065 
0066   this->checkConnection();
0067 
0068   m_runTag.setConnection(m_env, m_conn);
0069   int tagID = m_runTag.fetchID();
0070   if (!tagID) {
0071     return 0;
0072   }
0073 
0074   DateHandler dh(m_env, m_conn);
0075 
0076   if (m_runEnd.isNull()) {
0077     m_runEnd = dh.getPlusInfTm();
0078   }
0079 
0080   try {
0081     Statement* stmt = m_conn->createStatement();
0082     stmt->setSQL(
0083         "SELECT iov_id FROM run_iov "
0084         "WHERE tag_id = :tag_id AND "
0085         "run_num = :run_num AND "
0086         "run_start = :run_start  ");
0087     stmt->setInt(1, tagID);
0088     stmt->setInt(2, m_runNum);
0089     stmt->setDate(3, dh.tmToDate(m_runStart));
0090 
0091     ResultSet* rset = stmt->executeQuery();
0092 
0093     if (rset->next()) {
0094       m_ID = rset->getInt(1);
0095     } else {
0096       m_ID = 0;
0097     }
0098     m_conn->terminateStatement(stmt);
0099   } catch (SQLException& e) {
0100     throw(std::runtime_error("RunIOV::fetchID:  " + e.getMessage()));
0101   }
0102 
0103   return m_ID;
0104 }
0105 
0106 void RunIOV::setByID(int id) noexcept(false) {
0107   this->checkConnection();
0108 
0109   DateHandler dh(m_env, m_conn);
0110 
0111   try {
0112     Statement* stmt = m_conn->createStatement();
0113 
0114     stmt->setSQL("SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
0115     stmt->setInt(1, id);
0116 
0117     ResultSet* rset = stmt->executeQuery();
0118     if (rset->next()) {
0119       int tagID = rset->getInt(1);
0120       m_runNum = rset->getInt(2);
0121       Date startDate = rset->getDate(3);
0122       Date endDate = rset->getDate(4);
0123 
0124       m_runStart = dh.dateToTm(startDate);
0125       m_runEnd = dh.dateToTm(endDate);
0126 
0127       m_runTag.setConnection(m_env, m_conn);
0128       m_runTag.setByID(tagID);
0129       m_ID = id;
0130     } else {
0131       throw(std::runtime_error("RunIOV::setByID:  Given tag_id is not in the database"));
0132     }
0133 
0134     m_conn->terminateStatement(stmt);
0135   } catch (SQLException& e) {
0136     throw(std::runtime_error("RunIOV::setByID:  " + e.getMessage()));
0137   }
0138 }
0139 
0140 int RunIOV::writeDB() noexcept(false) {
0141   this->checkConnection();
0142 
0143   // Check if this IOV has already been written
0144   if (this->fetchID()) {
0145     return m_ID;
0146   }
0147 
0148   m_runTag.setConnection(m_env, m_conn);
0149   int tagID = m_runTag.writeDB();
0150 
0151   // Validate the data, use infinity-till convention
0152   DateHandler dh(m_env, m_conn);
0153 
0154   if (m_runStart.isNull()) {
0155     throw(std::runtime_error("RunIOV::writeDB:  Must setRunStart before writing"));
0156   }
0157 
0158   if (m_runEnd.isNull()) {
0159     m_runEnd = dh.getPlusInfTm();
0160   }
0161 
0162   try {
0163     Statement* stmt = m_conn->createStatement();
0164 
0165     stmt->setSQL(
0166         "INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) "
0167         "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
0168     stmt->setInt(1, tagID);
0169     stmt->setInt(2, m_runNum);
0170     stmt->setDate(3, dh.tmToDate(m_runStart));
0171     stmt->setDate(4, dh.tmToDate(m_runEnd));
0172 
0173     stmt->executeUpdate();
0174 
0175     m_conn->terminateStatement(stmt);
0176   } catch (SQLException& e) {
0177     throw(std::runtime_error("RunIOV::writeDB:  " + e.getMessage()));
0178   }
0179 
0180   // Now get the ID
0181   if (!this->fetchID()) {
0182     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
0183   }
0184 
0185   return m_ID;
0186 }
0187 
0188 int RunIOV::updateEndTimeDB() noexcept(false) {
0189   this->checkConnection();
0190 
0191   // Check if this IOV has already been written
0192   if (!this->fetchID()) {
0193     this->writeDB();
0194   }
0195 
0196   m_runTag.setConnection(m_env, m_conn);
0197   //  int tagID = m_runTag.writeDB();
0198 
0199   // Validate the data, use infinity-till convention
0200   DateHandler dh(m_env, m_conn);
0201 
0202   // we only update the run end here
0203   if (m_runEnd.isNull()) {
0204     m_runEnd = dh.getPlusInfTm();
0205   }
0206 
0207   try {
0208     Statement* stmt = m_conn->createStatement();
0209 
0210     stmt->setSQL("UPDATE run_iov set run_end=:1 where iov_id=:2 ");
0211     stmt->setDate(1, dh.tmToDate(m_runEnd));
0212     stmt->setInt(2, m_ID);
0213 
0214     stmt->executeUpdate();
0215 
0216     m_conn->terminateStatement(stmt);
0217   } catch (SQLException& e) {
0218     throw(std::runtime_error("RunIOV::writeDB:  " + e.getMessage()));
0219   }
0220 
0221   // Now get the ID
0222   if (!this->fetchID()) {
0223     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
0224   }
0225 
0226   return m_ID;
0227 }
0228 
0229 int RunIOV::fetchIDByRunAndTag() noexcept(false) {
0230   // Return from memory if available
0231   if (m_ID) {
0232     return m_ID;
0233   }
0234 
0235   this->checkConnection();
0236 
0237   m_runTag.setConnection(m_env, m_conn);
0238   int tagID = m_runTag.fetchID();
0239   if (!tagID) {
0240     return 0;
0241   }
0242 
0243   DateHandler dh(m_env, m_conn);
0244 
0245   if (m_runEnd.isNull()) {
0246     m_runEnd = dh.getPlusInfTm();
0247   }
0248 
0249   try {
0250     Statement* stmt = m_conn->createStatement();
0251     stmt->setSQL(
0252         "SELECT iov_id FROM run_iov "
0253         "WHERE tag_id = :tag_id AND "
0254         "run_num = :run_num ");
0255     stmt->setInt(1, tagID);
0256     stmt->setInt(2, m_runNum);
0257 
0258     ResultSet* rset = stmt->executeQuery();
0259 
0260     if (rset->next()) {
0261       m_ID = rset->getInt(1);
0262     } else {
0263       m_ID = 0;
0264     }
0265     m_conn->terminateStatement(stmt);
0266   } catch (SQLException& e) {
0267     throw(std::runtime_error("RunIOV::fetchID:  " + e.getMessage()));
0268   }
0269 
0270   return m_ID;
0271 }
0272 
0273 int RunIOV::updateStartTimeDB() noexcept(false) {
0274   this->checkConnection();
0275 
0276   // Check if this IOV has already been written
0277   if (!this->fetchIDByRunAndTag()) {
0278     this->writeDB();
0279   }
0280 
0281   //  m_runTag.setConnection(m_env, m_conn);
0282   // int tagID = m_runTag.writeDB();
0283 
0284   // Validate the data, use infinity-till convention
0285   DateHandler dh(m_env, m_conn);
0286 
0287   // we only update the run start here
0288   if (m_runEnd.isNull()) {
0289     m_runEnd = dh.getPlusInfTm();
0290   }
0291 
0292   try {
0293     Statement* stmt = m_conn->createStatement();
0294 
0295     stmt->setSQL("UPDATE run_iov set run_start=:1 where iov_id=:2 ");
0296     stmt->setDate(1, dh.tmToDate(m_runStart));
0297     stmt->setInt(2, m_ID);
0298 
0299     stmt->executeUpdate();
0300 
0301     m_conn->terminateStatement(stmt);
0302   } catch (SQLException& e) {
0303     throw(std::runtime_error("RunIOV::writeDB:  " + e.getMessage()));
0304   }
0305 
0306   // Now get the ID
0307   if (!this->fetchID()) {
0308     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
0309   }
0310 
0311   return m_ID;
0312 }
0313 
0314 void RunIOV::setByRun(RunTag* tag, run_t run) noexcept(false) {
0315   this->checkConnection();
0316 
0317   tag->setConnection(m_env, m_conn);
0318   int tagID = tag->fetchID();
0319   if (!tagID) {
0320     throw(std::runtime_error("RunIOV::setByRun:  Given tag is not in the database"));
0321   }
0322 
0323   DateHandler dh(m_env, m_conn);
0324 
0325   try {
0326     Statement* stmt = m_conn->createStatement();
0327 
0328     stmt->setSQL("SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
0329     stmt->setInt(1, tagID);
0330     stmt->setInt(2, run);
0331 
0332     ResultSet* rset = stmt->executeQuery();
0333     if (rset->next()) {
0334       m_runTag = *tag;
0335       m_runNum = run;
0336 
0337       m_ID = rset->getInt(1);
0338       Date startDate = rset->getDate(2);
0339       Date endDate = rset->getDate(3);
0340 
0341       m_runStart = dh.dateToTm(startDate);
0342       m_runEnd = dh.dateToTm(endDate);
0343     } else {
0344       throw(std::runtime_error("RunIOV::setByRun:  Given run is not in the database"));
0345     }
0346 
0347     m_conn->terminateStatement(stmt);
0348   } catch (SQLException& e) {
0349     throw(std::runtime_error("RunIOV::setByRun:  " + e.getMessage()));
0350   }
0351 }
0352 
0353 void RunIOV::setByTime(std::string location, const Tm& t) noexcept(false) {
0354   this->checkConnection();
0355 
0356   DateHandler dh(m_env, m_conn);
0357 
0358   try {
0359     Statement* stmt = m_conn->createStatement();
0360 
0361     stmt->setSQL(
0362         "SELECT iov_id FROM (SELECT iov_id FROM run_iov riov "
0363         "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
0364         "JOIN location_def loc ON rtag.location_id = loc.def_id "
0365         "WHERE loc.location = :1 AND "
0366         "run_start <= to_date(:2, 'YYYY-MM-DD HH24:MI:SS') AND "
0367         "run_end >= to_date(:3, 'YYYY-MM-DD HH24:MI:SS') "
0368         "AND rtag.gen_tag != 'INVALID' ORDER BY iov_id DESC) "
0369         "where ROWNUM <=1");
0370     stmt->setString(1, location);
0371     stmt->setString(2, t.str());
0372     stmt->setString(3, t.str());
0373 
0374     ResultSet* rset = stmt->executeQuery();
0375     if (rset->next()) {
0376       int id = rset->getInt(1);
0377       this->setByID(id);
0378     } else {
0379       throw(std::runtime_error("RunIOV::setByTime(loc, run):  Given run is not in the database"));
0380     }
0381 
0382     // Check for uniqueness of run
0383     if (rset->next()) {
0384       throw(std::runtime_error("RunIOV::setByTime(loc, run):  Run is nonunique for given location."));
0385     }
0386 
0387     m_conn->terminateStatement(stmt);
0388   } catch (SQLException& e) {
0389     throw(std::runtime_error("RunIOV::setByTime(loc, run):  " + e.getMessage()));
0390   }
0391 }
0392 
0393 void RunIOV::setByRun(std::string location, run_t run) noexcept(false) {
0394   this->checkConnection();
0395 
0396   DateHandler dh(m_env, m_conn);
0397 
0398   try {
0399     Statement* stmt = m_conn->createStatement();
0400 
0401     stmt->setSQL(
0402         "SELECT iov_id FROM run_iov riov "
0403         "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
0404         "JOIN location_def loc ON rtag.location_id = loc.def_id "
0405         "WHERE loc.location = :1 AND riov.run_num = :2 "
0406         "AND rtag.gen_tag != 'INVALID'");
0407     stmt->setString(1, location);
0408     stmt->setInt(2, run);
0409 
0410     ResultSet* rset = stmt->executeQuery();
0411     if (rset->next()) {
0412       int id = rset->getInt(1);
0413       this->setByID(id);
0414     } else {
0415       throw(std::runtime_error("RunIOV::setByRun(loc, run):  Given run is not in the database"));
0416     }
0417 
0418     // Check for uniqueness of run
0419     if (rset->next()) {
0420       throw(std::runtime_error("RunIOV::setByRun(loc, run):  Run is nonunique for given location."));
0421     }
0422 
0423     m_conn->terminateStatement(stmt);
0424   } catch (SQLException& e) {
0425     throw(std::runtime_error("RunIOV::setByRun(loc, run):  " + e.getMessage()));
0426   }
0427 }
0428 
0429 void RunIOV::setByRecentData(std::string dataTable, RunTag* tag, run_t run) noexcept(false) {
0430   this->checkConnection();
0431 
0432   tag->setConnection(m_env, m_conn);
0433   int tagID = tag->fetchID();
0434   if (!tagID) {
0435     throw(std::runtime_error("RunIOV::setByRecentData:  Given tag is not in the database"));
0436   }
0437 
0438   DateHandler dh(m_env, m_conn);
0439 
0440   try {
0441     Statement* stmt = m_conn->createStatement();
0442 
0443     stmt->setSQL(
0444         "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
0445         "FROM run_iov riov "
0446         "JOIN " +
0447         dataTable +
0448         " dat on dat.iov_id = riov.iov_id "
0449         "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
0450 
0451     stmt->setInt(1, tagID);
0452     stmt->setInt(2, run);
0453 
0454     ResultSet* rset = stmt->executeQuery();
0455     if (rset->next()) {
0456       m_runTag = *tag;
0457 
0458       m_ID = rset->getInt(1);
0459       m_runNum = rset->getInt(2);
0460       Date startDate = rset->getDate(3);
0461       Date endDate = rset->getDate(4);
0462 
0463       m_runStart = dh.dateToTm(startDate);
0464       m_runEnd = dh.dateToTm(endDate);
0465     } else {
0466       throw(std::runtime_error("RunIOV::setByRecentData:  No data exists for given tag and run"));
0467     }
0468 
0469     m_conn->terminateStatement(stmt);
0470   } catch (SQLException& e) {
0471     throw(std::runtime_error("RunIOV::setByRecentData:  " + e.getMessage()));
0472   }
0473 }
0474 
0475 void RunIOV::setByRecentData(std::string dataTable, std::string location, run_t run) noexcept(false) {
0476   this->checkConnection();
0477 
0478   DateHandler dh(m_env, m_conn);
0479 
0480   try {
0481     Statement* stmt = m_conn->createStatement();
0482 
0483     stmt->setSQL(
0484         "SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
0485         "FROM run_iov riov "
0486         "JOIN " +
0487         dataTable +
0488         " dat on dat.iov_id = riov.iov_id "
0489         "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
0490         "JOIN location_def loc ON rtag.location_id = loc.def_id "
0491         "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
0492 
0493     stmt->setString(1, location);
0494     stmt->setInt(2, run);
0495 
0496     ResultSet* rset = stmt->executeQuery();
0497 
0498     if (rset->next()) {
0499       int id = rset->getInt(1);
0500       this->setByID(id);
0501     } else {
0502       throw(std::runtime_error("RunIOV::setByRecentData(datatable, loc, run):  Given run is not in the database"));
0503     }
0504 
0505     m_conn->terminateStatement(stmt);
0506   } catch (SQLException& e) {
0507     throw(std::runtime_error("RunIOV::setByRecentData:  " + e.getMessage()));
0508   }
0509 }