Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cstdlib>
0004 
0005 #include "OnlineDB/Oracle/interface/Oracle.h"
0006 
0007 #include "OnlineDB/EcalCondDB/interface/ODTCCEEConfig.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 ODTCCEEConfig::ODTCCEEConfig() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017   m_config_tag = "";
0018 
0019   m_ID = 0;
0020   clear();
0021   m_size = 0;
0022 }
0023 
0024 void ODTCCEEConfig::clear() {
0025   m_tcc_ee_file = "";
0026   m_lut_file = "";
0027   m_slb_file = "";
0028   m_test_url = "";
0029   m_ntest = 0;
0030 }
0031 
0032 ODTCCEEConfig::~ODTCCEEConfig() {}
0033 
0034 int ODTCCEEConfig::fetchNextId() noexcept(false) {
0035   int result = 0;
0036   try {
0037     this->checkConnection();
0038 
0039     m_readStmt = m_conn->createStatement();
0040     m_readStmt->setSQL("select ecal_tcc_ee_config_sq.NextVal from dual");
0041     ResultSet* rset = m_readStmt->executeQuery();
0042     while (rset->next()) {
0043       result = rset->getInt(1);
0044     }
0045     m_conn->terminateStatement(m_readStmt);
0046     return result;
0047 
0048   } catch (SQLException& e) {
0049     throw(std::runtime_error(std::string("ODTCCEEConfig::fetchNextId():  ") + e.getMessage()));
0050   }
0051 }
0052 
0053 void ODTCCEEConfig::setParameters(const std::map<string, string>& my_keys_map) {
0054   // parses the result of the XML parser that is a map of
0055   // string string with variable name variable value
0056 
0057   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0058     if (ci->first == "TCC_EE_CONFIGURATION_ID")
0059       setConfigTag(ci->second);
0060     if (ci->first == "N_TESTPATTERNS_TO_LOAD")
0061       setNTestPatternsToLoad(atoi(ci->second.c_str()));
0062     if (ci->first == "LUT_CONFIGURATION_FILE")
0063       setLUTConfigurationFile(ci->second);
0064     if (ci->first == "CONFIGURATION_FILE")
0065       setTCCConfigurationFile(ci->second);
0066     if (ci->first == "SLB_CONFIGURATION_FILE")
0067       setSLBConfigurationFile(ci->second);
0068     if (ci->first == "TESTPATTERNFILE_URL")
0069       setTestPatternFileUrl(ci->second);
0070 
0071     if (ci->first == "SLB_LATENCY")
0072       setSLBLatency(atoi(ci->second.c_str()));
0073     if (ci->first == "SLBLATENCY")
0074       setSLBLatency(atoi(ci->second.c_str()));
0075 
0076     if (ci->first == "TRIGGER_POS")
0077       setTriggerPos(atoi(ci->second.c_str()));
0078   }
0079 }
0080 
0081 void ODTCCEEConfig::prepareWrite() noexcept(false) {
0082   this->checkConnection();
0083 
0084   int next_id = fetchNextId();
0085 
0086   try {
0087     m_writeStmt = m_conn->createStatement();
0088     m_writeStmt->setSQL(
0089         "INSERT INTO ECAL_TCC_EE_CONFIGURATION (tcc_ee_configuration_id, tcc_ee_tag, "
0090         "Configuration_file, LUT_CONFIGURATION_FILE, SLB_CONFIGURATION_FILE, "
0091         "TESTPATTERNFILE_URL , N_TESTPATTERNS_TO_LOAD, TRIGGER_POS, "
0092         "tcc_configuration, lut_configuration, slb_configuration, slblatency ) "
0093         "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10, :11, :12)");
0094     m_writeStmt->setInt(1, next_id);
0095     m_writeStmt->setString(2, getConfigTag());
0096     m_writeStmt->setString(3, getTCCConfigurationFile());
0097     m_writeStmt->setString(4, getLUTConfigurationFile());
0098     m_writeStmt->setString(5, getSLBConfigurationFile());
0099     m_writeStmt->setString(6, getTestPatternFileUrl());
0100     m_writeStmt->setInt(7, getNTestPatternsToLoad());
0101     m_writeStmt->setInt(8, getTrigPos());
0102     m_writeStmt->setInt(12, getSLBLatency());
0103     // and now the clobs
0104     oracle::occi::Clob clob1(m_conn);
0105     clob1.setEmpty();
0106     m_writeStmt->setClob(9, clob1);
0107 
0108     oracle::occi::Clob clob2(m_conn);
0109     clob2.setEmpty();
0110     m_writeStmt->setClob(10, clob2);
0111 
0112     oracle::occi::Clob clob3(m_conn);
0113     clob3.setEmpty();
0114     m_writeStmt->setClob(11, clob3);
0115 
0116     m_writeStmt->executeUpdate();
0117     m_ID = next_id;
0118 
0119     m_conn->terminateStatement(m_writeStmt);
0120     std::cout << "TCC 3 empty Clobs inserted into CONFIGURATION with id=" << next_id << std::endl;
0121 
0122     // now we read and update it
0123     m_writeStmt = m_conn->createStatement();
0124     m_writeStmt->setSQL(
0125         "SELECT tcc_configuration, lut_configuration, slb_configuration FROM ECAL_TCC_EE_CONFIGURATION WHERE"
0126         " tcc_ee_configuration_id=:1 FOR UPDATE");
0127 
0128     std::cout << "updating the clobs 0" << std::endl;
0129 
0130   } catch (SQLException& e) {
0131     throw(std::runtime_error(std::string("ODTCCEEConfig::prepareWrite():  ") + e.getMessage()));
0132   }
0133 
0134   std::cout << "updating the clob 1 " << std::endl;
0135 }
0136 
0137 void ODTCCEEConfig::writeDB() noexcept(false) {
0138   std::cout << "updating the clob 2" << std::endl;
0139 
0140   try {
0141     m_writeStmt->setInt(1, m_ID);
0142     ResultSet* rset = m_writeStmt->executeQuery();
0143 
0144     while (rset->next()) {
0145       oracle::occi::Clob clob1 = rset->getClob(1);
0146       oracle::occi::Clob clob2 = rset->getClob(2);
0147       oracle::occi::Clob clob3 = rset->getClob(3);
0148       cout << "Opening the clob in read write mode" << endl;
0149       cout << "Populating the clobs" << endl;
0150       populateClob(clob1, getTCCConfigurationFile(), m_size);
0151       populateClob(clob2, getLUTConfigurationFile(), m_size);
0152       populateClob(clob3, getSLBConfigurationFile(), m_size);
0153     }
0154 
0155     m_writeStmt->executeUpdate();
0156     m_writeStmt->closeResultSet(rset);
0157 
0158   } catch (SQLException& e) {
0159     throw(std::runtime_error(std::string("ODTCCEEConfig::writeDB():  ") + e.getMessage()));
0160   }
0161   // Now get the ID
0162   if (!this->fetchID()) {
0163     throw(std::runtime_error("ODTCCEEConfig::writeDB:  Failed to write"));
0164   }
0165 }
0166 
0167 void ODTCCEEConfig::fetchData(ODTCCEEConfig* result) noexcept(false) {
0168   this->checkConnection();
0169   result->clear();
0170   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0171     throw(std::runtime_error("ODTCCEEConfig::fetchData(): no Id defined for this ODTCCEEConfig "));
0172   }
0173 
0174   try {
0175     m_readStmt->setSQL(
0176         "SELECT * "
0177         "FROM ECAL_TCC_EE_CONFIGURATION d "
0178         " where (tcc_ee_configuration_id = :1 or tcc_ee_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     // the first is the id
0185     result->setId(rset->getInt(1));
0186     result->setConfigTag(rset->getString(2));
0187 
0188     result->setTCCConfigurationFile(rset->getString(3));
0189     result->setLUTConfigurationFile(rset->getString(4));
0190     result->setSLBConfigurationFile(rset->getString(5));
0191     result->setTestPatternFileUrl(rset->getString(6));
0192     result->setNTestPatternsToLoad(rset->getInt(7));
0193     result->setSLBLatency(rset->getInt(12));
0194     //
0195 
0196     Clob clob1 = rset->getClob(8);
0197     cout << "Opening the clob in Read only mode" << endl;
0198     clob1.open(OCCI_LOB_READONLY);
0199     int clobLength = clob1.length();
0200     cout << "Length of the clob1 is: " << clobLength << endl;
0201     unsigned char* buffer = readClob(clob1, clobLength);
0202     clob1.close();
0203     cout << "the clob buffer is:" << endl;
0204     for (int i = 0; i < clobLength; ++i)
0205       cout << (char)buffer[i];
0206     cout << endl;
0207     result->setTCCClob(buffer);
0208 
0209     Clob clob2 = rset->getClob(9);
0210     cout << "Opening the clob in Read only mode" << endl;
0211     clob2.open(OCCI_LOB_READONLY);
0212     clobLength = clob2.length();
0213     cout << "Length of the clob2 is: " << clobLength << endl;
0214     unsigned char* buffer2 = readClob(clob2, clobLength);
0215     clob2.close();
0216     cout << "the clob buffer is:" << endl;
0217     for (int i = 0; i < clobLength; ++i)
0218       cout << (char)buffer2[i];
0219     cout << endl;
0220     result->setLUTClob(buffer2);
0221 
0222     Clob clob3 = rset->getClob(10);
0223     cout << "Opening the clob in Read only mode" << endl;
0224     clob3.open(OCCI_LOB_READONLY);
0225     clobLength = clob3.length();
0226     cout << "Length of the clob3 is: " << clobLength << endl;
0227     unsigned char* buffer3 = readClob(clob3, clobLength);
0228     clob3.close();
0229     cout << "the clob buffer is:" << endl;
0230     for (int i = 0; i < clobLength; ++i)
0231       cout << (char)buffer3[i];
0232     cout << endl;
0233     result->setSLBClob(buffer3);
0234 
0235   } catch (SQLException& e) {
0236     throw(std::runtime_error(std::string("ODTCCEEConfig::fetchData():  ") + e.getMessage()));
0237   }
0238 }
0239 
0240 int ODTCCEEConfig::fetchID() noexcept(false) {
0241   if (m_ID != 0) {
0242     return m_ID;
0243   }
0244 
0245   this->checkConnection();
0246 
0247   try {
0248     Statement* stmt = m_conn->createStatement();
0249     stmt->setSQL(
0250         "SELECT tcc_ee_configuration_id FROM ecal_tcc_ee_configuration "
0251         "WHERE  tcc_ee_tag=:tcc_ee_tag ");
0252 
0253     stmt->setString(1, getConfigTag());
0254 
0255     ResultSet* rset = stmt->executeQuery();
0256 
0257     if (rset->next()) {
0258       m_ID = rset->getInt(1);
0259     } else {
0260       m_ID = 0;
0261     }
0262     m_conn->terminateStatement(stmt);
0263   } catch (SQLException& e) {
0264     throw(std::runtime_error(std::string("ODTCCEEConfig::fetchID:  ") + e.getMessage()));
0265   }
0266 
0267   return m_ID;
0268 }