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