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