Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/MODRunIOV.h"
0005 #include "OnlineDB/EcalCondDB/interface/RunIOV.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 MODRunIOV::MODRunIOV() {
0013   m_conn = nullptr;
0014   m_ID = 0;
0015   m_runIOV = RunIOV();
0016   m_subRunNum = 0;
0017   m_subRunStart = Tm();
0018   m_subRunEnd = Tm();
0019 }
0020 
0021 MODRunIOV::~MODRunIOV() {}
0022 
0023 void MODRunIOV::setID(int id) { m_ID = id; }
0024 
0025 void MODRunIOV::setRunIOV(const RunIOV& iov) {
0026   if (iov != m_runIOV) {
0027     m_ID = 0;
0028     m_runIOV = iov;
0029   }
0030 }
0031 
0032 RunIOV MODRunIOV::getRunIOV() { return m_runIOV; }
0033 
0034 void MODRunIOV::setSubRunNumber(subrun_t subrun) {
0035   if (subrun != m_subRunNum) {
0036     m_ID = 0;
0037     m_subRunNum = subrun;
0038   }
0039 }
0040 
0041 run_t MODRunIOV::getSubRunNumber() const { return m_subRunNum; }
0042 
0043 void MODRunIOV::setSubRunStart(const Tm& start) {
0044   if (start != m_subRunStart) {
0045     m_ID = 0;
0046     m_subRunStart = start;
0047   }
0048 }
0049 
0050 Tm MODRunIOV::getSubRunStart() const { return m_subRunStart; }
0051 
0052 void MODRunIOV::setSubRunEnd(const Tm& end) {
0053   if (end != m_subRunEnd) {
0054     m_ID = 0;
0055     m_subRunEnd = end;
0056   }
0057 }
0058 
0059 Tm MODRunIOV::getSubRunEnd() const { return m_subRunEnd; }
0060 
0061 int MODRunIOV::fetchID() noexcept(false) {
0062   // Return from memory if available
0063   if (m_ID) {
0064     return m_ID;
0065   }
0066 
0067   this->checkConnection();
0068 
0069   // fetch the parent IDs
0070   int runIOVID;
0071   this->fetchParentIDs(&runIOVID);
0072 
0073   if (!runIOVID) {
0074     return 0;
0075   }
0076 
0077   DateHandler dh(m_env, m_conn);
0078 
0079   if (m_subRunEnd.isNull()) {
0080     m_subRunEnd = dh.getPlusInfTm();
0081   }
0082 
0083   try {
0084     Statement* stmt = m_conn->createStatement();
0085     stmt->setSQL(
0086         "SELECT iov_id FROM OD_run_iov "
0087         "WHERE "
0088         "run_iov_id   = :1 AND "
0089         "subrun_num   = :2 AND "
0090         "subrun_start = :3 AND "
0091         "subrun_end   = :4");
0092 
0093     stmt->setInt(1, runIOVID);
0094     stmt->setInt(2, m_subRunNum);
0095     stmt->setDate(3, dh.tmToDate(m_subRunStart));
0096     stmt->setDate(4, dh.tmToDate(m_subRunEnd));
0097 
0098     ResultSet* rset = stmt->executeQuery();
0099 
0100     if (rset->next()) {
0101       m_ID = rset->getInt(1);
0102     } else {
0103       m_ID = 0;
0104     }
0105     m_conn->terminateStatement(stmt);
0106   } catch (SQLException& e) {
0107     throw(std::runtime_error("MODRunIOV::fetchID:  " + e.getMessage()));
0108   }
0109 
0110   return m_ID;
0111 }
0112 
0113 void MODRunIOV::setByID(int id) noexcept(false) {
0114   this->checkConnection();
0115 
0116   DateHandler dh(m_env, m_conn);
0117 
0118   try {
0119     Statement* stmt = m_conn->createStatement();
0120 
0121     stmt->setSQL("SELECT run_iov_id, subrun_num, subrun_start, subrun_end FROM OD_run_iov WHERE iov_id = :1");
0122     stmt->setInt(1, id);
0123 
0124     ResultSet* rset = stmt->executeQuery();
0125     if (rset->next()) {
0126       int runIOVID = rset->getInt(1);
0127       m_subRunNum = rset->getInt(2);
0128       Date startDate = rset->getDate(3);
0129       Date endDate = rset->getDate(4);
0130 
0131       m_subRunStart = dh.dateToTm(startDate);
0132       m_subRunEnd = dh.dateToTm(endDate);
0133 
0134       m_runIOV.setConnection(m_env, m_conn);
0135       m_runIOV.setByID(runIOVID);
0136 
0137       m_ID = id;
0138     } else {
0139       throw(std::runtime_error("MODRunIOV::setByID:  Given id is not in the database"));
0140     }
0141 
0142     m_conn->terminateStatement(stmt);
0143   } catch (SQLException& e) {
0144     throw(std::runtime_error("MODRunIOV::setByID:  " + e.getMessage()));
0145   }
0146 }
0147 
0148 int MODRunIOV::writeDB() noexcept(false) {
0149   this->checkConnection();
0150 
0151   // Check if this IOV has already been written
0152   if (this->fetchID()) {
0153     return m_ID;
0154   }
0155 
0156   // fetch Parent IDs
0157   int runIOVID;
0158   this->fetchParentIDs(&runIOVID);
0159 
0160   // Validate the data, use infinity-till convention
0161   DateHandler dh(m_env, m_conn);
0162 
0163   if (m_subRunStart.isNull()) {
0164     throw(std::runtime_error("MODRunIOV::writeDB:  Must setSubRunStart before writing"));
0165   }
0166 
0167   if (m_subRunEnd.isNull()) {
0168     m_subRunEnd = dh.getPlusInfTm();
0169   }
0170 
0171   try {
0172     Statement* stmt = m_conn->createStatement();
0173 
0174     stmt->setSQL(
0175         "INSERT INTO od_run_iov (iov_id,  run_iov_id, subrun_num, subrun_start, subrun_end) "
0176         "VALUES (OD_run_iov_sq.NextVal, :1, :2, :3, :4)");
0177     stmt->setInt(1, runIOVID);
0178     stmt->setInt(2, m_subRunNum);
0179     stmt->setDate(3, dh.tmToDate(m_subRunStart));
0180     stmt->setDate(4, dh.tmToDate(m_subRunEnd));
0181 
0182     stmt->executeUpdate();
0183 
0184     m_conn->terminateStatement(stmt);
0185   } catch (SQLException& e) {
0186     throw(std::runtime_error("MODRunIOV::writeDB:  " + e.getMessage()));
0187   }
0188 
0189   // Now get the ID
0190   if (!this->fetchID()) {
0191     throw(std::runtime_error("MODRunIOV::writeDB:  Failed to write"));
0192   }
0193 
0194   return m_ID;
0195 }
0196 
0197 void MODRunIOV::fetchParentIDs(int* runIOVID) noexcept(false) {
0198   // get the RunIOV
0199   m_runIOV.setConnection(m_env, m_conn);
0200   *runIOVID = m_runIOV.fetchID();
0201 
0202   if (!*runIOVID) {
0203     throw(std::runtime_error("MODRunIOV:  Given RunIOV does not exist in DB"));
0204   }
0205 }
0206 
0207 void MODRunIOV::setByRun(RunIOV* runiov, subrun_t subrun) noexcept(false) {
0208   this->checkConnection();
0209 
0210   runiov->setConnection(m_env, m_conn);
0211   int runIOVID = runiov->fetchID();
0212 
0213   if (!runIOVID) {
0214     throw(std::runtime_error("MODRunIOV::setByRun:  Given RunIOV does not exist in DB"));
0215   }
0216 
0217   DateHandler dh(m_env, m_conn);
0218 
0219   try {
0220     Statement* stmt = m_conn->createStatement();
0221 
0222     stmt->setSQL(
0223         "SELECT iov_id, subrun_start, subrun_end FROM OD_run_iov "
0224         "WHERE run_iov_id = :1 AND subrun_num = :2");
0225     stmt->setInt(1, runIOVID);
0226     stmt->setInt(2, subrun);
0227 
0228     ResultSet* rset = stmt->executeQuery();
0229     if (rset->next()) {
0230       m_runIOV = *runiov;
0231       m_subRunNum = subrun;
0232 
0233       m_ID = rset->getInt(1);
0234       Date startDate = rset->getDate(2);
0235       Date endDate = rset->getDate(3);
0236 
0237       m_subRunStart = dh.dateToTm(startDate);
0238       m_subRunEnd = dh.dateToTm(endDate);
0239     } else {
0240       throw(std::runtime_error("MODRunIOV::setByRun:  Given subrun is not in the database"));
0241     }
0242 
0243     m_conn->terminateStatement(stmt);
0244   } catch (SQLException& e) {
0245     throw(std::runtime_error("MODRunIOV::setByRun:  " + e.getMessage()));
0246   }
0247 }