File indexing completed on 2023-03-17 11:15:22
0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004
0005 #include "OnlineDB/EcalCondDB/interface/ODDCUConfig.h"
0006
0007 using namespace std;
0008 using namespace oracle::occi;
0009
0010 ODDCUConfig::ODDCUConfig() {
0011 m_env = nullptr;
0012 m_conn = nullptr;
0013 m_writeStmt = nullptr;
0014 m_readStmt = nullptr;
0015 m_config_tag = "";
0016 m_ID = 0;
0017 clear();
0018 }
0019
0020 void ODDCUConfig::clear() {}
0021
0022 ODDCUConfig::~ODDCUConfig() {}
0023
0024 void ODDCUConfig::setParameters(const std::map<string, string>& my_keys_map) {
0025
0026
0027
0028 for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0029 if (ci->first == "DCU_CONFIGURATION_ID")
0030 setConfigTag(ci->second);
0031 }
0032 }
0033
0034 int ODDCUConfig::fetchNextId() noexcept(false) {
0035 int result = 0;
0036 try {
0037 this->checkConnection();
0038
0039 m_readStmt = m_conn->createStatement();
0040 m_readStmt->setSQL("select ecal_dcu_config_sq.NextVal from dual");
0041 ResultSet* rset = m_readStmt->executeQuery();
0042 while (rset->next()) {
0043 result = rset->getInt(1);
0044 }
0045 m_conn->terminateStatement(m_readStmt);
0046 return result;
0047
0048 } catch (SQLException& e) {
0049 throw(std::runtime_error(std::string("ODDCUConfig::fetchNextId(): ") + e.getMessage()));
0050 }
0051 }
0052
0053 void ODDCUConfig::prepareWrite() noexcept(false) {
0054 this->checkConnection();
0055 int next_id = fetchNextId();
0056
0057 try {
0058 m_writeStmt = m_conn->createStatement();
0059 m_writeStmt->setSQL(
0060 "INSERT INTO ECAL_DCU_CONFIGURATION ( dcu_configuration_id, dcu_tag ) "
0061 "VALUES ( "
0062 ":1, :2 )");
0063 m_writeStmt->setInt(1, next_id);
0064 m_ID = next_id;
0065
0066 } catch (SQLException& e) {
0067 throw(std::runtime_error(std::string("ODDCUConfig::prepareWrite(): ") + e.getMessage()));
0068 }
0069 }
0070
0071 void ODDCUConfig::writeDB() noexcept(false) {
0072 this->checkConnection();
0073 this->checkPrepare();
0074
0075 try {
0076 m_writeStmt->setString(2, this->getConfigTag());
0077
0078 m_writeStmt->executeUpdate();
0079
0080 } catch (SQLException& e) {
0081 throw(std::runtime_error(std::string("ODDCUConfig::writeDB(): ") + e.getMessage()));
0082 }
0083
0084 if (!this->fetchID()) {
0085 throw(std::runtime_error("ODDCUConfig::writeDB: Failed to write"));
0086 }
0087 }
0088
0089 void ODDCUConfig::fetchData(ODDCUConfig* result) noexcept(false) {
0090 this->checkConnection();
0091 result->clear();
0092 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0093 throw(std::runtime_error("ODDCUConfig::fetchData(): no Id defined for this ODDCUConfig "));
0094 }
0095
0096 try {
0097 m_readStmt->setSQL(
0098 "SELECT * "
0099 "FROM ECAL_DCU_CONFIGURATION "
0100 " where ( dcu_configuration_id = :1 or dcu_tag=:2 ) ");
0101 m_readStmt->setInt(1, result->getId());
0102 m_readStmt->setString(2, result->getConfigTag());
0103 ResultSet* rset = m_readStmt->executeQuery();
0104
0105 rset->next();
0106
0107 result->setId(rset->getInt(1));
0108 result->setConfigTag(rset->getString(2));
0109
0110 } catch (SQLException& e) {
0111 throw(std::runtime_error(std::string("ODDCUConfig::fetchData(): ") + e.getMessage()));
0112 }
0113 }
0114
0115 int ODDCUConfig::fetchID() noexcept(false) {
0116
0117 if (m_ID != 0) {
0118 return m_ID;
0119 }
0120
0121 this->checkConnection();
0122
0123 try {
0124 Statement* stmt = m_conn->createStatement();
0125 stmt->setSQL(
0126 "SELECT dcu_configuration_id FROM ecal_dcu_configuration "
0127 "WHERE dcu_tag=:dcu_tag ");
0128
0129 stmt->setString(1, getConfigTag());
0130
0131 ResultSet* rset = stmt->executeQuery();
0132
0133 if (rset->next()) {
0134 m_ID = rset->getInt(1);
0135 } else {
0136 m_ID = 0;
0137 }
0138 m_conn->terminateStatement(stmt);
0139 } catch (SQLException& e) {
0140 throw(std::runtime_error(std::string("ODDCUConfig::fetchID: ") + e.getMessage()));
0141 }
0142
0143 return m_ID;
0144 }