File indexing completed on 2024-04-06 12:23:08
0001 #include <string>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003
0004 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
0005
0006 using namespace std;
0007 using namespace oracle::occi;
0008
0009 MonRunTag::MonRunTag() {
0010 m_env = nullptr;
0011 m_conn = nullptr;
0012 m_ID = 0;
0013 m_genTag = "default";
0014 m_monVersionDef = MonVersionDef();
0015 }
0016
0017 MonRunTag::~MonRunTag() {}
0018
0019 string MonRunTag::getGeneralTag() const { return m_genTag; }
0020
0021 void MonRunTag::setGeneralTag(string genTag) {
0022 if (genTag != m_genTag) {
0023 m_ID = 0;
0024 m_genTag = genTag;
0025 }
0026 }
0027
0028 MonVersionDef MonRunTag::getMonVersionDef() const { return m_monVersionDef; }
0029
0030 void MonRunTag::setMonVersionDef(const MonVersionDef& ver) {
0031 if (ver != m_monVersionDef) {
0032 m_ID = 0;
0033 m_monVersionDef = ver;
0034 }
0035 }
0036
0037 int MonRunTag::fetchID() noexcept(false) {
0038
0039 if (m_ID) {
0040 return m_ID;
0041 }
0042
0043 this->checkConnection();
0044
0045
0046 int verID;
0047 this->fetchParentIDs(&verID);
0048
0049
0050 try {
0051 Statement* stmt = m_conn->createStatement();
0052 stmt->setSQL(
0053 "SELECT tag_id FROM mon_run_tag WHERE "
0054 "gen_tag = :1 AND "
0055 "mon_ver_id = :2");
0056
0057 stmt->setString(1, m_genTag);
0058 stmt->setInt(2, verID);
0059
0060 ResultSet* rset = stmt->executeQuery();
0061
0062 if (rset->next()) {
0063 m_ID = rset->getInt(1);
0064 } else {
0065 m_ID = 0;
0066 }
0067 m_conn->terminateStatement(stmt);
0068 } catch (SQLException& e) {
0069 throw(std::runtime_error("MonRunTag::fetchID: " + e.getMessage()));
0070 }
0071
0072 return m_ID;
0073 }
0074
0075 void MonRunTag::setByID(int id) noexcept(false) {
0076 this->checkConnection();
0077
0078 try {
0079 Statement* stmt = m_conn->createStatement();
0080
0081 stmt->setSQL("SELECT gen_tag, mon_ver_id FROM mon_run_tag WHERE tag_id = :1");
0082 stmt->setInt(1, id);
0083
0084 ResultSet* rset = stmt->executeQuery();
0085 if (rset->next()) {
0086 m_genTag = rset->getString(1);
0087 int verID = rset->getInt(2);
0088 m_monVersionDef.setByID(verID);
0089 m_ID = id;
0090 } else {
0091 throw(std::runtime_error("MonRunTag::setByID: Given tag_id is not in the database"));
0092 }
0093
0094 m_conn->terminateStatement(stmt);
0095 } catch (SQLException& e) {
0096 throw(std::runtime_error("MonRunTag::setByID: " + e.getMessage()));
0097 }
0098 }
0099
0100 int MonRunTag::writeDB() noexcept(false) {
0101
0102 if (this->fetchID()) {
0103 return m_ID;
0104 }
0105
0106
0107 this->checkConnection();
0108
0109
0110 int verID;
0111 this->fetchParentIDs(&verID);
0112
0113
0114 try {
0115 Statement* stmt = m_conn->createStatement();
0116
0117 stmt->setSQL(
0118 "INSERT INTO mon_run_tag (tag_id, gen_tag, mon_ver_id) "
0119 "VALUES (mon_run_tag_sq.NextVal, :1, :2)");
0120 stmt->setString(1, m_genTag);
0121 stmt->setInt(2, verID);
0122
0123 stmt->executeUpdate();
0124
0125 m_conn->terminateStatement(stmt);
0126 } catch (SQLException& e) {
0127 throw(std::runtime_error("MonRunTag::writeDB: " + e.getMessage()));
0128 }
0129
0130
0131 if (!this->fetchID()) {
0132 throw(std::runtime_error("MonRunTag::writeDB: Failed to write"));
0133 }
0134
0135 return m_ID;
0136 }
0137
0138 void MonRunTag::fetchAllTags(std::vector<MonRunTag>* fillVec) noexcept(false) {
0139 this->checkConnection();
0140 try {
0141 Statement* stmt = m_conn->createStatement();
0142 stmt->setSQL("SELECT tag_id FROM mon_run_tag ORDER BY tag_id");
0143 ResultSet* rset = stmt->executeQuery();
0144
0145 MonRunTag runtag;
0146 runtag.setConnection(m_env, m_conn);
0147 while (rset->next()) {
0148 runtag.setByID(rset->getInt(1));
0149 fillVec->push_back(runtag);
0150 }
0151 m_conn->terminateStatement(stmt);
0152 } catch (SQLException& e) {
0153 throw(std::runtime_error("MonRunTag::fetchAllTags: " + e.getMessage()));
0154 }
0155 }
0156
0157 void MonRunTag::fetchParentIDs(int* verID) noexcept(false) {
0158
0159 m_monVersionDef.setConnection(m_env, m_conn);
0160 *verID = m_monVersionDef.fetchID();
0161
0162 if (!*verID) {
0163 throw(std::runtime_error("MonRunTag::writeDB: Given monitoring version does not exist in DB"));
0164 }
0165 }