Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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