File indexing completed on 2023-03-17 11:15:15
0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigSpikeInfo.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 FEConfigSpikeInfo::FEConfigSpikeInfo() {
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 FEConfigSpikeInfo::clear() {}
0025
0026 FEConfigSpikeInfo::~FEConfigSpikeInfo() {}
0027
0028 int FEConfigSpikeInfo::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_SPI_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("FEConfigSpikeInfo::fetchNextId(): ") + e.getMessage()));
0045 }
0046 }
0047
0048 void FEConfigSpikeInfo::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 " ( spi_conf_id, tag, version ) "
0060 " VALUES ( :1, :2, :3 ) ");
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("FEConfigSpikeInfo::prepareWrite(): ") + e.getMessage()));
0067 }
0068 }
0069
0070 void FEConfigSpikeInfo::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 }
0080 }
0081
0082 void FEConfigSpikeInfo::writeDB() noexcept(false) {
0083 this->checkConnection();
0084 this->checkPrepare();
0085
0086 try {
0087
0088 m_writeStmt->setString(2, this->getConfigTag());
0089 m_writeStmt->setInt(3, this->getVersion());
0090
0091 m_writeStmt->executeUpdate();
0092
0093 } catch (SQLException& e) {
0094 throw(std::runtime_error(std::string("FEConfigSpikeInfo::writeDB(): ") + e.getMessage()));
0095 }
0096
0097 if (!this->fetchID()) {
0098 throw(std::runtime_error("FEConfigSpikeInfo::writeDB: Failed to write"));
0099 }
0100 }
0101
0102 void FEConfigSpikeInfo::fetchData(FEConfigSpikeInfo* result) noexcept(false) {
0103 this->checkConnection();
0104 result->clear();
0105 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0106 throw(std::runtime_error("FEConfigSpikeInfo::fetchData(): no Id defined for this FEConfigSpikeInfo "));
0107 }
0108
0109 try {
0110 DateHandler dh(m_env, m_conn);
0111
0112 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where ( spi_conf_id= :1 or (tag=:2 AND version=:3 ) )");
0113 m_readStmt->setInt(1, result->getId());
0114 m_readStmt->setString(2, result->getConfigTag());
0115 m_readStmt->setInt(3, result->getVersion());
0116 ResultSet* rset = m_readStmt->executeQuery();
0117
0118 rset->next();
0119
0120
0121
0122 result->setId(rset->getInt(1));
0123 result->setConfigTag(rset->getString(2));
0124 result->setVersion(rset->getInt(3));
0125 Date dbdate = rset->getDate(4);
0126 result->setDBTime(dh.dateToTm(dbdate));
0127
0128 } catch (SQLException& e) {
0129 throw(std::runtime_error(std::string("FEConfigSpikeInfo::fetchData(): ") + e.getMessage()));
0130 }
0131 }
0132
0133 void FEConfigSpikeInfo::fetchLastData(FEConfigSpikeInfo* result) noexcept(false) {
0134 this->checkConnection();
0135 result->clear();
0136 try {
0137 DateHandler dh(m_env, m_conn);
0138
0139 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where spi_conf_id = ( select max( spi_conf_id) from " +
0140 getTable() + " ) ");
0141 ResultSet* rset = m_readStmt->executeQuery();
0142
0143 rset->next();
0144
0145 result->setId(rset->getInt(1));
0146 result->setConfigTag(rset->getString(2));
0147 result->setVersion(rset->getInt(3));
0148 Date dbdate = rset->getDate(4);
0149 result->setDBTime(dh.dateToTm(dbdate));
0150
0151 } catch (SQLException& e) {
0152 throw(std::runtime_error(std::string("FEConfigSpikeInfo::fetchData(): ") + e.getMessage()));
0153 }
0154 }
0155
0156 int FEConfigSpikeInfo::fetchID() noexcept(false) {
0157
0158 if (m_ID != 0) {
0159 return m_ID;
0160 }
0161
0162 this->checkConnection();
0163
0164 try {
0165 Statement* stmt = m_conn->createStatement();
0166 stmt->setSQL("SELECT spi_conf_id FROM " + getTable() + " WHERE tag=:1 and version=:2 ");
0167
0168 stmt->setString(1, getConfigTag());
0169 stmt->setInt(2, getVersion());
0170
0171 ResultSet* rset = stmt->executeQuery();
0172
0173 if (rset->next()) {
0174 m_ID = rset->getInt(1);
0175 } else {
0176 m_ID = 0;
0177 }
0178 m_conn->terminateStatement(stmt);
0179 } catch (SQLException& e) {
0180 throw(std::runtime_error(std::string("FEConfigSpikeInfo::fetchID: ") + e.getMessage()));
0181 }
0182
0183 return m_ID;
0184 }
0185
0186 void FEConfigSpikeInfo::setByID(int id) noexcept(false) {
0187 this->checkConnection();
0188
0189 DateHandler dh(m_env, m_conn);
0190
0191 try {
0192 Statement* stmt = m_conn->createStatement();
0193
0194 stmt->setSQL("SELECT * FROM " + getTable() + " WHERE spi_conf_id = :1");
0195 stmt->setInt(1, id);
0196
0197 ResultSet* rset = stmt->executeQuery();
0198 if (rset->next()) {
0199 this->setId(rset->getInt(1));
0200 this->setConfigTag(rset->getString(2));
0201 this->setVersion(rset->getInt(3));
0202 Date dbdate = rset->getDate(4);
0203 this->setDBTime(dh.dateToTm(dbdate));
0204 } else {
0205 throw(std::runtime_error("FEConfigSpikeInfo::setByID: Given spi_conf_id is not in the database"));
0206 }
0207
0208 m_conn->terminateStatement(stmt);
0209 } catch (SQLException& e) {
0210 throw(std::runtime_error(std::string("FEConfigSpikeInfo::setByID: ") + e.getMessage()));
0211 }
0212 }