File indexing completed on 2024-04-06 12:23:12
0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004
0005 #include "OnlineDB/EcalCondDB/interface/ODWeightsSamplesDat.h"
0006
0007 using namespace std;
0008 using namespace oracle::occi;
0009
0010 ODWeightsSamplesDat::ODWeightsSamplesDat() {
0011 m_env = nullptr;
0012 m_conn = nullptr;
0013 m_writeStmt = nullptr;
0014 m_readStmt = nullptr;
0015
0016 m_fed = 0;
0017 }
0018
0019 ODWeightsSamplesDat::~ODWeightsSamplesDat() {}
0020
0021 void ODWeightsSamplesDat::clear() {
0022 m_fed = 0;
0023 m_ss = 0;
0024 m_sn = 0;
0025 }
0026
0027 void ODWeightsSamplesDat::prepareWrite() noexcept(false) {
0028 this->checkConnection();
0029
0030 try {
0031 m_writeStmt = m_conn->createStatement();
0032 m_writeStmt->setSQL("INSERT INTO " + getTable() +
0033 " (rec_id, fed_id, sample_id, weight_number ) "
0034 "VALUES (:1, :2, :3, :4 )");
0035 } catch (SQLException& e) {
0036 throw(std::runtime_error("ODWeightsSamplesDat::prepareWrite(): " + e.getMessage()));
0037 }
0038 }
0039
0040 void ODWeightsSamplesDat::writeDB(const ODWeightsSamplesDat* item, ODFEWeightsInfo* iov) noexcept(false) {
0041 this->checkConnection();
0042
0043 try {
0044 m_writeStmt->setInt(1, item->getId());
0045 m_writeStmt->setInt(2, item->getFedId());
0046 m_writeStmt->setInt(3, item->getSampleId());
0047 m_writeStmt->setInt(4, item->getWeightNumber());
0048
0049 m_writeStmt->executeUpdate();
0050 } catch (SQLException& e) {
0051 throw(std::runtime_error("ODWeightsSamplesDat::writeDB(): " + e.getMessage()));
0052 }
0053 }
0054
0055 void ODWeightsSamplesDat::fetchData(std::vector<ODWeightsSamplesDat>* p, ODFEWeightsInfo* iov) noexcept(false) {
0056 this->checkConnection();
0057
0058 iov->setConnection(m_env, m_conn);
0059 int iovID = iov->fetchID();
0060 if (!iovID) {
0061 std::cout << "ID not in the DB" << endl;
0062 return;
0063 }
0064
0065 try {
0066 m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :rec_id order by fed_id, sample_id");
0067 m_readStmt->setInt(1, iovID);
0068 ResultSet* rset = m_readStmt->executeQuery();
0069
0070
0071 ODWeightsSamplesDat dat;
0072 while (rset->next()) {
0073
0074 dat.setFedId(rset->getInt(2));
0075 dat.setSampleId(rset->getInt(3));
0076 dat.setWeightNumber(rset->getInt(4));
0077
0078 p->push_back(dat);
0079 }
0080
0081 } catch (SQLException& e) {
0082 throw(std::runtime_error("ODWeightsSamplesDat::fetchData(): " + e.getMessage()));
0083 }
0084 }
0085
0086 void ODWeightsSamplesDat::fetchData(ODWeightsSamplesDat* p) noexcept(false) {
0087 this->checkConnection();
0088
0089 try {
0090 m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :1 order by fed_id, sample_id");
0091 m_readStmt->setInt(1, p->getId());
0092 ResultSet* rset = m_readStmt->executeQuery();
0093
0094 while (rset->next()) {
0095 p->setFedId(rset->getInt(2));
0096 p->setSampleId(rset->getInt(3));
0097 p->setWeightNumber(rset->getInt(4));
0098 }
0099
0100 } catch (SQLException& e) {
0101 throw(std::runtime_error("ODWeightsSamplesDat::fetchData(): " + e.getMessage()));
0102 }
0103 }
0104
0105
0106
0107 void ODWeightsSamplesDat::writeArrayDB(const std::vector<ODWeightsSamplesDat>& data,
0108 ODFEWeightsInfo* iov) noexcept(false) {
0109 this->checkConnection();
0110
0111 int iovID = iov->fetchID();
0112 if (!iovID) {
0113 throw(std::runtime_error("ODDelays::writeArrayDB: ODFEDelaysInfo not in DB"));
0114 }
0115
0116 int nrows = data.size();
0117 int* ids = new int[nrows];
0118 int* xx = new int[nrows];
0119 int* yy = new int[nrows];
0120 int* zz = new int[nrows];
0121
0122 ub2* ids_len = new ub2[nrows];
0123 ub2* x_len = new ub2[nrows];
0124 ub2* y_len = new ub2[nrows];
0125 ub2* z_len = new ub2[nrows];
0126
0127 ODWeightsSamplesDat dataitem;
0128
0129 int n_data = (int)data.size();
0130 for (int count = 0; count < n_data; count++) {
0131 dataitem = data[count];
0132 ids[count] = iovID;
0133 xx[count] = dataitem.getFedId();
0134 yy[count] = dataitem.getSampleId();
0135 zz[count] = dataitem.getWeightNumber();
0136
0137 ids_len[count] = sizeof(ids[count]);
0138 x_len[count] = sizeof(xx[count]);
0139 y_len[count] = sizeof(yy[count]);
0140 z_len[count] = sizeof(zz[count]);
0141 }
0142
0143 try {
0144 m_writeStmt->setDataBuffer(1, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0145 m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0146 m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT, sizeof(yy[0]), y_len);
0147 m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT, sizeof(zz[0]), z_len);
0148
0149 m_writeStmt->executeArrayUpdate(nrows);
0150
0151 delete[] ids;
0152 delete[] xx;
0153 delete[] yy;
0154 delete[] zz;
0155
0156 delete[] ids_len;
0157 delete[] x_len;
0158 delete[] y_len;
0159 delete[] z_len;
0160
0161 } catch (SQLException& e) {
0162 throw(std::runtime_error("ODWeightsSamplesDat::writeArrayDB(): " + e.getMessage()));
0163 }
0164 }