Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigWeightDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigWeightInfo.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 FEConfigWeightDat::FEConfigWeightDat() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_writeStmt = nullptr;
0015   m_readStmt = nullptr;
0016 
0017   m_group_id = 0;
0018 }
0019 
0020 FEConfigWeightDat::~FEConfigWeightDat() {}
0021 
0022 void FEConfigWeightDat::prepareWrite() noexcept(false) {
0023   this->checkConnection();
0024 
0025   try {
0026     m_writeStmt = m_conn->createStatement();
0027     m_writeStmt->setSQL(
0028         "INSERT INTO fe_config_weight_dat (wei_conf_id, logic_id, "
0029         "group_id ) "
0030         "VALUES (:wei_conf_id, :logic_id, "
0031         ":group_id )");
0032   } catch (SQLException& e) {
0033     throw(std::runtime_error("FEConfigWeightDat::prepareWrite():  " + e.getMessage()));
0034   }
0035 }
0036 
0037 void FEConfigWeightDat::writeDB(const EcalLogicID* ecid,
0038                                 const FEConfigWeightDat* item,
0039                                 FEConfigWeightInfo* iconf) noexcept(false) {
0040   this->checkConnection();
0041   this->checkPrepare();
0042 
0043   int iconfID = iconf->fetchID();
0044   if (!iconfID) {
0045     throw(std::runtime_error("FEConfigWeightDat::writeDB:  ICONF not in DB"));
0046   }
0047 
0048   int logicID = ecid->getLogicID();
0049   if (!logicID) {
0050     throw(std::runtime_error("FEConfigWeightDat::writeDB:  Bad EcalLogicID"));
0051   }
0052 
0053   try {
0054     m_writeStmt->setInt(1, iconfID);
0055     m_writeStmt->setInt(2, logicID);
0056     m_writeStmt->setInt(3, item->getWeightGroupId());
0057 
0058     m_writeStmt->executeUpdate();
0059   } catch (SQLException& e) {
0060     throw(std::runtime_error("FEConfigWeightDat::writeDB():  " + e.getMessage()));
0061   }
0062 }
0063 
0064 void FEConfigWeightDat::fetchData(map<EcalLogicID, FEConfigWeightDat>* fillMap,
0065                                   FEConfigWeightInfo* iconf) noexcept(false) {
0066   this->checkConnection();
0067   fillMap->clear();
0068 
0069   iconf->setConnection(m_env, m_conn);
0070   int iconfID = iconf->fetchID();
0071   if (!iconfID) {
0072     //  throw(std::runtime_error("FEConfigWeightDat::writeDB:  ICONF not in DB"));
0073     return;
0074   }
0075 
0076   try {
0077     m_readStmt->setSQL(
0078         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0079         "d.group_id  "
0080         "FROM channelview cv JOIN fe_config_weight_dat d "
0081         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0082         "WHERE wei_conf_id = :wei_conf_id");
0083     m_readStmt->setInt(1, iconfID);
0084     ResultSet* rset = m_readStmt->executeQuery();
0085 
0086     std::pair<EcalLogicID, FEConfigWeightDat> p;
0087     FEConfigWeightDat dat;
0088     while (rset->next()) {
0089       p.first = EcalLogicID(rset->getString(1),   // name
0090                             rset->getInt(2),      // logic_id
0091                             rset->getInt(3),      // id1
0092                             rset->getInt(4),      // id2
0093                             rset->getInt(5),      // id3
0094                             rset->getString(6));  // maps_to
0095 
0096       dat.setWeightGroupId(rset->getInt(7));
0097 
0098       p.second = dat;
0099       fillMap->insert(p);
0100     }
0101   } catch (SQLException& e) {
0102     throw(std::runtime_error("FEConfigWeightDat::fetchData:  " + e.getMessage()));
0103   }
0104 }
0105 
0106 void FEConfigWeightDat::writeArrayDB(const std::map<EcalLogicID, FEConfigWeightDat>* data,
0107                                      FEConfigWeightInfo* iconf) noexcept(false) {
0108   this->checkConnection();
0109   this->checkPrepare();
0110 
0111   int iconfID = iconf->fetchID();
0112   if (!iconfID) {
0113     throw(std::runtime_error("FEConfigWeightDat::writeArrayDB:  ICONF not in DB"));
0114   }
0115 
0116   int nrows = data->size();
0117   int* ids = new int[nrows];
0118   int* iconfid_vec = new int[nrows];
0119   int* xx = new int[nrows];
0120 
0121   ub2* ids_len = new ub2[nrows];
0122   ub2* iconf_len = new ub2[nrows];
0123   ub2* x_len = new ub2[nrows];
0124 
0125   const EcalLogicID* channel;
0126   const FEConfigWeightDat* dataitem;
0127   int count = 0;
0128   typedef map<EcalLogicID, FEConfigWeightDat>::const_iterator CI;
0129   for (CI p = data->begin(); p != data->end(); ++p) {
0130     channel = &(p->first);
0131     int logicID = channel->getLogicID();
0132     if (!logicID) {
0133       throw(std::runtime_error("FEConfigWeightDat::writeArrayDB:  Bad EcalLogicID"));
0134     }
0135     ids[count] = logicID;
0136     iconfid_vec[count] = iconfID;
0137 
0138     dataitem = &(p->second);
0139     // dataIface.writeDB( channel, dataitem, iconf);
0140     int x = dataitem->getWeightGroupId();
0141 
0142     xx[count] = x;
0143 
0144     ids_len[count] = sizeof(ids[count]);
0145     iconf_len[count] = sizeof(iconfid_vec[count]);
0146 
0147     x_len[count] = sizeof(xx[count]);
0148 
0149     count++;
0150   }
0151 
0152   try {
0153     m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]), iconf_len);
0154     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0155     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0156 
0157     m_writeStmt->executeArrayUpdate(nrows);
0158 
0159     delete[] ids;
0160     delete[] iconfid_vec;
0161     delete[] xx;
0162 
0163     delete[] ids_len;
0164     delete[] iconf_len;
0165     delete[] x_len;
0166 
0167   } catch (SQLException& e) {
0168     throw(std::runtime_error("FEConfigWeightDat::writeArrayDB():  " + e.getMessage()));
0169   }
0170 }