Back to home page

Project CMSSW displayed by LXR

 
 

    


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/ODTTCciCycle.h"
0005 
0006 using namespace std;
0007 using namespace oracle::occi;
0008 
0009 ODTTCciCycle::ODTTCciCycle() {
0010   m_env = nullptr;
0011   m_conn = nullptr;
0012   m_writeStmt = nullptr;
0013   m_readStmt = nullptr;
0014   //
0015   m_ID = 0;
0016   m_ttcci_config_id = 0;
0017 }
0018 
0019 ODTTCciCycle::~ODTTCciCycle() {}
0020 
0021 void ODTTCciCycle::prepareWrite() noexcept(false) {
0022   this->checkConnection();
0023 
0024   try {
0025     m_writeStmt = m_conn->createStatement();
0026     m_writeStmt->setSQL(
0027         "INSERT INTO ECAL_TTCci_Cycle (cycle_id, ttcci_configuration_id ) "
0028         "VALUES (:1, :2 )");
0029   } catch (SQLException &e) {
0030     throw(std::runtime_error("ODTTCciCycle::prepareWrite():  " + e.getMessage()));
0031   }
0032 }
0033 
0034 void ODTTCciCycle::writeDB() noexcept(false) {
0035   this->checkConnection();
0036   this->checkPrepare();
0037 
0038   try {
0039     m_writeStmt->setInt(1, this->getId());
0040     m_writeStmt->setInt(2, this->getTTCciConfigurationID());
0041 
0042     m_writeStmt->executeUpdate();
0043 
0044   } catch (SQLException &e) {
0045     throw(std::runtime_error("ODTTCciCycle::writeDB:  " + e.getMessage()));
0046   }
0047 
0048   // Now get the ID
0049   if (!this->fetchID()) {
0050     throw(std::runtime_error("ODTTCciCycle::writeDB:  Failed to write"));
0051   }
0052 }
0053 
0054 void ODTTCciCycle::clear() { m_ttcci_config_id = 0; }
0055 
0056 int ODTTCciCycle::fetchID() noexcept(false) {
0057   // Return from memory if available
0058   if (m_ID) {
0059     return m_ID;
0060   }
0061 
0062   this->checkConnection();
0063 
0064   try {
0065     Statement *stmt = m_conn->createStatement();
0066     stmt->setSQL(
0067         "SELECT cycle_id, ttcci_configuration_id FROM ecal_ttcci_cycle "
0068         "WHERE cycle_id = :1 ");
0069     stmt->setInt(1, m_ID);
0070     ResultSet *rset = stmt->executeQuery();
0071 
0072     if (rset->next()) {
0073       m_ID = rset->getInt(1);
0074       m_ttcci_config_id = rset->getInt(2);
0075     } else {
0076       m_ID = 0;
0077     }
0078     m_conn->terminateStatement(stmt);
0079   } catch (SQLException &e) {
0080     throw(std::runtime_error("ODTTCciCycle::fetchID:  " + e.getMessage()));
0081   }
0082 
0083   return m_ID;
0084 }
0085 
0086 void ODTTCciCycle::setByID(int id) noexcept(false) {
0087   this->checkConnection();
0088 
0089   try {
0090     Statement *stmt = m_conn->createStatement();
0091     stmt->setSQL(
0092         "SELECT cycle_id, ttcci_configuration_id FROM ecal_ttcci_cycle "
0093         "WHERE cycle_id = :1 ");
0094     stmt->setInt(1, id);
0095     ResultSet *rset = stmt->executeQuery();
0096 
0097     if (rset->next()) {
0098       m_ID = rset->getInt(1);
0099       m_ttcci_config_id = rset->getInt(2);
0100     } else {
0101       m_ID = 0;
0102     }
0103     m_conn->terminateStatement(stmt);
0104   } catch (SQLException &e) {
0105     throw(std::runtime_error("ODTTCciCycle::fetchID:  " + e.getMessage()));
0106   }
0107 }
0108 
0109 void ODTTCciCycle::fetchData(ODTTCciCycle *result) noexcept(false) {
0110   this->checkConnection();
0111   result->clear();
0112 
0113   if (result->getId() == 0) {
0114     throw(std::runtime_error("ODTTCciConfig::fetchData(): no Id defined for this ODTTCciConfig "));
0115   }
0116 
0117   try {
0118     m_readStmt->setSQL(
0119         "SELECT  ttcci_configuration_id FROM ecal_ttcci_cycle "
0120         "WHERE cycle_id = :1 ");
0121 
0122     m_readStmt->setInt(1, result->getId());
0123     ResultSet *rset = m_readStmt->executeQuery();
0124 
0125     rset->next();
0126 
0127     result->setTTCciConfigurationID(rset->getInt(1));
0128 
0129   } catch (SQLException &e) {
0130     throw(std::runtime_error("ODTTCciCycle::fetchData():  " + e.getMessage()));
0131   }
0132 }
0133 
0134 void ODTTCciCycle::insertConfig() noexcept(false) {
0135   try {
0136     prepareWrite();
0137     writeDB();
0138     m_conn->commit();
0139     terminateWriteStatement();
0140   } catch (std::runtime_error &e) {
0141     m_conn->rollback();
0142     throw(e);
0143   } catch (...) {
0144     m_conn->rollback();
0145     throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
0146   }
0147 }