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 "OnlineDB/Oracle/interface/Oracle.h"
0005 
0006 #include "OnlineDB/EcalCondDB/interface/ODLTSConfig.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 ODLTSConfig::ODLTSConfig() {
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 void ODLTSConfig::clear() {
0022   m_trg_type = "";
0023   m_num = 0;
0024   m_rate = 0;
0025   m_delay = 0;
0026 }
0027 
0028 ODLTSConfig::~ODLTSConfig() {}
0029 
0030 void ODLTSConfig::setParameters(const std::map<string, string>& my_keys_map) {
0031   // parses the result of the XML parser that is a map of
0032   // string string with variable name variable value
0033 
0034   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0035     if (ci->first == "LTS_CONFIGURATION_ID")
0036       setConfigTag(ci->second);
0037     if (ci->first == "NUM_OF_EVENTS")
0038       setNumberOfEvents(atoi(ci->second.c_str()));
0039     if (ci->first == "RATE")
0040       setRate(atoi(ci->second.c_str()));
0041     if (ci->first == "TRIGGER_TYPE")
0042       setTriggerType(ci->second);
0043     if (ci->first == "TRIG_LOC_L1_DELAY")
0044       setTrigLocL1Delay(atoi(ci->second.c_str()));
0045   }
0046 }
0047 
0048 int ODLTSConfig::fetchNextId() noexcept(false) {
0049   int result = 0;
0050   try {
0051     this->checkConnection();
0052 
0053     m_readStmt = m_conn->createStatement();
0054     m_readStmt->setSQL("select ecal_lts_config_sq.NextVal from dual");
0055     ResultSet* rset = m_readStmt->executeQuery();
0056     while (rset->next()) {
0057       result = rset->getInt(1);
0058     }
0059     m_conn->terminateStatement(m_readStmt);
0060     return result;
0061 
0062   } catch (SQLException& e) {
0063     throw(std::runtime_error(std::string("ODLTSConfig::fetchNextId():  ") + e.getMessage()));
0064   }
0065 }
0066 
0067 void ODLTSConfig::prepareWrite() noexcept(false) {
0068   this->checkConnection();
0069   int next_id = fetchNextId();
0070 
0071   try {
0072     m_writeStmt = m_conn->createStatement();
0073     m_writeStmt->setSQL(
0074         "INSERT INTO ECAL_LTS_CONFIGURATION ( lts_configuration_id, lts_tag, "
0075         "trigger_type, num_of_events, rate, trig_loc_l1_delay ) "
0076         "VALUES (  "
0077         ":1, :2, :3, :4 , :5, :6 )");
0078     m_writeStmt->setInt(1, next_id);
0079     m_ID = next_id;
0080 
0081   } catch (SQLException& e) {
0082     throw(std::runtime_error(std::string("ODLTSConfig::prepareWrite():  ") + e.getMessage()));
0083   }
0084 }
0085 
0086 void ODLTSConfig::writeDB() noexcept(false) {
0087   this->checkConnection();
0088   this->checkPrepare();
0089 
0090   try {
0091     m_writeStmt->setString(2, this->getConfigTag());
0092     m_writeStmt->setString(3, this->getTriggerType());
0093     m_writeStmt->setInt(4, this->getNumberOfEvents());
0094     m_writeStmt->setInt(5, this->getRate());
0095     m_writeStmt->setInt(6, this->getTrigLocL1Delay());
0096 
0097     m_writeStmt->executeUpdate();
0098 
0099   } catch (SQLException& e) {
0100     throw(std::runtime_error(std::string("ODLTSConfig::writeDB():  ") + e.getMessage()));
0101   }
0102   // Now get the ID
0103   if (!this->fetchID()) {
0104     throw(std::runtime_error("ODLTSConfig::writeDB:  Failed to write"));
0105   }
0106 }
0107 
0108 void ODLTSConfig::fetchData(ODLTSConfig* result) noexcept(false) {
0109   this->checkConnection();
0110   result->clear();
0111   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0112     throw(std::runtime_error("ODLTSConfig::fetchData(): no Id defined for this ODLTSConfig "));
0113   }
0114 
0115   try {
0116     m_readStmt->setSQL(
0117         "SELECT * "
0118         "FROM ECAL_LTS_CONFIGURATION  "
0119         " where ( lts_configuration_id = :1 or lts_tag=:2 ) ");
0120     m_readStmt->setInt(1, result->getId());
0121     m_readStmt->setString(2, result->getConfigTag());
0122     ResultSet* rset = m_readStmt->executeQuery();
0123 
0124     rset->next();
0125     // 1 is the id and 2 is the config tag
0126     result->setId(rset->getInt(1));
0127     result->setConfigTag(rset->getString(2));
0128 
0129     result->setTriggerType(rset->getString(3));
0130     result->setNumberOfEvents(rset->getInt(4));
0131     result->setRate(rset->getInt(5));
0132     result->setTrigLocL1Delay(rset->getInt(6));
0133 
0134   } catch (SQLException& e) {
0135     throw(std::runtime_error(std::string("ODLTSConfig::fetchData():  ") + e.getMessage()));
0136   }
0137 }
0138 
0139 int ODLTSConfig::fetchID() noexcept(false) {
0140   // Return from memory if available
0141   if (m_ID != 0) {
0142     return m_ID;
0143   }
0144 
0145   this->checkConnection();
0146 
0147   try {
0148     Statement* stmt = m_conn->createStatement();
0149     stmt->setSQL(
0150         "SELECT lts_configuration_id FROM ecal_lts_configuration "
0151         "WHERE  lts_tag=:lts_tag  ");
0152 
0153     stmt->setString(1, getConfigTag());
0154 
0155     ResultSet* rset = stmt->executeQuery();
0156 
0157     if (rset->next()) {
0158       m_ID = rset->getInt(1);
0159     } else {
0160       m_ID = 0;
0161     }
0162     m_conn->terminateStatement(stmt);
0163   } catch (SQLException& e) {
0164     throw(std::runtime_error(std::string("ODLTSConfig::fetchID:  ") + e.getMessage()));
0165   }
0166 
0167   return m_ID;
0168 }