Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:59

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