CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/OnlineDB/EcalCondDB/src/ODJBH4Cycle.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/ODJBH4Cycle.h"
00005 
00006 using namespace std;
00007 using namespace oracle::occi;
00008 
00009 ODJBH4Cycle::ODJBH4Cycle()
00010 {
00011   m_env = NULL;
00012   m_conn = NULL;
00013   m_writeStmt = NULL;
00014   m_readStmt = NULL;
00015   //
00016   m_ID = 0;
00017   m_jbh4_config_id = 0;
00018 }
00019 
00020 
00021 ODJBH4Cycle::~ODJBH4Cycle()
00022 {
00023 }
00024 
00025 
00026 void ODJBH4Cycle::prepareWrite()
00027   throw(std::runtime_error)
00028 {
00029   this->checkConnection();
00030 
00031   try {
00032     m_writeStmt = m_conn->createStatement();
00033     m_writeStmt->setSQL("INSERT INTO ECAL_JBH4_Cycle (cycle_id, jbh4_configuration_id ) "
00034                  "VALUES (:1, :2 )");
00035   } catch (SQLException &e) {
00036     throw(std::runtime_error("ODJBH4Cycle::prepareWrite():  "+e.getMessage()));
00037   }
00038 }
00039 
00040 
00041 void ODJBH4Cycle::writeDB()  throw(std::runtime_error)
00042 {
00043   this->checkConnection();
00044   this->checkPrepare();
00045 
00046   try {
00047 
00048     m_writeStmt->setInt(1, this->getId());
00049     m_writeStmt->setInt(2, this->getJBH4ConfigurationID());
00050 
00051     m_writeStmt->executeUpdate();
00052 
00053 
00054   } catch (SQLException &e) {
00055     throw(std::runtime_error("ODJBH4Cycle::writeDB:  "+e.getMessage()));
00056   }
00057 
00058   // Now get the ID
00059   if (!this->fetchID()) {
00060     throw(std::runtime_error("ODJBH4Cycle::writeDB:  Failed to write"));
00061   }
00062   
00063  
00064 }
00065 
00066 void ODJBH4Cycle::clear(){
00067   m_jbh4_config_id=0;
00068 }
00069 
00070 
00071 int ODJBH4Cycle::fetchID()
00072   throw(std::runtime_error)
00073 {
00074   // Return from memory if available
00075   if (m_ID) {
00076     return m_ID;
00077   }
00078 
00079   this->checkConnection();
00080 
00081   try {
00082     Statement* stmt = m_conn->createStatement();
00083     stmt->setSQL("SELECT cycle_id, jbh4_configuration_id FROM ecal_jbh4_cycle "
00084                  "WHERE cycle_id = :1 ");
00085     stmt->setInt(1, m_ID);
00086     ResultSet* rset = stmt->executeQuery();
00087 
00088     if (rset->next()) {
00089       m_ID = rset->getInt(1);
00090       m_jbh4_config_id = rset->getInt(2);
00091     } else {
00092       m_ID = 0;
00093     }
00094     m_conn->terminateStatement(stmt);
00095   } catch (SQLException &e) {
00096     throw(std::runtime_error("ODJBH4Cycle::fetchID:  "+e.getMessage()));
00097   }
00098 
00099   return m_ID;
00100 }
00101 
00102 
00103 
00104 void ODJBH4Cycle::setByID(int id) 
00105   throw(std::runtime_error)
00106 {
00107    this->checkConnection();
00108 
00109 
00110   try {
00111     Statement* stmt = m_conn->createStatement();
00112     stmt->setSQL("SELECT cycle_id, jbh4_configuration_id FROM ecal_jbh4_cycle "
00113                  "WHERE cycle_id = :1 ");
00114     stmt->setInt(1, id);
00115     ResultSet* rset = stmt->executeQuery();
00116 
00117     if (rset->next()) {
00118       m_ID = rset->getInt(1);
00119       m_jbh4_config_id = rset->getInt(2);
00120     } else {
00121       m_ID = 0;
00122     }
00123     m_conn->terminateStatement(stmt);
00124   } catch (SQLException &e) {
00125     throw(std::runtime_error("ODJBH4Cycle::fetchID:  "+e.getMessage()));
00126   }
00127 }
00128 
00129 
00130 
00131 void ODJBH4Cycle::fetchData(ODJBH4Cycle * result)
00132   throw(std::runtime_error)
00133 {
00134   this->checkConnection();
00135   result->clear();
00136 
00137   if(result->getId()==0){
00138     throw(std::runtime_error("ODJBH4Config::fetchData(): no Id defined for this ODJBH4Config "));
00139   }
00140 
00141   try {
00142 
00143     m_readStmt->setSQL("SELECT  jbh4_configuration_id FROM ecal_jbh4_cycle "
00144                  "WHERE cycle_id = :1 ");
00145 
00146     m_readStmt->setInt(1, result->getId());
00147     ResultSet* rset = m_readStmt->executeQuery();
00148 
00149     rset->next();
00150 
00151     result->setJBH4ConfigurationID(       rset->getInt(1) );
00152 
00153   } catch (SQLException &e) {
00154     throw(std::runtime_error("ODJBH4Cycle::fetchData():  "+e.getMessage()));
00155   }
00156 }
00157 
00158  void ODJBH4Cycle::insertConfig()
00159   throw(std::runtime_error)
00160 {
00161   try {
00162 
00163     prepareWrite();
00164     writeDB();
00165     m_conn->commit();
00166     terminateWriteStatement();
00167   } catch (std::runtime_error &e) {
00168     m_conn->rollback();
00169     throw(e);
00170   } catch (...) {
00171     m_conn->rollback();
00172     throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00173   }
00174 }
00175