Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include <fstream>
0004 #include <iostream>
0005 #include <cstdio>
0006 #include <cstdlib>
0007 
0008 #include "OnlineDB/Oracle/interface/Oracle.h"
0009 
0010 #include "OnlineDB/EcalCondDB/interface/ODDCCConfig.h"
0011 
0012 using namespace std;
0013 using namespace oracle::occi;
0014 
0015 ODDCCConfig::ODDCCConfig() {
0016   m_env = nullptr;
0017   m_conn = nullptr;
0018   m_writeStmt = nullptr;
0019   m_readStmt = nullptr;
0020   m_size = 0;
0021   m_config_tag = "";
0022   m_ID = 0;
0023   m_wei = "";
0024   clear();
0025 }
0026 
0027 ODDCCConfig::~ODDCCConfig() {}
0028 
0029 int ODDCCConfig::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_dcc_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("ODDCCConfig::fetchNextId():  ") + e.getMessage()));
0045   }
0046 }
0047 
0048 void ODDCCConfig::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_DCC_CONFIGURATION (dcc_configuration_id, dcc_tag, "
0057         " DCC_CONFIGURATION_URL, TESTPATTERN_FILE_URL, "
0058         " N_TESTPATTERNS_TO_LOAD , SM_HALF, weightsmode, "
0059         " dcc_configuration) "
0060         "VALUES (:1, :2, :3, :4, :5, :6 , :7 ,:8 )");
0061     m_writeStmt->setInt(1, next_id);
0062     m_writeStmt->setString(2, getConfigTag());
0063     m_writeStmt->setString(3, getDCCConfigurationUrl());
0064     m_writeStmt->setString(4, getTestPatternFileUrl());
0065     m_writeStmt->setInt(5, getNTestPatternsToLoad());
0066     m_writeStmt->setInt(6, getSMHalf());
0067     m_writeStmt->setString(7, getDCCWeightsMode());
0068 
0069     // and now the clob
0070     oracle::occi::Clob clob(m_conn);
0071     clob.setEmpty();
0072     m_writeStmt->setClob(8, clob);
0073     m_writeStmt->executeUpdate();
0074     m_ID = next_id;
0075 
0076     m_conn->terminateStatement(m_writeStmt);
0077     std::cout << "DCC Clob inserted into CONFIGURATION with id=" << next_id << std::endl;
0078 
0079     // now we read and update it
0080     m_writeStmt = m_conn->createStatement();
0081     m_writeStmt->setSQL(
0082         "SELECT dcc_configuration FROM ECAL_DCC_CONFIGURATION WHERE"
0083         " dcc_configuration_id=:1 FOR UPDATE");
0084 
0085     std::cout << "updating the clob 0" << std::endl;
0086 
0087   } catch (SQLException &e) {
0088     throw(std::runtime_error(std::string("ODDCCConfig::prepareWrite():  ") + e.getMessage()));
0089   }
0090 
0091   std::cout << "updating the clob 1 " << std::endl;
0092 }
0093 
0094 void ODDCCConfig::setParameters(const std::map<string, string> &my_keys_map) {
0095   // parses the result of the XML parser that is a map of
0096   // string string with variable name variable value
0097 
0098   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0099     if (ci->first == "DCC_CONFIGURATION_ID")
0100       setConfigTag(ci->second);
0101     if (ci->first == "TESTPATTERN_FILE_URL")
0102       setTestPatternFileUrl(ci->second);
0103     if (ci->first == "N_TESTPATTERNS_TO_LOAD")
0104       setNTestPatternsToLoad(atoi(ci->second.c_str()));
0105     if (ci->first == "SM_HALF")
0106       setSMHalf(atoi(ci->second.c_str()));
0107     if (ci->first == "WEIGHTSMODE")
0108       setDCCWeightsMode(ci->second);
0109     if (ci->first == "DCC_CONFIGURATION_URL") {
0110       std::string fname = ci->second;
0111       setDCCConfigurationUrl(fname);
0112 
0113       // here we must open the file and read the DCC Clob
0114       std::cout << "Going to read DCC file: " << fname << endl;
0115 
0116       ifstream inpFile;
0117       inpFile.open(fname.c_str());
0118 
0119       // tell me size of file
0120       int bufsize = 0;
0121       inpFile.seekg(0, ios::end);
0122       bufsize = inpFile.tellg();
0123       std::cout << " bufsize =" << bufsize << std::endl;
0124       // set file pointer to start again
0125       inpFile.seekg(0, ios::beg);
0126 
0127       m_size = bufsize;
0128 
0129       inpFile.close();
0130     }
0131   }
0132 }
0133 
0134 void ODDCCConfig::writeDB() noexcept(false) {
0135   std::cout << "updating the clob " << std::endl;
0136 
0137   try {
0138     m_writeStmt->setInt(1, m_ID);
0139     ResultSet *rset = m_writeStmt->executeQuery();
0140 
0141     rset->next();
0142     oracle::occi::Clob clob = rset->getClob(1);
0143 
0144     cout << "Opening the clob in read write mode" << endl;
0145 
0146     std::cout << "Populating the clob" << endl;
0147 
0148     populateClob(clob, getDCCConfigurationUrl(), m_size);
0149     int clobLength = clob.length();
0150     cout << "Length of the clob is: " << clobLength << endl;
0151 
0152     m_writeStmt->executeUpdate();
0153 
0154     m_writeStmt->closeResultSet(rset);
0155 
0156   } catch (SQLException &e) {
0157     throw(std::runtime_error(std::string("ODDCCConfig::writeDB():  ") + e.getMessage()));
0158   }
0159   // Now get the ID
0160   if (!this->fetchID()) {
0161     throw(std::runtime_error("ODDCCConfig::writeDB:  Failed to write"));
0162   }
0163 }
0164 
0165 void ODDCCConfig::clear() {
0166   m_dcc_url = "";
0167   m_test_url = "";
0168   m_ntest = 0;
0169   m_sm_half = 0;
0170   m_wei = "";
0171 }
0172 
0173 void ODDCCConfig::fetchData(ODDCCConfig *result) noexcept(false) {
0174   this->checkConnection();
0175   //  result->clear();
0176   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0177     //    throw(std::runtime_error("ODDCCConfig::fetchData(): no Id defined for this ODDCCConfig "));
0178     result->fetchID();
0179   }
0180 
0181   try {
0182     m_readStmt->setSQL(
0183         "SELECT * "
0184         "FROM ECAL_DCC_CONFIGURATION  "
0185         " where  dcc_configuration_id = :1 or dcc_tag=:2 ");
0186     m_readStmt->setInt(1, result->getId());
0187     m_readStmt->setString(2, result->getConfigTag());
0188     ResultSet *rset = m_readStmt->executeQuery();
0189 
0190     rset->next();
0191 
0192     // 1 is the id and 2 is the config tag
0193 
0194     result->setId(rset->getInt(1));
0195     result->setConfigTag(rset->getString(2));
0196     result->setDCCConfigurationUrl(rset->getString(3));
0197     result->setTestPatternFileUrl(rset->getString(4));
0198     result->setNTestPatternsToLoad(rset->getInt(5));
0199     result->setSMHalf(rset->getInt(6));
0200 
0201     Clob clob = rset->getClob(7);
0202     m_size = clob.length();
0203     Stream *instream = clob.getStream(1, 0);
0204     unsigned char *buffer = new unsigned char[m_size];
0205     memset(buffer, 0, m_size);
0206     instream->readBuffer((char *)buffer, m_size);
0207     /*
0208     cout << "Opening the clob in Read only mode" << endl;
0209     clob.open (OCCI_LOB_READONLY);
0210     int clobLength=clob.length ();
0211     cout << "Length of the clob is: " << clobLength << endl;
0212     m_size=clobLength;
0213     unsigned char* buffer = readClob (clob, m_size);
0214     clob.close ();
0215     cout<< "the clob buffer is:"<<endl;  
0216     for (int i = 0; i < clobLength; ++i)
0217       cout << (char) buffer[i];
0218     cout << endl;
0219 
0220 
0221     */
0222     result->setDCCClob(buffer);
0223     result->setDCCWeightsMode(rset->getString(8));
0224 
0225   } catch (SQLException &e) {
0226     throw(std::runtime_error(std::string("ODDCCConfig::fetchData():  ") + e.getMessage()));
0227   }
0228 }
0229 
0230 int ODDCCConfig::fetchID() noexcept(false) {
0231   if (m_ID != 0) {
0232     return m_ID;
0233   }
0234 
0235   this->checkConnection();
0236 
0237   try {
0238     Statement *stmt = m_conn->createStatement();
0239     stmt->setSQL(
0240         "SELECT DCC_configuration_id FROM ecal_dcc_configuration "
0241         "WHERE  dcc_tag=:dcc_tag ");
0242 
0243     stmt->setString(1, getConfigTag());
0244 
0245     ResultSet *rset = stmt->executeQuery();
0246 
0247     if (rset->next()) {
0248       m_ID = rset->getInt(1);
0249     } else {
0250       m_ID = 0;
0251     }
0252     m_conn->terminateStatement(stmt);
0253   } catch (SQLException &e) {
0254     throw(std::runtime_error(std::string("ODDCCConfig::fetchID:  ") + e.getMessage()));
0255   }
0256 
0257   return m_ID;
0258 }