File indexing completed on 2024-04-06 12:23:11
0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003
0004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigSeqInfo.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 ODRunConfigSeqInfo::ODRunConfigSeqInfo() {
0012 m_env = nullptr;
0013 m_conn = nullptr;
0014 m_writeStmt = nullptr;
0015 m_readStmt = nullptr;
0016
0017 m_ID = 0;
0018
0019 m_ecal_config_id = 0;
0020 m_seq_num = 0;
0021 m_cycles = 0;
0022 m_run_seq = RunSeqDef();
0023 m_description = "";
0024 }
0025
0026 ODRunConfigSeqInfo::~ODRunConfigSeqInfo() {}
0027
0028
0029 RunSeqDef ODRunConfigSeqInfo::getRunSeqDef() const { return m_run_seq; }
0030 void ODRunConfigSeqInfo::setRunSeqDef(const RunSeqDef& run_seq) {
0031 if (run_seq != m_run_seq) {
0032 m_run_seq = run_seq;
0033 }
0034 }
0035
0036
0037 int ODRunConfigSeqInfo::fetchID() noexcept(false) {
0038
0039 if (m_ID > 0) {
0040 return m_ID;
0041 }
0042
0043 this->checkConnection();
0044
0045 DateHandler dh(m_env, m_conn);
0046
0047 try {
0048 Statement* stmt = m_conn->createStatement();
0049 stmt->setSQL(
0050 "SELECT sequence_id from ECAL_sequence_DAT "
0051 "WHERE ecal_config_id = :id1 "
0052 " and sequence_num = :id2 ");
0053 stmt->setInt(1, m_ecal_config_id);
0054 stmt->setInt(2, m_seq_num);
0055
0056 ResultSet* rset = stmt->executeQuery();
0057
0058 if (rset->next()) {
0059 m_ID = rset->getInt(1);
0060 } else {
0061 m_ID = 0;
0062 }
0063 m_conn->terminateStatement(stmt);
0064 } catch (SQLException& e) {
0065 throw(std::runtime_error("ODRunConfigSeqInfo::fetchID: " + e.getMessage()));
0066 }
0067 setByID(m_ID);
0068 return m_ID;
0069 }
0070
0071 int ODRunConfigSeqInfo::fetchIDLast() noexcept(false) {
0072 this->checkConnection();
0073
0074 DateHandler dh(m_env, m_conn);
0075
0076 try {
0077 Statement* stmt = m_conn->createStatement();
0078 stmt->setSQL("SELECT max(sequence_id) FROM ecal_sequence_dat ");
0079 ResultSet* rset = stmt->executeQuery();
0080
0081 if (rset->next()) {
0082 m_ID = rset->getInt(1);
0083 } else {
0084 m_ID = 0;
0085 }
0086 m_conn->terminateStatement(stmt);
0087 } catch (SQLException& e) {
0088 throw(std::runtime_error("ODRunConfigSeqInfo::fetchIDLast: " + e.getMessage()));
0089 }
0090
0091 setByID(m_ID);
0092 return m_ID;
0093 }
0094
0095 void ODRunConfigSeqInfo::setByID(int id) noexcept(false) {
0096 this->checkConnection();
0097
0098 DateHandler dh(m_env, m_conn);
0099
0100 cout << "ODRunConfigSeqInfo::setByID called for id " << id << endl;
0101
0102 try {
0103 Statement* stmt = m_conn->createStatement();
0104
0105 stmt->setSQL(
0106 "SELECT ecal_config_id, sequence_num, num_of_cycles, sequence_type_def_id, description FROM ECAL_sequence_DAT "
0107 "WHERE sequence_id = :1 ");
0108 stmt->setInt(1, id);
0109
0110 ResultSet* rset = stmt->executeQuery();
0111 if (rset->next()) {
0112 m_ecal_config_id = rset->getInt(1);
0113 m_seq_num = rset->getInt(2);
0114 m_cycles = rset->getInt(3);
0115 int seq_def_id = rset->getInt(4);
0116 m_description = rset->getString(5);
0117 m_ID = id;
0118 m_run_seq.setConnection(m_env, m_conn);
0119 m_run_seq.setByID(seq_def_id);
0120 } else {
0121 throw(std::runtime_error("ODRunConfigSeqInfo::setByID: Given config_id is not in the database"));
0122 }
0123 m_conn->terminateStatement(stmt);
0124 } catch (SQLException& e) {
0125 throw(std::runtime_error("ODRunConfigSeqInfo::setByID: " + e.getMessage()));
0126 }
0127 }
0128
0129 void ODRunConfigSeqInfo::prepareWrite() noexcept(false) {
0130 this->checkConnection();
0131
0132 try {
0133 m_writeStmt = m_conn->createStatement();
0134 m_writeStmt->setSQL(
0135 "INSERT INTO ECAL_SEQUENCE_DAT ( ecal_config_id, "
0136 "sequence_num, num_of_cycles, sequence_type_def_id, description ) "
0137 "VALUES (:1, :2, :3 , :4, :5 )");
0138 } catch (SQLException& e) {
0139 throw(std::runtime_error("ODRunConfigSeqInfo::prepareWrite(): " + e.getMessage()));
0140 }
0141 }
0142 void ODRunConfigSeqInfo::writeDB() noexcept(false) {
0143 this->checkConnection();
0144 this->checkPrepare();
0145
0146
0147 DateHandler dh(m_env, m_conn);
0148
0149 try {
0150
0151
0152 m_run_seq.setConnection(m_env, m_conn);
0153 int seq_def_id = m_run_seq.writeDB();
0154
0155 m_writeStmt->setInt(1, this->getEcalConfigId());
0156 m_writeStmt->setInt(2, this->getSequenceNumber());
0157 m_writeStmt->setInt(3, this->getNumberOfCycles());
0158 m_writeStmt->setInt(4, seq_def_id);
0159 m_writeStmt->setString(5, this->getDescription());
0160
0161 m_writeStmt->executeUpdate();
0162
0163 } catch (SQLException& e) {
0164 throw(std::runtime_error("ODRunConfigSeqInfo::writeDB(): " + e.getMessage()));
0165 }
0166 if (!this->fetchID()) {
0167 throw(std::runtime_error("ODRunConfigSeqInfo::writeDB: Failed to write"));
0168 }
0169 cout << "ODRunConfigSeqInfo::writeDB>> done inserting ODRunConfigSeqInfo with id=" << m_ID << endl;
0170 }
0171
0172 void ODRunConfigSeqInfo::clear() {
0173
0174
0175 m_ID = 0;
0176 m_cycles = 0;
0177 m_run_seq = RunSeqDef();
0178 m_description = "";
0179 }
0180
0181 void ODRunConfigSeqInfo::fetchData(ODRunConfigSeqInfo* result) noexcept(false) {
0182 this->checkConnection();
0183
0184 if (result->getId() == 0) {
0185
0186 result->fetchID();
0187 }
0188
0189 try {
0190 m_readStmt->setSQL(
0191 "SELECT ecal_config_id, sequence_num, num_of_cycles, "
0192 "sequence_type_def_id, description FROM ECAL_sequence_DAT WHERE sequence_id = :1 ");
0193
0194 m_readStmt->setInt(1, result->getId());
0195 ResultSet* rset = m_readStmt->executeQuery();
0196
0197 rset->next();
0198
0199 result->setEcalConfigId(rset->getInt(1));
0200 result->setSequenceNumber(rset->getInt(2));
0201 result->setNumberOfCycles(rset->getInt(3));
0202 int seq_def_id = rset->getInt(4);
0203
0204 m_run_seq.setConnection(m_env, m_conn);
0205 m_run_seq.setByID(seq_def_id);
0206 result->setDescription(rset->getString(5));
0207
0208 } catch (SQLException& e) {
0209 throw(std::runtime_error("ODRunConfigSeqInfo::fetchData(): " + e.getMessage()));
0210 }
0211 }