Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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