File indexing completed on 2024-04-06 12:23:09
0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/ODBadTTInfo.h"
0007
0008 using namespace std;
0009 using namespace oracle::occi;
0010
0011 ODBadTTInfo::ODBadTTInfo() {
0012 m_env = nullptr;
0013 m_conn = nullptr;
0014 m_writeStmt = nullptr;
0015 m_readStmt = nullptr;
0016 m_config_tag = "";
0017 m_ID = 0;
0018 m_version = 0;
0019 clear();
0020 }
0021
0022 void ODBadTTInfo::clear() {}
0023
0024 ODBadTTInfo::~ODBadTTInfo() {}
0025
0026 int ODBadTTInfo::fetchNextId() noexcept(false) {
0027 int result = 0;
0028 try {
0029 this->checkConnection();
0030
0031 m_readStmt = m_conn->createStatement();
0032 m_readStmt->setSQL("select COND2CONF_INFO_SQ.NextVal from DUAL ");
0033 ResultSet* rset = m_readStmt->executeQuery();
0034 while (rset->next()) {
0035 result = rset->getInt(1);
0036 }
0037 result++;
0038 m_conn->terminateStatement(m_readStmt);
0039 return result;
0040
0041 } catch (SQLException& e) {
0042 throw(std::runtime_error(std::string("ODBadTTInfo::fetchNextId(): ") + e.getMessage()));
0043 }
0044 }
0045
0046 void ODBadTTInfo::prepareWrite() noexcept(false) {
0047 this->checkConnection();
0048
0049 int next_id = 0;
0050 if (getId() == 0) {
0051 next_id = fetchNextId();
0052 }
0053
0054 try {
0055 m_writeStmt = m_conn->createStatement();
0056 m_writeStmt->setSQL("INSERT INTO " + getTable() +
0057 " ( rec_id, tag, version) "
0058 " VALUES ( :1, :2, :3 ) ");
0059
0060 m_writeStmt->setInt(1, next_id);
0061 m_ID = next_id;
0062
0063 } catch (SQLException& e) {
0064 throw(std::runtime_error(std::string("ODBadTTInfo::prepareWrite(): ") + e.getMessage()));
0065 }
0066 }
0067
0068 void ODBadTTInfo::setParameters(const std::map<string, string>& my_keys_map) {
0069
0070
0071
0072 for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0073 if (ci->first == "VERSION")
0074 setVersion(atoi(ci->second.c_str()));
0075 if (ci->first == "TAG")
0076 setConfigTag(ci->second);
0077 }
0078 }
0079
0080 void ODBadTTInfo::writeDB() noexcept(false) {
0081 this->checkConnection();
0082 this->checkPrepare();
0083
0084 try {
0085
0086 m_writeStmt->setString(2, this->getConfigTag());
0087 m_writeStmt->setInt(3, this->getVersion());
0088
0089 m_writeStmt->executeUpdate();
0090
0091 } catch (SQLException& e) {
0092 throw(std::runtime_error(std::string("ODBadTTInfo::writeDB(): ") + e.getMessage()));
0093 }
0094
0095 if (!this->fetchID()) {
0096 throw(std::runtime_error("ODBadTTInfo::writeDB: Failed to write"));
0097 } else {
0098 int old_version = this->getVersion();
0099 m_readStmt = m_conn->createStatement();
0100 this->fetchData(this);
0101 m_conn->terminateStatement(m_readStmt);
0102 if (this->getVersion() != old_version)
0103 std::cout << "ODBadTTInfo>>WARNING version is " << getVersion() << endl;
0104 }
0105 }
0106
0107 void ODBadTTInfo::fetchData(ODBadTTInfo* result) noexcept(false) {
0108 this->checkConnection();
0109 result->clear();
0110 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0111 throw(std::runtime_error("ODBadTTInfo::fetchData(): no Id defined for this ODBadTTInfo "));
0112 }
0113
0114 try {
0115 if (result->getId() != 0) {
0116 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where rec_id = :1 ");
0117 m_readStmt->setInt(1, result->getId());
0118 } else if (!result->getConfigTag().empty()) {
0119 m_readStmt->setSQL("SELECT * FROM " + getTable() + " where tag=:1 AND version=:2 ");
0120 m_readStmt->setString(1, result->getConfigTag());
0121 m_readStmt->setInt(2, result->getVersion());
0122 } else {
0123
0124 throw(std::runtime_error("ODBadTTInfo::fetchData(): no Id defined for this record "));
0125 }
0126
0127 ResultSet* rset = m_readStmt->executeQuery();
0128
0129 rset->next();
0130
0131
0132
0133 result->setId(rset->getInt(1));
0134 result->setConfigTag(rset->getString(2));
0135 result->setVersion(rset->getInt(3));
0136
0137 } catch (SQLException& e) {
0138 throw(std::runtime_error(std::string("ODBadTTInfo::fetchData(): ") + e.getMessage()));
0139 }
0140 }
0141
0142 int ODBadTTInfo::fetchID() noexcept(false) {
0143
0144 if (m_ID != 0) {
0145 return m_ID;
0146 }
0147
0148 this->checkConnection();
0149
0150 try {
0151 Statement* stmt = m_conn->createStatement();
0152 stmt->setSQL("SELECT rec_id FROM " + getTable() + "WHERE tag=:1 and version=:2 ");
0153
0154 stmt->setString(1, getConfigTag());
0155 stmt->setInt(2, getVersion());
0156
0157 ResultSet* rset = stmt->executeQuery();
0158
0159 if (rset->next()) {
0160 m_ID = rset->getInt(1);
0161 } else {
0162 m_ID = 0;
0163 }
0164 m_conn->terminateStatement(stmt);
0165 } catch (SQLException& e) {
0166 throw(std::runtime_error(std::string("ODBadTTInfo::fetchID: ") + e.getMessage()));
0167 }
0168
0169 return m_ID;
0170 }