Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:07

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