Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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