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