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