Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstdlib>
0004 
0005 #include "OnlineDB/Oracle/interface/Oracle.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/ODTTCFConfig.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 ODTTCFConfig::ODTTCFConfig() {
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   clear();
0020   m_size = 0;
0021 }
0022 
0023 void ODTTCFConfig::clear() {}
0024 
0025 ODTTCFConfig::~ODTTCFConfig() {}
0026 
0027 int ODTTCFConfig::fetchNextId() noexcept(false) {
0028   int result = 0;
0029   try {
0030     this->checkConnection();
0031     std::cout << "going to fetch new id for TTCF 1" << endl;
0032     m_readStmt = m_conn->createStatement();
0033     m_readStmt->setSQL("select ecal_ttcf_config_sq.NextVal from dual");
0034     ResultSet* rset = m_readStmt->executeQuery();
0035     while (rset->next()) {
0036       result = rset->getInt(1);
0037     }
0038     std::cout << "id is : " << result << endl;
0039 
0040     m_conn->terminateStatement(m_readStmt);
0041     return result;
0042 
0043   } catch (SQLException& e) {
0044     throw(std::runtime_error(std::string("ODTTCFConfig::fetchNextId():  ") + e.getMessage()));
0045   }
0046 }
0047 
0048 void ODTTCFConfig::prepareWrite() noexcept(false) {
0049   this->checkConnection();
0050   int next_id = fetchNextId();
0051 
0052   try {
0053     m_writeStmt = m_conn->createStatement();
0054     m_writeStmt->setSQL(
0055         "INSERT INTO ECAL_TTCF_CONFIGURATION (ttcf_configuration_id, ttcf_tag, "
0056         " rxbc0_delay, reg_30 , ttcf_configuration_file , ttcf_configuration ) "
0057         "VALUES (:1, :2, :3 , :4, :5, :6)");
0058     m_writeStmt->setInt(1, next_id);
0059     m_writeStmt->setString(2, getConfigTag());
0060 
0061     m_writeStmt->setInt(3, getRxBC0Delay());
0062     m_writeStmt->setInt(4, getReg30());
0063 
0064     m_writeStmt->setString(5, getTTCFConfigurationFile());
0065 
0066     oracle::occi::Clob clob(m_conn);
0067     clob.setEmpty();
0068     m_writeStmt->setClob(6, clob);
0069     m_writeStmt->executeUpdate();
0070     m_ID = next_id;
0071 
0072     m_conn->terminateStatement(m_writeStmt);
0073     std::cout << "inserted into CONFIGURATION with id=" << next_id << std::endl;
0074 
0075     // now we read and update it
0076     m_writeStmt = m_conn->createStatement();
0077     m_writeStmt->setSQL(
0078         "SELECT ttcf_configuration FROM ECAL_TTCF_CONFIGURATION WHERE"
0079         " ttcf_configuration_id=:1 FOR UPDATE");
0080 
0081     std::cout << "updating the clob 0" << std::endl;
0082 
0083   } catch (SQLException& e) {
0084     throw(std::runtime_error(std::string("ODTTCFConfig::prepareWrite():  ") + e.getMessage()));
0085   }
0086 
0087   std::cout << "updating the clob 1 " << std::endl;
0088 }
0089 
0090 void ODTTCFConfig::writeDB() noexcept(false) {
0091   std::cout << "updating the clob 2" << std::endl;
0092 
0093   try {
0094     m_writeStmt->setInt(1, m_ID);
0095     ResultSet* rset = m_writeStmt->executeQuery();
0096 
0097     rset->next();
0098 
0099     oracle::occi::Clob clob = rset->getClob(1);
0100     cout << "Opening the clob in read write mode" << endl;
0101     populateClob(clob, getTTCFConfigurationFile(), m_size);
0102     int clobLength = clob.length();
0103     cout << "Length of the clob is: " << clobLength << endl;
0104 
0105     m_writeStmt->executeUpdate();
0106     m_writeStmt->closeResultSet(rset);
0107 
0108   } catch (SQLException& e) {
0109     throw(std::runtime_error(std::string("ODTTCFConfig::writeDB():  ") + e.getMessage()));
0110   }
0111   // Now get the ID
0112   if (!this->fetchID()) {
0113     throw(std::runtime_error("ODTTCFConfig::writeDB:  Failed to write"));
0114   }
0115 }
0116 
0117 void ODTTCFConfig::fetchData(ODTTCFConfig* result) noexcept(false) {
0118   this->checkConnection();
0119   result->clear();
0120 
0121   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0122     throw(std::runtime_error("ODTTCFConfig::fetchData(): no Id defined for this ODTTCFConfig "));
0123   }
0124 
0125   try {
0126     m_readStmt->setSQL(
0127         "SELECT *   "
0128         "FROM ECAL_TTCF_CONFIGURATION  "
0129         " where (ttcf_configuration_id = :1 or ttcf_tag= :2) ");
0130     m_readStmt->setInt(1, result->getId());
0131     m_readStmt->setString(2, result->getConfigTag());
0132     ResultSet* rset = m_readStmt->executeQuery();
0133 
0134     rset->next();
0135 
0136     result->setId(rset->getInt(1));
0137     result->setConfigTag(rset->getString(2));
0138     result->setTTCFConfigurationFile(rset->getString(3));
0139     Clob clob = rset->getClob(4);
0140     cout << "Opening the clob in Read only mode" << endl;
0141     clob.open(OCCI_LOB_READONLY);
0142     int clobLength = clob.length();
0143     cout << "Length of the clob is: " << clobLength << endl;
0144     m_size = clobLength;
0145     unsigned char* buffer = readClob(clob, m_size);
0146     clob.close();
0147     result->setTTCFClob((unsigned char*)buffer);
0148 
0149   } catch (SQLException& e) {
0150     throw(std::runtime_error(std::string("ODTTCFConfig::fetchData():  ") + e.getMessage()));
0151   }
0152 }
0153 
0154 int ODTTCFConfig::fetchID() noexcept(false) {
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(
0164         "SELECT ttcf_configuration_id FROM ecal_ttcf_configuration "
0165         "WHERE  ttcf_tag=:ttcf_tag ");
0166 
0167     stmt->setString(1, getConfigTag());
0168 
0169     ResultSet* rset = stmt->executeQuery();
0170 
0171     if (rset->next()) {
0172       m_ID = rset->getInt(1);
0173     } else {
0174       m_ID = 0;
0175     }
0176     m_conn->terminateStatement(stmt);
0177   } catch (SQLException& e) {
0178     throw(std::runtime_error(std::string("ODTTCFConfig::fetchID:  ") + e.getMessage()));
0179   }
0180 
0181   return m_ID;
0182 }
0183 
0184 void ODTTCFConfig::setParameters(const std::map<string, string>& my_keys_map) {
0185   // parses the result of the XML parser that is a map of
0186   // string string with variable name variable value
0187 
0188   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0189     if (ci->first == "TTCF_CONFIGURATION_ID")
0190       setConfigTag(ci->second);
0191     if (ci->first == "Configuration") {
0192       std::string fname = ci->second;
0193       string str3;
0194       size_t pos, pose;
0195 
0196       pos = fname.find('=');  // position of "live" in str
0197       pose = fname.size();    // position of "]" in str
0198       str3 = fname.substr(pos + 1, pose - pos - 2);
0199 
0200       cout << "fname=" << fname << " and reduced is: " << str3 << endl;
0201       setTTCFConfigurationFile(str3);
0202 
0203       // here we must open the file and read the LTC Clob
0204       std::cout << "Going to read file: " << str3 << endl;
0205 
0206       ifstream inpFile;
0207       inpFile.open(str3.c_str());
0208 
0209       // tell me size of file
0210       int bufsize = 0;
0211       inpFile.seekg(0, ios::end);
0212       bufsize = inpFile.tellg();
0213       std::cout << " bufsize =" << bufsize << std::endl;
0214       // set file pointer to start again
0215       inpFile.seekg(0, ios::beg);
0216 
0217       inpFile.close();
0218       m_size = bufsize;
0219 
0220     } else if (ci->first == "RXBC0_DELAY") {
0221       setRxBC0Delay(atoi(ci->second.c_str()));
0222     } else if (ci->first == "REG_30") {
0223       setReg30(atoi(ci->second.c_str()));
0224     }
0225   }
0226 }