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