Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:13

0001 #include <string>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/RunSeqDef.h"
0005 
0006 using namespace std;
0007 using namespace oracle::occi;
0008 
0009 RunSeqDef::RunSeqDef() {
0010   m_env = nullptr;
0011   m_conn = nullptr;
0012   m_ID = 0;
0013   m_runSeq = "";
0014   m_runType = RunTypeDef();
0015 }
0016 
0017 RunSeqDef::~RunSeqDef() {}
0018 
0019 string RunSeqDef::getRunSeq() const { return m_runSeq; }
0020 
0021 void RunSeqDef::setRunSeq(string runseq) { m_runSeq = runseq; }
0022 
0023 RunTypeDef RunSeqDef::getRunTypeDef() const { return m_runType; }
0024 
0025 void RunSeqDef::setRunTypeDef(const RunTypeDef& runTypeDef) { m_runType = runTypeDef; }
0026 
0027 int RunSeqDef::fetchID() noexcept(false) {
0028   // Return def from memory if available
0029   if (m_ID) {
0030     return m_ID;
0031   }
0032 
0033   this->checkConnection();
0034 
0035   // get the run type
0036   m_runType.setConnection(m_env, m_conn);
0037   int run_type_id = m_runType.fetchID();
0038 
0039   try {
0040     Statement* stmt = m_conn->createStatement();
0041     stmt->setSQL(
0042         "SELECT def_id FROM ecal_sequence_type_def WHERE "
0043         " run_type_def_id   = :1 and sequence_type_string = :2 ");
0044 
0045     stmt->setInt(1, run_type_id);
0046     stmt->setString(2, m_runSeq);
0047 
0048     ResultSet* rset = stmt->executeQuery();
0049 
0050     if (rset->next()) {
0051       m_ID = rset->getInt(1);
0052     } else {
0053       m_ID = 0;
0054     }
0055     m_conn->terminateStatement(stmt);
0056   } catch (SQLException& e) {
0057     throw(std::runtime_error("RunSeqDef::fetchID:  " + e.getMessage()));
0058   }
0059 
0060   return m_ID;
0061 }
0062 
0063 void RunSeqDef::setByID(int id) noexcept(false) {
0064   this->checkConnection();
0065 
0066   try {
0067     Statement* stmt = m_conn->createStatement();
0068 
0069     stmt->setSQL("SELECT run_type_def_id, sequence_type_string FROM ecal_sequence_type_def WHERE def_id = :1");
0070     stmt->setInt(1, id);
0071 
0072     int idruntype = 0;
0073     ResultSet* rset = stmt->executeQuery();
0074     if (rset->next()) {
0075       idruntype = rset->getInt(1);
0076       m_runSeq = rset->getString(2);
0077     } else {
0078       throw(std::runtime_error("RunSeqDef::setByID:  Given def_id is not in the database"));
0079     }
0080 
0081     m_conn->terminateStatement(stmt);
0082 
0083     m_runType.setConnection(m_env, m_conn);
0084     m_runType.setByID(idruntype);
0085 
0086   } catch (SQLException& e) {
0087     throw(std::runtime_error("RunSeqDef::setByID:  " + e.getMessage()));
0088   }
0089 }
0090 
0091 void RunSeqDef::fetchAllDefs(std::vector<RunSeqDef>* fillVec) noexcept(false) {
0092   this->checkConnection();
0093   try {
0094     Statement* stmt = m_conn->createStatement();
0095     stmt->setSQL("SELECT def_id FROM ecal_sequence_type_def ORDER BY def_id");
0096     ResultSet* rset = stmt->executeQuery();
0097 
0098     RunSeqDef runSeqDef;
0099     runSeqDef.setConnection(m_env, m_conn);
0100 
0101     while (rset->next()) {
0102       runSeqDef.setByID(rset->getInt(1));
0103       fillVec->push_back(runSeqDef);
0104     }
0105   } catch (SQLException& e) {
0106     throw(std::runtime_error("RunSeqDef::fetchAllDefs:  " + e.getMessage()));
0107   }
0108 }
0109 
0110 int RunSeqDef::writeDB() noexcept(false) {
0111   // see if this data is already in the DB
0112   try {
0113     if (this->fetchID()) {
0114       return m_ID;
0115     }
0116   } catch (SQLException& e) {
0117     // it does not exist yet
0118   }
0119 
0120   // check the connectioin
0121   this->checkConnection();
0122 
0123   // get the run type
0124   m_runType.setConnection(m_env, m_conn);
0125   int run_type_id = m_runType.fetchID();
0126 
0127   // write new seq def to the DB
0128   try {
0129     Statement* stmt = m_conn->createStatement();
0130 
0131     stmt->setSQL(
0132         "insert into ecal_sequence_type_def(RUN_TYPE_DEF_ID, SEQUENCE_TYPE_STRING) values "
0133         " ( :1, :2 )");
0134     stmt->setInt(1, run_type_id);
0135     stmt->setString(2, m_runSeq);
0136 
0137     stmt->executeUpdate();
0138 
0139     m_conn->terminateStatement(stmt);
0140   } catch (SQLException& e) {
0141     throw(std::runtime_error("RunSeqDef::writeDB:  " + e.getMessage()));
0142   }
0143 
0144   // now get the tag_id
0145   if (!this->fetchID()) {
0146     throw(std::runtime_error("RunSeqDef::writeDB:  Failed to write"));
0147   }
0148 
0149   return m_ID;
0150 }