Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:11

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstring>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/ODTowersToByPassInfo.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 ODTowersToByPassInfo::ODTowersToByPassInfo() {
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 ODTowersToByPassInfo::clear() {}
0023 
0024 ODTowersToByPassInfo::~ODTowersToByPassInfo() {}
0025 
0026 int ODTowersToByPassInfo::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("ODTowersToByPassInfo::fetchNextId():  ") + e.getMessage()));
0043   }
0044 }
0045 
0046 void ODTowersToByPassInfo::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("ODTowersToByPassInfo::prepareWrite():  ") + e.getMessage()));
0065   }
0066 }
0067 
0068 void ODTowersToByPassInfo::setParameters(const std::map<string, string>& my_keys_map) {
0069   // parses the result of the XML parser that is a map of
0070   // string string with variable name variable value
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 ODTowersToByPassInfo::writeDB() noexcept(false) {
0081   this->checkConnection();
0082   this->checkPrepare();
0083 
0084   try {
0085     // number 1 is the id
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("ODTowersToByPassInfo::writeDB():  ") + e.getMessage()));
0093   }
0094   // Now get the ID
0095   if (!this->fetchID()) {
0096     throw(std::runtime_error("ODTowersToByPassInfo::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 << "ODTowersToByPassInfo>>WARNING version is " << getVersion() << endl;
0104   }
0105 }
0106 
0107 void ODTowersToByPassInfo::fetchData(ODTowersToByPassInfo* result) noexcept(false) {
0108   this->checkConnection();
0109   result->clear();
0110   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0111     throw(std::runtime_error("ODTowersToByPassInfo::fetchData(): no Id defined for this ODTowersToByPassInfo "));
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       if (result->getVersion() != 0) {
0120         m_readStmt->setSQL("SELECT * FROM " + getTable() +
0121                            " WHERE tag = :tag "
0122                            " and version = :version ");
0123         m_readStmt->setString(1, result->getConfigTag());
0124         m_readStmt->setInt(2, result->getVersion());
0125       } else {
0126         // always select the last inserted one with a given tag
0127         m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE tag = :1 and version= (select max(version) from " +
0128                            getTable() + " where tag=:2) ");
0129         m_readStmt->setString(1, result->getConfigTag());
0130         m_readStmt->setString(2, result->getConfigTag());
0131       }
0132 
0133     } else {
0134       // we should never pass here
0135       throw(std::runtime_error("ODTowersToByPassInfo::fetchData(): no Id defined for this record "));
0136     }
0137 
0138     ResultSet* rset = m_readStmt->executeQuery();
0139 
0140     rset->next();
0141 
0142     // 1 is the id and 2 is the config tag and 3 is the version
0143 
0144     result->setId(rset->getInt(1));
0145     result->setConfigTag(rset->getString(2));
0146     result->setVersion(rset->getInt(3));
0147 
0148   } catch (SQLException& e) {
0149     throw(std::runtime_error(std::string("ODTowersToByPassInfo::fetchData():  ") + e.getMessage()));
0150   }
0151 }
0152 
0153 int ODTowersToByPassInfo::fetchID() noexcept(false) {
0154   // Return from memory if available
0155   if (m_ID != 0) {
0156     return m_ID;
0157   }
0158 
0159   this->checkConnection();
0160 
0161   try {
0162     Statement* stmt = m_conn->createStatement();
0163     stmt->setSQL("SELECT rec_id FROM " + getTable() + " WHERE  tag=:1 and version=:2 ");
0164 
0165     stmt->setString(1, getConfigTag());
0166     stmt->setInt(2, getVersion());
0167 
0168     ResultSet* rset = stmt->executeQuery();
0169 
0170     if (rset->next()) {
0171       m_ID = rset->getInt(1);
0172     } else {
0173       m_ID = 0;
0174     }
0175     m_conn->terminateStatement(stmt);
0176   } catch (SQLException& e) {
0177     throw(std::runtime_error(std::string("ODTowersToByPassInfo::fetchID:  ") + e.getMessage()));
0178   }
0179 
0180   return m_ID;
0181 }