File indexing completed on 2023-03-17 11:15:24
0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003
0004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigCycleInfo.h"
0005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0007
0008 using namespace std;
0009 using namespace oracle::occi;
0010
0011 ODRunConfigCycleInfo::ODRunConfigCycleInfo() {
0012 m_env = nullptr;
0013 m_conn = nullptr;
0014 m_writeStmt = nullptr;
0015 m_readStmt = nullptr;
0016
0017 m_ID = 0;
0018
0019 m_sequence_id = 0;
0020 m_cycle_num = 0;
0021 m_tag = "";
0022 m_description = "";
0023 }
0024
0025 ODRunConfigCycleInfo::~ODRunConfigCycleInfo() {}
0026
0027 void ODRunConfigCycleInfo::clear() {
0028 m_sequence_id = 0;
0029 m_cycle_num = 0;
0030 m_tag = "";
0031 m_description = "";
0032 }
0033
0034 void ODRunConfigCycleInfo::prepareWrite() noexcept(false) {
0035 this->checkConnection();
0036
0037 try {
0038 m_writeStmt = m_conn->createStatement();
0039 m_writeStmt->setSQL(
0040 "INSERT INTO ECAL_CYCLE_DAT ( sequence_id , cycle_num, tag, description ) "
0041 "VALUES (:1, :2, :3 , :4 )");
0042
0043 } catch (SQLException& e) {
0044 throw(std::runtime_error("ODRunConfigCycleInfo::prepareWrite(): " + e.getMessage()));
0045 }
0046 }
0047
0048 void ODRunConfigCycleInfo::writeDB() noexcept(false) {
0049 this->checkConnection();
0050 this->checkPrepare();
0051
0052
0053 DateHandler dh(m_env, m_conn);
0054
0055 try {
0056 m_writeStmt->setInt(1, this->getSequenceID());
0057 m_writeStmt->setInt(2, this->getCycleNumber());
0058 m_writeStmt->setString(3, this->getTag());
0059 m_writeStmt->setString(4, this->getDescription());
0060 m_writeStmt->executeUpdate();
0061
0062 } catch (SQLException& e) {
0063 throw(std::runtime_error("ODRunConfigCycleInfo::writeDB: " + e.getMessage()));
0064 }
0065
0066 if (!this->fetchID()) {
0067 throw(std::runtime_error("ODRunConfigCycleInfo::writeDB: Failed to write"));
0068 }
0069
0070 cout << "ODRunConfigCycleInfo::writeDB>> done inserting ODRunConfigCycleInfo with id=" << m_ID << endl;
0071 }
0072
0073 int ODRunConfigCycleInfo::fetchID() noexcept(false) {
0074
0075 if (m_ID > 0) {
0076 return m_ID;
0077 }
0078
0079 this->checkConnection();
0080
0081 DateHandler dh(m_env, m_conn);
0082
0083 try {
0084 Statement* stmt = m_conn->createStatement();
0085 stmt->setSQL(
0086 "SELECT cycle_id from ECAL_cycle_DAT "
0087 "WHERE sequence_id = :id1 "
0088 " and cycle_num = :id2 ");
0089 stmt->setInt(1, m_sequence_id);
0090 stmt->setInt(2, m_cycle_num);
0091
0092 ResultSet* rset = stmt->executeQuery();
0093
0094 if (rset->next()) {
0095 m_ID = rset->getInt(1);
0096 } else {
0097 m_ID = 0;
0098 }
0099 m_conn->terminateStatement(stmt);
0100 } catch (SQLException& e) {
0101 throw(std::runtime_error("ODRunConfigCycleInfo::fetchID: " + e.getMessage()));
0102 }
0103 setByID(m_ID);
0104
0105 return m_ID;
0106 }
0107
0108 int ODRunConfigCycleInfo::fetchIDLast() noexcept(false) {
0109 this->checkConnection();
0110
0111 DateHandler dh(m_env, m_conn);
0112
0113 try {
0114 Statement* stmt = m_conn->createStatement();
0115 stmt->setSQL("SELECT max(cycle_id) FROM ecal_cycle_dat ");
0116 ResultSet* rset = stmt->executeQuery();
0117
0118 if (rset->next()) {
0119 m_ID = rset->getInt(1);
0120 } else {
0121 m_ID = 0;
0122 }
0123 m_conn->terminateStatement(stmt);
0124 } catch (SQLException& e) {
0125 throw(std::runtime_error("ODRunConfigCycleInfo::fetchIDLast: " + e.getMessage()));
0126 }
0127
0128 setByID(m_ID);
0129 return m_ID;
0130 }
0131
0132 void ODRunConfigCycleInfo::setByID(int id) noexcept(false) {
0133 this->checkConnection();
0134
0135 DateHandler dh(m_env, m_conn);
0136
0137 cout << "ODRunConfigCycleInfo::setByID called for id " << id << endl;
0138
0139 try {
0140 Statement* stmt = m_conn->createStatement();
0141
0142 stmt->setSQL("SELECT sequence_id , cycle_num , tag , description FROM ECAL_cycle_DAT WHERE cycle_id = :1 ");
0143 stmt->setInt(1, id);
0144
0145 ResultSet* rset = stmt->executeQuery();
0146 if (rset->next()) {
0147 m_sequence_id = rset->getInt(1);
0148 m_cycle_num = rset->getInt(2);
0149 m_tag = rset->getString(3);
0150 m_description = rset->getString(4);
0151 m_ID = id;
0152 } else {
0153 throw(std::runtime_error("ODRunConfigCycleInfo::setByID: Given cycle_id is not in the database"));
0154 }
0155 m_conn->terminateStatement(stmt);
0156 } catch (SQLException& e) {
0157 throw(std::runtime_error("ODRunConfigCycleInfo::setByID: " + e.getMessage()));
0158 }
0159 }
0160
0161 void ODRunConfigCycleInfo::fetchData(ODRunConfigCycleInfo* result) noexcept(false) {
0162 this->checkConnection();
0163 result->clear();
0164 if (result->getId() == 0) {
0165 throw(std::runtime_error("ODRunConfigCycleInfo::fetchData(): no Id defined for this ODRunConfigCycleInfo "));
0166 }
0167
0168 try {
0169 m_readStmt->setSQL("SELECT sequence_id , cycle_num , tag , description FROM ECAL_cycle_DAT WHERE cycle_id = :1 ");
0170
0171 m_readStmt->setInt(1, result->getId());
0172 ResultSet* rset = m_readStmt->executeQuery();
0173
0174 rset->next();
0175
0176 result->setSequenceID(rset->getInt(1));
0177 result->setCycleNumber(rset->getInt(2));
0178 result->setTag(rset->getString(3));
0179 result->setDescription(rset->getString(4));
0180
0181 } catch (SQLException& e) {
0182 throw(std::runtime_error("ODRunConfigCycleInfo::fetchData(): " + e.getMessage()));
0183 }
0184 }
0185
0186 void ODRunConfigCycleInfo::insertConfig() noexcept(false) {
0187 try {
0188 prepareWrite();
0189 writeDB();
0190 m_conn->commit();
0191 terminateWriteStatement();
0192 } catch (std::runtime_error& e) {
0193 m_conn->rollback();
0194 throw(e);
0195 } catch (...) {
0196 m_conn->rollback();
0197 throw(std::runtime_error("EcalCondDBInterface::insertDataSet: Unknown exception caught"));
0198 }
0199 }