File indexing completed on 2024-04-06 12:23:04
0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigSpikeDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigSpikeInfo.h"
0007
0008 using namespace std;
0009 using namespace oracle::occi;
0010
0011 FEConfigSpikeDat::FEConfigSpikeDat() {
0012 m_env = nullptr;
0013 m_conn = nullptr;
0014 m_writeStmt = nullptr;
0015 m_readStmt = nullptr;
0016
0017 m_thr = 0;
0018 }
0019
0020 FEConfigSpikeDat::~FEConfigSpikeDat() {}
0021
0022 void FEConfigSpikeDat::prepareWrite() noexcept(false) {
0023 this->checkConnection();
0024
0025 try {
0026 m_writeStmt = m_conn->createStatement();
0027 m_writeStmt->setSQL("INSERT INTO " + getTable() +
0028 " (spi_conf_id, logic_id, "
0029 "spike_threshold ) "
0030 "VALUES (:spi_conf_id, :logic_id, "
0031 ":spike_threshold )");
0032 } catch (SQLException& e) {
0033 throw(std::runtime_error("FEConfigSpikeDat::prepareWrite(): " + e.getMessage()));
0034 }
0035 }
0036
0037 void FEConfigSpikeDat::writeDB(const EcalLogicID* ecid,
0038 const FEConfigSpikeDat* item,
0039 FEConfigSpikeInfo* iconf) noexcept(false) {
0040 this->checkConnection();
0041 this->checkPrepare();
0042
0043 int iconfID = iconf->fetchID();
0044 if (!iconfID) {
0045 throw(std::runtime_error("FEConfigSpikeDat::writeDB: ICONF not in DB"));
0046 }
0047
0048 int logicID = ecid->getLogicID();
0049 if (!logicID) {
0050 throw(std::runtime_error("FEConfigSpikeDat::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->getSpikeThreshold());
0057
0058 m_writeStmt->executeUpdate();
0059 } catch (SQLException& e) {
0060 throw(std::runtime_error("FEConfigSpikeDat::writeDB(): " + e.getMessage()));
0061 }
0062 }
0063
0064 void FEConfigSpikeDat::fetchData(map<EcalLogicID, FEConfigSpikeDat>* fillMap,
0065 FEConfigSpikeInfo* 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
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.spike_threshold FROM channelview cv JOIN " +
0080 getTable() +
0081 " d ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0082 "WHERE spi_conf_id = :spi_conf_id");
0083 m_readStmt->setInt(1, iconfID);
0084 ResultSet* rset = m_readStmt->executeQuery();
0085
0086 std::pair<EcalLogicID, FEConfigSpikeDat> p;
0087 FEConfigSpikeDat dat;
0088 while (rset->next()) {
0089 p.first = EcalLogicID(rset->getString(1),
0090 rset->getInt(2),
0091 rset->getInt(3),
0092 rset->getInt(4),
0093 rset->getInt(5),
0094 rset->getString(6));
0095
0096 dat.setSpikeThreshold(rset->getInt(7));
0097
0098 p.second = dat;
0099 fillMap->insert(p);
0100 }
0101 } catch (SQLException& e) {
0102 throw(std::runtime_error("FEConfigSpikeDat::fetchData: " + e.getMessage()));
0103 }
0104 }
0105
0106 void FEConfigSpikeDat::writeArrayDB(const std::map<EcalLogicID, FEConfigSpikeDat>* data,
0107 FEConfigSpikeInfo* iconf) noexcept(false) {
0108 this->checkConnection();
0109 this->checkPrepare();
0110
0111 int iconfID = iconf->fetchID();
0112 if (!iconfID) {
0113 throw(std::runtime_error("FEConfigSpikeDat::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 FEConfigSpikeDat* dataitem;
0127 int count = 0;
0128 typedef map<EcalLogicID, FEConfigSpikeDat>::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("FEConfigSpikeDat::writeArrayDB: Bad EcalLogicID"));
0134 }
0135 ids[count] = logicID;
0136 iconfid_vec[count] = iconfID;
0137
0138 dataitem = &(p->second);
0139
0140 int x = dataitem->getSpikeThreshold();
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("FEConfigSpikeDat::writeArrayDB(): " + e.getMessage()));
0169 }
0170 }