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