Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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