File indexing completed on 2024-04-06 12:23:11
0001 #include <stdexcept>
0002 #include <cstdlib>
0003 #include <string>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005
0006 #include "OnlineDB/EcalCondDB/interface/ODScanConfig.h"
0007
0008 using namespace std;
0009 using namespace oracle::occi;
0010
0011 ODScanConfig::ODScanConfig() {
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 clear();
0019 }
0020
0021 ODScanConfig::~ODScanConfig() {}
0022
0023 void ODScanConfig::clear() {
0024 m_type_id = 0;
0025 m_type = "";
0026 m_from_val = 0;
0027 m_to_val = 0;
0028 m_step = 0;
0029 }
0030
0031 int ODScanConfig::fetchNextId() noexcept(false) {
0032 int result = 0;
0033 try {
0034 this->checkConnection();
0035
0036 m_readStmt = m_conn->createStatement();
0037 m_readStmt->setSQL("select ecal_scan_config_sq.NextVal from dual");
0038 ResultSet* rset = m_readStmt->executeQuery();
0039 while (rset->next()) {
0040 result = rset->getInt(1);
0041 }
0042 m_conn->terminateStatement(m_readStmt);
0043 return result;
0044
0045 } catch (SQLException& e) {
0046 throw(std::runtime_error(std::string("ODScanConfig::fetchNextId(): ") + e.getMessage()));
0047 }
0048 }
0049
0050 void ODScanConfig::setParameters(const std::map<string, string>& my_keys_map) {
0051
0052
0053
0054 for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0055 if (ci->first == "SCAN_ID")
0056 setConfigTag(ci->second);
0057 if (ci->first == "TYPE_ID")
0058 setTypeId(atoi(ci->second.c_str()));
0059 if (ci->first == "TYPE" || ci->first == "SCAN_TYPE")
0060 setScanType(ci->second);
0061 if (ci->first == "FROM" || ci->first == "FROM_VAL")
0062 setFromVal(atoi(ci->second.c_str()));
0063 if (ci->first == "TO" || ci->first == "TO_VAL")
0064 setToVal(atoi(ci->second.c_str()));
0065 if (ci->first == "STEP")
0066 setStep(atoi(ci->second.c_str()));
0067 }
0068 }
0069
0070 void ODScanConfig::prepareWrite() noexcept(false) {
0071 this->checkConnection();
0072 int next_id = fetchNextId();
0073
0074 try {
0075 m_writeStmt = m_conn->createStatement();
0076 m_writeStmt->setSQL(
0077 "INSERT INTO ECAL_scan_dat ( scan_id, scan_tag ,"
0078 " type_id, scan_type , FROM_VAL , TO_VAL, STEP )"
0079 " VALUES ( :1, :2, :3, :4, :5, :6, :7)");
0080 m_writeStmt->setInt(1, next_id);
0081 m_ID = next_id;
0082
0083 } catch (SQLException& e) {
0084 throw(std::runtime_error(std::string("ODScanConfig::prepareWrite(): ") + e.getMessage()));
0085 }
0086 }
0087
0088 void ODScanConfig::writeDB() noexcept(false) {
0089 this->checkConnection();
0090 this->checkPrepare();
0091
0092 try {
0093
0094 m_writeStmt->setString(2, this->getConfigTag());
0095
0096 m_writeStmt->setInt(3, this->getTypeId());
0097 m_writeStmt->setString(4, this->getScanType());
0098 m_writeStmt->setInt(5, this->getFromVal());
0099 m_writeStmt->setInt(6, this->getToVal());
0100 m_writeStmt->setInt(7, this->getStep());
0101
0102 m_writeStmt->executeUpdate();
0103
0104 } catch (SQLException& e) {
0105 throw(std::runtime_error(std::string("ODScanConfig::writeDB(): ") + e.getMessage()));
0106 }
0107
0108 if (!this->fetchID()) {
0109 throw(std::runtime_error("ODScanConfig::writeDB: Failed to write"));
0110 }
0111 }
0112
0113 void ODScanConfig::fetchData(ODScanConfig* result) noexcept(false) {
0114 this->checkConnection();
0115 result->clear();
0116 if (result->getId() == 0 && (result->getConfigTag().empty())) {
0117 throw(std::runtime_error("ODScanConfig::fetchData(): no Id defined for this ODScanConfig "));
0118 }
0119
0120 try {
0121 m_readStmt->setSQL(
0122 "SELECT * "
0123 "FROM ECAL_SCAN_DAT "
0124 " where (scan_id = :1 or scan_tag=:2 )");
0125 m_readStmt->setInt(1, result->getId());
0126 m_readStmt->setString(2, result->getConfigTag());
0127
0128 ResultSet* rset = m_readStmt->executeQuery();
0129
0130 rset->next();
0131
0132
0133 result->setId(rset->getInt(1));
0134 result->setConfigTag(rset->getString(2));
0135 result->setTypeId(rset->getInt(3));
0136 result->setScanType(rset->getString(4));
0137 result->setFromVal(rset->getInt(5));
0138 result->setToVal(rset->getInt(6));
0139 result->setStep(rset->getInt(7));
0140
0141 } catch (SQLException& e) {
0142 throw(std::runtime_error(std::string("ODScanConfig::fetchData(): ") + e.getMessage()));
0143 }
0144 }
0145
0146 int ODScanConfig::fetchID() noexcept(false) {
0147
0148 if (m_ID != 0) {
0149 return m_ID;
0150 }
0151
0152 this->checkConnection();
0153
0154 try {
0155 Statement* stmt = m_conn->createStatement();
0156 stmt->setSQL(
0157 "SELECT scan_id FROM ecal_scan_dat "
0158 "WHERE scan_tag=:scan_tag ");
0159
0160 stmt->setString(1, getConfigTag());
0161
0162 ResultSet* rset = stmt->executeQuery();
0163
0164 if (rset->next()) {
0165 m_ID = rset->getInt(1);
0166 } else {
0167 m_ID = 0;
0168 }
0169 m_conn->terminateStatement(stmt);
0170 } catch (SQLException& e) {
0171 throw(std::runtime_error(std::string("ODScanConfig::fetchID: ") + e.getMessage()));
0172 }
0173
0174 return m_ID;
0175 }