File indexing completed on 2024-04-06 12:23:03
0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigLinInfo.h"
0007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0008 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0009
0010 using namespace std;
0011 using namespace oracle::occi;
0012
0013 FEConfigLinInfo::FEConfigLinInfo() {
0014 m_env = nullptr;
0015 m_conn = nullptr;
0016 m_writeStmt = nullptr;
0017 m_readStmt = nullptr;
0018 m_config_tag = "";
0019 m_version = 0;
0020 m_ID = 0;
0021 clear();
0022 }
0023
0024 void FEConfigLinInfo::clear() { m_iov_id = 0; }
0025
0026 FEConfigLinInfo::~FEConfigLinInfo() {}
0027
0028 int FEConfigLinInfo::fetchNextId() noexcept(false) {
0029 int result = 0;
0030 try {
0031 this->checkConnection();
0032
0033 m_readStmt = m_conn->createStatement();
0034 m_readStmt->setSQL("select FE_CONFIG_LIN_SQ.NextVal from DUAL ");
0035 ResultSet* rset = m_readStmt->executeQuery();
0036 while (rset->next()) {
0037 result = rset->getInt(1);
0038 }
0039 result++;
0040 m_conn->terminateStatement(m_readStmt);
0041 return result;
0042
0043 } catch (SQLException& e) {
0044 throw(std::runtime_error(std::string("FEConfigLinInfo::fetchNextId(): ") + e.getMessage()));
0045 }
0046 }
0047
0048 void FEConfigLinInfo::prepareWrite() noexcept(false) {
0049 this->checkConnection();
0050
0051 int next_id = 0;
0052 if (getId() == 0) {
0053 next_id = fetchNextId();
0054 }
0055
0056 try {
0057 m_writeStmt = m_conn->createStatement();
0058 m_writeStmt->setSQL("INSERT INTO " + getTable() +
0059 " ( lin_conf_id, tag, version, iov_id) "
0060 " VALUES ( :1, :2, :3 , :4) ");
0061
0062 m_writeStmt->setInt(1, next_id);
0063 m_ID = next_id;
0064
0065 } catch (SQLException& e) {
0066 throw(std::runtime_error(std::string("FEConfigLinInfo::prepareWrite(): ") + e.getMessage()));
0067 }
0068 }
0069
0070 void FEConfigLinInfo::setParameters(const std::map<string, string>& my_keys_map) {
0071
0072
0073
0074 for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0075 if (ci->first == "VERSION")
0076 setVersion(atoi(ci->second.c_str()));
0077 if (ci->first == "TAG")
0078 setConfigTag(ci->second);
0079 if (ci->first == "IOV_ID")
0080 setIOVId(atoi(ci->second.c_str()));
0081 }
0082 }
0083
0084 void FEConfigLinInfo::writeDB() noexcept(false) {
0085 this->checkConnection();
0086 this->checkPrepare();
0087
0088 try {
0089
0090 m_writeStmt->setString(2, this->getConfigTag());
0091 m_writeStmt->setInt(3, this->getVersion());
0092 m_writeStmt->setInt(4, this->getIOVId());
0093
0094 m_writeStmt->executeUpdate();
0095
0096 } catch (SQLException& e) {
0097 throw(std::runtime_error(std::string("FEConfigLinInfo::writeDB(): ") + e.getMessage()));
0098 }
0099
0100 if (!this->fetchID()) {
0101 throw(std::runtime_error("FEConfigLinInfo::writeDB: Failed to write"));
0102 }
0103 }
0104
0105 void FEConfigLinInfo::fetchData(FEConfigLinInfo* result) noexcept(false) {
0106 this->checkConnection();
0107 result->clear();
0108 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0109 throw(std::runtime_error("FEConfigLinInfo::fetchData(): no Id defined for this FEConfigLinInfo "));
0110 }
0111
0112 try {
0113 DateHandler dh(m_env, m_conn);
0114
0115 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where ( lin_conf_id= :1 or (tag=:2 AND version=:3 ) )");
0116 m_readStmt->setInt(1, result->getId());
0117 m_readStmt->setString(2, result->getConfigTag());
0118 m_readStmt->setInt(3, result->getVersion());
0119 ResultSet* rset = m_readStmt->executeQuery();
0120
0121 rset->next();
0122
0123
0124
0125 result->setId(rset->getInt(1));
0126 result->setConfigTag(rset->getString(2));
0127 result->setVersion(rset->getInt(3));
0128 result->setIOVId(rset->getInt(4));
0129 Date dbdate = rset->getDate(5);
0130 result->setDBTime(dh.dateToTm(dbdate));
0131
0132 } catch (SQLException& e) {
0133 throw(std::runtime_error(std::string("FEConfigLinInfo::fetchData(): ") + e.getMessage()));
0134 }
0135 }
0136
0137 void FEConfigLinInfo::fetchLastData(FEConfigLinInfo* result) noexcept(false) {
0138 this->checkConnection();
0139 result->clear();
0140 try {
0141 DateHandler dh(m_env, m_conn);
0142
0143 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where lin_conf_id = ( select max( lin_conf_id) from " +
0144 getTable() + " ) ");
0145 ResultSet* rset = m_readStmt->executeQuery();
0146
0147 rset->next();
0148
0149 result->setId(rset->getInt(1));
0150 result->setConfigTag(rset->getString(2));
0151 result->setVersion(rset->getInt(3));
0152 result->setIOVId(rset->getInt(4));
0153 Date dbdate = rset->getDate(5);
0154 result->setDBTime(dh.dateToTm(dbdate));
0155
0156 } catch (SQLException& e) {
0157 throw(std::runtime_error(std::string("FEConfigLinInfo::fetchData(): ") + e.getMessage()));
0158 }
0159 }
0160
0161 int FEConfigLinInfo::fetchID() noexcept(false) {
0162
0163 if (m_ID != 0) {
0164 return m_ID;
0165 }
0166
0167 this->checkConnection();
0168
0169 try {
0170 Statement* stmt = m_conn->createStatement();
0171 stmt->setSQL("SELECT lin_conf_id FROM " + getTable() + " WHERE tag=:1 and version=:2 ");
0172
0173 stmt->setString(1, getConfigTag());
0174 stmt->setInt(2, getVersion());
0175
0176 ResultSet* rset = stmt->executeQuery();
0177
0178 if (rset->next()) {
0179 m_ID = rset->getInt(1);
0180 } else {
0181 m_ID = 0;
0182 }
0183 m_conn->terminateStatement(stmt);
0184 } catch (SQLException& e) {
0185 throw(std::runtime_error(std::string("FEConfigLinInfo::fetchID: ") + e.getMessage()));
0186 }
0187
0188 return m_ID;
0189 }
0190
0191 void FEConfigLinInfo::setByID(int id) noexcept(false) {
0192 this->checkConnection();
0193
0194 DateHandler dh(m_env, m_conn);
0195
0196 try {
0197 Statement* stmt = m_conn->createStatement();
0198
0199 stmt->setSQL("SELECT * FROM fe_config_lin_info WHERE lin_conf_id = :1");
0200 stmt->setInt(1, id);
0201
0202 ResultSet* rset = stmt->executeQuery();
0203 if (rset->next()) {
0204 this->setId(rset->getInt(1));
0205 this->setConfigTag(rset->getString(2));
0206 this->setVersion(rset->getInt(3));
0207 this->setIOVId(rset->getInt(4));
0208 Date dbdate = rset->getDate(5);
0209 this->setDBTime(dh.dateToTm(dbdate));
0210 } else {
0211 throw(std::runtime_error("FEConfigLinInfo::setByID: Given config_id is not in the database"));
0212 }
0213
0214 m_conn->terminateStatement(stmt);
0215 } catch (SQLException& e) {
0216 throw(std::runtime_error(std::string("FEConfigLinInfo::setByID: ") + e.getMessage()));
0217 }
0218 }