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/ODFEPedestalOffsetInfo.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 ODFEPedestalOffsetInfo::ODFEPedestalOffsetInfo() {
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 ODFEPedestalOffsetInfo::clear() {}
0024 
0025 ODFEPedestalOffsetInfo::~ODFEPedestalOffsetInfo() {}
0026 
0027 int ODFEPedestalOffsetInfo::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("ODFEPedestalOffsetInfo::fetchNextId():  ") + e.getMessage()));
0044   }
0045 }
0046 
0047 void ODFEPedestalOffsetInfo::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("ODFEPedestalOffsetInfo::prepareWrite():  ") + e.getMessage()));
0066   }
0067 }
0068 
0069 void ODFEPedestalOffsetInfo::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 ODFEPedestalOffsetInfo::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 
0090     m_writeStmt->executeUpdate();
0091 
0092   } catch (SQLException& e) {
0093     throw(std::runtime_error(std::string("ODFEPedestalOffsetInfo::writeDB():  ") + e.getMessage()));
0094   }
0095 
0096   // Now get the ID
0097   if (!this->fetchID()) {
0098     throw(std::runtime_error("ODFEPedestalOffsetInfo::writeDB:  Failed to write"));
0099   } else {
0100     int old_version = this->getVersion();
0101     m_readStmt = m_conn->createStatement();
0102     this->fetchData(this);
0103     m_conn->terminateStatement(m_readStmt);
0104     if (this->getVersion() != old_version)
0105       std::cout << "ODFEPedestalOffsetInfo>>WARNING version is " << getVersion() << endl;
0106   }
0107 }
0108 
0109 void ODFEPedestalOffsetInfo::fetchData(ODFEPedestalOffsetInfo* result) noexcept(false) {
0110   this->checkConnection();
0111   result->clear();
0112   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0113     throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData(): no Id defined for this ODFEPedestalOffsetInfo "));
0114   }
0115 
0116   try {
0117     if (result->getId() != 0) {
0118       m_readStmt->setSQL("SELECT * FROM " + getTable() + " where  rec_id = :1 ");
0119       m_readStmt->setInt(1, result->getId());
0120     } else if (!result->getConfigTag().empty()) {
0121       m_readStmt->setSQL("SELECT * FROM " + getTable() + " where  tag=:1 AND version=:2 ");
0122       m_readStmt->setString(1, result->getConfigTag());
0123       m_readStmt->setInt(2, result->getVersion());
0124     } else {
0125       // we should never pass here
0126       throw(std::runtime_error("ODFEPedestalOffsetInfo::fetchData(): no Id defined for this record "));
0127     }
0128 
0129     ResultSet* rset = m_readStmt->executeQuery();
0130 
0131     rset->next();
0132 
0133     // 1 is the id and 2 is the config tag and 3 is the version
0134 
0135     result->setId(rset->getInt(1));
0136     result->setConfigTag(rset->getString(2));
0137     result->setVersion(rset->getInt(3));
0138 
0139   } catch (SQLException& e) {
0140     throw(std::runtime_error(std::string("ODFEPedestalOffsetInfo::fetchData():  ") + e.getMessage()));
0141   }
0142 }
0143 
0144 void ODFEPedestalOffsetInfo::fetchLastData(ODFEPedestalOffsetInfo* result) noexcept(false) {
0145   this->checkConnection();
0146   result->clear();
0147   try {
0148     m_readStmt->setSQL("SELECT * FROM " + getTable() + " where  rec_id = ( select max(rec_id) from " + getTable() +
0149                        " ) ");
0150     ResultSet* rset = m_readStmt->executeQuery();
0151 
0152     rset->next();
0153 
0154     result->setId(rset->getInt(1));
0155     result->setConfigTag(rset->getString(2));
0156     result->setVersion(rset->getInt(3));
0157 
0158   } catch (SQLException& e) {
0159     throw(std::runtime_error(std::string("ODFEPedestalOffsetInfo::fetchData():  ") + e.getMessage()));
0160   }
0161 }
0162 
0163 int ODFEPedestalOffsetInfo::fetchID() noexcept(false) {
0164   // Return from memory if available
0165   if (m_ID != 0) {
0166     return m_ID;
0167   }
0168 
0169   this->checkConnection();
0170 
0171   try {
0172     Statement* stmt = m_conn->createStatement();
0173     stmt->setSQL("SELECT rec_id FROM " + getTable() + "WHERE  tag=:1 and version=:2 ");
0174 
0175     stmt->setString(1, getConfigTag());
0176     stmt->setInt(2, getVersion());
0177 
0178     ResultSet* rset = stmt->executeQuery();
0179 
0180     if (rset->next()) {
0181       m_ID = rset->getInt(1);
0182     } else {
0183       m_ID = 0;
0184     }
0185     m_conn->terminateStatement(stmt);
0186   } catch (SQLException& e) {
0187     throw(std::runtime_error(std::string("ODFEPedestalOffsetInfo::fetchID:  ") + e.getMessage()));
0188   }
0189 
0190   return m_ID;
0191 }