File indexing completed on 2023-03-17 11:15:14
0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigOddWeightInfo.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 FEConfigOddWeightInfo::FEConfigOddWeightInfo() {
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 FEConfigOddWeightInfo::clear() { m_ngr = 0; }
0026
0027 FEConfigOddWeightInfo::~FEConfigOddWeightInfo() {}
0028
0029 int FEConfigOddWeightInfo::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_WEIGHT2GROUP_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") << "FEConfigOddWeightInfo::fetchNextId(): " << e.getMessage();
0046 }
0047 }
0048
0049 void FEConfigOddWeightInfo::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 " ( wei2_conf_id, tag, number_of_groups) "
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") << "FEConfigOddWeightInfo::prepareWrite(): " << e.getMessage();
0068 }
0069 }
0070
0071 void FEConfigOddWeightInfo::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 == "TAG")
0077 setConfigTag(ci->second);
0078 if (ci->first == "NUMBER_OF_GROUPS")
0079 setNumberOfGroups(atoi(ci->second.c_str()));
0080 }
0081 }
0082
0083 void FEConfigOddWeightInfo::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->getNumberOfGroups());
0091
0092 m_writeStmt->executeUpdate();
0093
0094 } catch (SQLException& e) {
0095 throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::writeDB(): " << e.getMessage();
0096 }
0097
0098 if (!this->fetchID()) {
0099 throw(std::runtime_error("FEConfigOddWeightInfo::writeDB: Failed to write"));
0100 }
0101 }
0102
0103 void FEConfigOddWeightInfo::fetchData(FEConfigOddWeightInfo* result) noexcept(false) {
0104 this->checkConnection();
0105 result->clear();
0106 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0107 throw(std::runtime_error("FEConfigOddWeightInfo::fetchData(): no Id defined for this FEConfigOddWeightInfo "));
0108 }
0109
0110 try {
0111 DateHandler dh(m_env, m_conn);
0112
0113 m_readStmt->setSQL("SELECT wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
0114 " where ( wei2_conf_id= :1 or (tag=:2 ) )");
0115 m_readStmt->setInt(1, result->getId());
0116 m_readStmt->setString(2, result->getConfigTag());
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->setNumberOfGroups(rset->getInt(3));
0126 Date dbdate = rset->getDate(4);
0127 result->setDBTime(dh.dateToTm(dbdate));
0128
0129 } catch (SQLException& e) {
0130 throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchData(): " << e.getMessage();
0131 }
0132 }
0133
0134 void FEConfigOddWeightInfo::fetchLastData(FEConfigOddWeightInfo* result) noexcept(false) {
0135 this->checkConnection();
0136 result->clear();
0137 try {
0138 DateHandler dh(m_env, m_conn);
0139
0140 m_readStmt->setSQL("SELECT wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
0141 " where wei2_conf_id = ( select max( wei2_conf_id) from " + getTable() + " ) ");
0142 ResultSet* rset = m_readStmt->executeQuery();
0143
0144 rset->next();
0145
0146 result->setId(rset->getInt(1));
0147 result->setConfigTag(rset->getString(2));
0148 result->setNumberOfGroups(rset->getInt(3));
0149 Date dbdate = rset->getDate(4);
0150 result->setDBTime(dh.dateToTm(dbdate));
0151
0152 } catch (SQLException& e) {
0153 throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchData(): " << e.getMessage();
0154 }
0155 }
0156
0157 int FEConfigOddWeightInfo::fetchID() noexcept(false) {
0158
0159 if (m_ID != 0) {
0160 return m_ID;
0161 }
0162
0163 this->checkConnection();
0164
0165 try {
0166 Statement* stmt = m_conn->createStatement();
0167 stmt->setSQL("SELECT wei2_conf_id FROM " + getTable() + " WHERE tag=:1 ");
0168
0169 stmt->setString(1, getConfigTag());
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 cms::Exception("SQLException") << "FEConfigOddWeightInfo::fetchID: " << e.getMessage();
0181 }
0182
0183 return m_ID;
0184 }
0185
0186 void FEConfigOddWeightInfo::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 wei2_conf_id, tag, number_of_groups, db_timestamp FROM " + getTable() +
0195 " WHERE wei2_conf_id = :1");
0196 stmt->setInt(1, id);
0197
0198 ResultSet* rset = stmt->executeQuery();
0199 if (rset->next()) {
0200 this->setId(rset->getInt(1));
0201 this->setConfigTag(rset->getString(2));
0202 this->setNumberOfGroups(rset->getInt(3));
0203 Date dbdate = rset->getDate(4);
0204 this->setDBTime(dh.dateToTm(dbdate));
0205 } else {
0206 throw(std::runtime_error("FEConfigOddWeightInfo::setByID: Given config_id is not in the database"));
0207 }
0208
0209 m_conn->terminateStatement(stmt);
0210 } catch (SQLException& e) {
0211 throw cms::Exception("SQLException") << "FEConfigOddWeightInfo::setByID: " << e.getMessage();
0212 }
0213 }