Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:57:24

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/DCUIOV.h"
0005 #include "OnlineDB/EcalCondDB/interface/DCUTag.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 DCUIOV::DCUIOV() {
0013   m_conn = nullptr;
0014   m_ID = 0;
0015   m_since = Tm();
0016   m_till = Tm();
0017 }
0018 
0019 DCUIOV::~DCUIOV() {}
0020 
0021 void DCUIOV::setSince(const Tm& since) {
0022   if (since != m_since) {
0023     m_ID = 0;
0024     m_since = since;
0025   }
0026 }
0027 
0028 Tm DCUIOV::getSince() const { return m_since; }
0029 
0030 void DCUIOV::setTill(const Tm& till) {
0031   if (till != m_till) {
0032     m_ID = 0;
0033     m_till = till;
0034   }
0035 }
0036 
0037 Tm DCUIOV::getTill() const { return m_till; }
0038 
0039 void DCUIOV::setDCUTag(const DCUTag& tag) {
0040   if (tag != m_dcuTag) {
0041     m_ID = 0;
0042     m_dcuTag = tag;
0043   }
0044 }
0045 
0046 DCUTag DCUIOV::getDCUTag() const { return m_dcuTag; }
0047 
0048 int DCUIOV::fetchID() noexcept(false) {
0049   // Return from memory if available
0050   if (m_ID) {
0051     return m_ID;
0052   }
0053 
0054   this->checkConnection();
0055 
0056   m_dcuTag.setConnection(m_env, m_conn);
0057   int tagID = m_dcuTag.fetchID();
0058   if (!tagID) {
0059     return 0;
0060   }
0061 
0062   DateHandler dh(m_env, m_conn);
0063 
0064   if (m_till.isNull()) {
0065     m_till = dh.getPlusInfTm();
0066   }
0067 
0068   try {
0069     Statement* stmt = m_conn->createStatement();
0070     stmt->setSQL(
0071         "SELECT iov_id FROM dcu_iov "
0072         "WHERE tag_id = :tag_id AND "
0073         "since = :since ");
0074     stmt->setInt(1, tagID);
0075     stmt->setDate(2, dh.tmToDate(m_since));
0076     //    stmt->setDate(3, dh.tmToDate(m_till));
0077 
0078     ResultSet* rset = stmt->executeQuery();
0079 
0080     if (rset->next()) {
0081       m_ID = rset->getInt(1);
0082     } else {
0083       m_ID = 0;
0084     }
0085     m_conn->terminateStatement(stmt);
0086   } catch (SQLException& e) {
0087     throw(std::runtime_error("DCUIOV::fetchID:  " + e.getMessage()));
0088   }
0089 
0090   return m_ID;
0091 }
0092 
0093 void DCUIOV::setByID(int id) noexcept(false) {
0094   this->checkConnection();
0095 
0096   DateHandler dh(m_env, m_conn);
0097 
0098   try {
0099     Statement* stmt = m_conn->createStatement();
0100 
0101     stmt->setSQL("SELECT tag_id, since, till FROM dcu_iov WHERE iov_id = :1");
0102     stmt->setInt(1, id);
0103 
0104     ResultSet* rset = stmt->executeQuery();
0105     if (rset->next()) {
0106       int tagID = rset->getInt(1);
0107       Date since = rset->getDate(2);
0108       Date till = rset->getDate(3);
0109 
0110       m_since = dh.dateToTm(since);
0111       m_till = dh.dateToTm(till);
0112 
0113       m_dcuTag.setConnection(m_env, m_conn);
0114       m_dcuTag.setByID(tagID);
0115       m_ID = id;
0116     } else {
0117       throw(std::runtime_error("DCUTag::setByID:  Given tag_id is not in the database"));
0118     }
0119 
0120     m_conn->terminateStatement(stmt);
0121   } catch (SQLException& e) {
0122     throw(std::runtime_error("DCUTag::setByID:  " + e.getMessage()));
0123   }
0124 }
0125 
0126 int DCUIOV::writeDB() noexcept(false) {
0127   this->checkConnection();
0128 
0129   // Check if this IOV has already been written
0130   if (this->fetchID()) {
0131     return m_ID;
0132   }
0133 
0134   m_dcuTag.setConnection(m_env, m_conn);
0135   int tagID = m_dcuTag.writeDB();
0136 
0137   // Validate the data, use infinity-till convention
0138   DateHandler dh(m_env, m_conn);
0139 
0140   if (m_since.isNull()) {
0141     throw(std::runtime_error("DCUIOV::writeDB:  Must setSince before writing"));
0142   }
0143 
0144   if (m_till.isNull()) {
0145     m_till = dh.getPlusInfTm();
0146   }
0147 
0148   try {
0149     Statement* stmt = m_conn->createStatement();
0150 
0151     stmt->setSQL(
0152         "INSERT INTO dcu_iov (iov_id, tag_id, since, till) "
0153         "VALUES (dcu_iov_sq.NextVal, :1, :2, :3)");
0154     stmt->setInt(1, tagID);
0155     stmt->setDate(2, dh.tmToDate(m_since));
0156     stmt->setDate(3, dh.tmToDate(m_till));
0157 
0158     stmt->executeUpdate();
0159 
0160     m_conn->terminateStatement(stmt);
0161   } catch (SQLException& e) {
0162     throw(std::runtime_error("DCUIOV::writeDB:  " + e.getMessage()));
0163   }
0164 
0165   // Now get the ID
0166   if (!this->fetchID()) {
0167     throw(std::runtime_error("DCUIOV::writeDB:  Failed to write"));
0168   }
0169 
0170   return m_ID;
0171 }
0172 
0173 void DCUIOV::setByTm(DCUTag* tag, const Tm& eventTm) noexcept(false) {
0174   this->checkConnection();
0175 
0176   tag->setConnection(m_env, m_conn);
0177   int tagID = tag->fetchID();
0178 
0179   if (!tagID) {
0180     throw(std::runtime_error("DCUIOV::setByTm:  Given DCUTag does not exist in the DB"));
0181   }
0182 
0183   DateHandler dh(m_env, m_conn);
0184 
0185   Date eventDate = dh.tmToDate(eventTm);
0186 
0187   try {
0188     Statement* stmt = m_conn->createStatement();
0189 
0190     stmt->setSQL(
0191         "SELECT iov_id, since, till FROM dcu_iov "
0192         "WHERE tag_id = :1 AND since <= :2 AND till > :3");
0193     stmt->setInt(1, tagID);
0194     stmt->setDate(2, eventDate);
0195     stmt->setDate(3, eventDate);
0196 
0197     ResultSet* rset = stmt->executeQuery();
0198     if (rset->next()) {
0199       m_dcuTag = *tag;
0200 
0201       m_ID = rset->getInt(1);
0202       Date sinceDate = rset->getDate(2);
0203       Date tillDate = rset->getDate(3);
0204 
0205       m_since = dh.dateToTm(sinceDate);
0206       m_till = dh.dateToTm(tillDate);
0207     } else {
0208       throw(std::runtime_error("DCUIOV::setByTm:  Given subrun is not in the database"));
0209     }
0210 
0211     m_conn->terminateStatement(stmt);
0212   } catch (SQLException& e) {
0213     throw(std::runtime_error("DCUIOV::setByTm:  " + e.getMessage()));
0214   }
0215 }