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