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