Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-09 02:18:49

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/RunPNErrorsDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 RunPNErrorsDat::RunPNErrorsDat() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_writeStmt = nullptr;
0015   m_readStmt = nullptr;
0016 
0017   m_errorBits = 0;
0018 }
0019 
0020 RunPNErrorsDat::~RunPNErrorsDat() {}
0021 
0022 void RunPNErrorsDat::prepareWrite() noexcept(false) {
0023   this->checkConnection();
0024 
0025   try {
0026     m_writeStmt = m_conn->createStatement();
0027     /* Using TO_NUMBER because OCCI does not support 64-bit integers well */
0028     m_writeStmt->setSQL(
0029         "INSERT INTO run_pn_errors_dat (iov_id, logic_id, "
0030         "error_bits) "
0031         "VALUES (:iov_id, :logic_id, "
0032         "to_number(:error_bits))");
0033   } catch (SQLException& e) {
0034     throw(std::runtime_error("RunPNErrorsDat::prepareWrite():  " + e.getMessage()));
0035   }
0036 }
0037 
0038 void RunPNErrorsDat::writeDB(const EcalLogicID* ecid, const RunPNErrorsDat* item, RunIOV* iov) noexcept(false) {
0039   this->checkConnection();
0040   this->checkPrepare();
0041 
0042   int iovID = iov->fetchID();
0043   if (!iovID) {
0044     throw(std::runtime_error("RunPNErrorsDat::writeDB:  IOV not in DB"));
0045   }
0046 
0047   int logicID = ecid->getLogicID();
0048   if (!logicID) {
0049     throw(std::runtime_error("RunPNErrorsDat::writeDB:  Bad EcalLogicID"));
0050   }
0051 
0052   try {
0053     m_writeStmt->setInt(1, iovID);
0054     m_writeStmt->setInt(2, logicID);
0055     m_writeStmt->setString(3, std::to_string(item->getErrorBits()));
0056     m_writeStmt->executeUpdate();
0057   } catch (SQLException& e) {
0058     throw(std::runtime_error("RunPNErrorsDat::writeDB():  " + e.getMessage()));
0059   }
0060 }
0061 
0062 void RunPNErrorsDat::fetchData(map<EcalLogicID, RunPNErrorsDat>* fillMap, RunIOV* iov) noexcept(false) {
0063   this->checkConnection();
0064   fillMap->clear();
0065 
0066   iov->setConnection(m_env, m_conn);
0067   int iovID = iov->fetchID();
0068   if (!iovID) {
0069     //  throw(std::runtime_error("RunPNErrorsDat::writeDB:  IOV not in DB"));
0070     return;
0071   }
0072 
0073   try {
0074     /* Using TO_CHAR because OCCI does not support 64-bit integers well */
0075     m_readStmt->setSQL(
0076         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0077         "to_char(d.error_bits) "
0078         "FROM channelview cv JOIN run_pn_errors_dat d "
0079         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0080         "WHERE d.iov_id = :iov_id");
0081     m_readStmt->setInt(1, iovID);
0082     ResultSet* rset = m_readStmt->executeQuery();
0083 
0084     std::pair<EcalLogicID, RunPNErrorsDat> p;
0085     RunPNErrorsDat dat;
0086     while (rset->next()) {
0087       p.first = EcalLogicID(rset->getString(1),   // name
0088                             rset->getInt(2),      // logic_id
0089                             rset->getInt(3),      // id1
0090                             rset->getInt(4),      // id2
0091                             rset->getInt(5),      // id3
0092                             rset->getString(6));  // maps_to
0093 
0094       dat.setErrorBits(std::stoul(rset->getString(7)));
0095 
0096       p.second = dat;
0097       fillMap->insert(p);
0098     }
0099 
0100   } catch (SQLException& e) {
0101     throw(std::runtime_error("RunPNErrorsDat::fetchData():  " + e.getMessage()));
0102   }
0103 }