CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/OnlineDB/EcalCondDB/src/ODRunConfigCycleInfo.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigCycleInfo.h"
00005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 
00012 ODRunConfigCycleInfo::ODRunConfigCycleInfo()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018 
00019    m_ID=0;
00020 
00021   m_sequence_id =0;
00022   m_cycle_num =0;
00023   m_tag = "";
00024   m_description="";
00025 }
00026 
00027 
00028 
00029 ODRunConfigCycleInfo::~ODRunConfigCycleInfo(){}
00030 
00031 
00032 void ODRunConfigCycleInfo::clear() {
00033   m_sequence_id =0;
00034   m_cycle_num =0;
00035   m_tag = "";
00036   m_description="";
00037 
00038 }
00039 
00040 void ODRunConfigCycleInfo::prepareWrite()
00041   throw(std::runtime_error)
00042 {
00043   this->checkConnection();
00044 
00045   try {
00046     m_writeStmt = m_conn->createStatement();
00047     m_writeStmt->setSQL("INSERT INTO ECAL_CYCLE_DAT ( sequence_id , cycle_num, tag, description ) "
00048      "VALUES (:1, :2, :3 , :4 )");
00049 
00050   } catch (SQLException &e) {
00051     throw(std::runtime_error("ODRunConfigCycleInfo::prepareWrite():  "+e.getMessage()));
00052   }
00053 }
00054 
00055 
00056 void ODRunConfigCycleInfo::writeDB()
00057   throw(std::runtime_error)
00058 {
00059   this->checkConnection();
00060   this->checkPrepare();
00061 
00062   // Validate the data, use infinity-till convention
00063   DateHandler dh(m_env, m_conn);
00064 
00065   try {
00066 
00067     m_writeStmt->setInt(1, this->getSequenceID());
00068     m_writeStmt->setInt(2, this->getCycleNumber());
00069     m_writeStmt->setString(3, this->getTag());
00070     m_writeStmt->setString(4, this->getDescription());
00071     m_writeStmt->executeUpdate();
00072 
00073 
00074 
00075   } catch (SQLException &e) {
00076     throw(std::runtime_error("ODRunConfigCycleInfo::writeDB:  "+e.getMessage()));
00077   }
00078   // Now get the ID
00079   if (!this->fetchID()) {
00080     throw(std::runtime_error("ODRunConfigCycleInfo::writeDB:  Failed to write"));
00081   }
00082 
00083   cout<< "ODRunConfigCycleInfo::writeDB>> done inserting ODRunConfigCycleInfo with id="<<m_ID<<endl;
00084 
00085 }
00086 
00087 
00088 
00089 
00090 
00091 
00092 int ODRunConfigCycleInfo::fetchID() 
00093   throw(std::runtime_error)
00094 {
00095   // Return from memory if available
00096   if (m_ID>0) {
00097     return m_ID;
00098   }
00099 
00100   this->checkConnection();
00101 
00102 
00103   DateHandler dh(m_env, m_conn);
00104 
00105   try {
00106     Statement* stmt = m_conn->createStatement();
00107     stmt->setSQL("SELECT cycle_id from ECAL_cycle_DAT "
00108                  "WHERE sequence_id = :id1 " 
00109                  " and cycle_num = :id2  " );
00110     stmt->setInt(1, m_sequence_id);
00111     stmt->setInt(2, m_cycle_num);
00112 
00113     ResultSet* rset = stmt->executeQuery();
00114 
00115     if (rset->next()) {
00116       m_ID = rset->getInt(1);
00117     } else {
00118       m_ID = 0;
00119     }
00120     m_conn->terminateStatement(stmt);
00121   } catch (SQLException &e) {
00122     throw(std::runtime_error("ODRunConfigCycleInfo::fetchID:  "+e.getMessage()));
00123   }
00124   setByID(m_ID);
00125 
00126   return m_ID;
00127 }
00128 
00129 
00130 
00131 int ODRunConfigCycleInfo::fetchIDLast()
00132   throw(std::runtime_error)
00133 {
00134 
00135   this->checkConnection();
00136 
00137   DateHandler dh(m_env, m_conn);
00138 
00139   try {
00140     Statement* stmt = m_conn->createStatement();
00141     stmt->setSQL("SELECT max(cycle_id) FROM ecal_cycle_dat "    );
00142     ResultSet* rset = stmt->executeQuery();
00143 
00144     if (rset->next()) {
00145       m_ID = rset->getInt(1);
00146     } else {
00147       m_ID = 0;
00148     }
00149     m_conn->terminateStatement(stmt);
00150   } catch (SQLException &e) {
00151     throw(std::runtime_error("ODRunConfigCycleInfo::fetchIDLast:  "+e.getMessage()));
00152   }
00153 
00154   setByID(m_ID);
00155   return m_ID;
00156 }
00157 
00158 
00159 void ODRunConfigCycleInfo::setByID(int id) 
00160   throw(std::runtime_error)
00161 {
00162    this->checkConnection();
00163 
00164    DateHandler dh(m_env, m_conn);
00165 
00166    cout<< "ODRunConfigCycleInfo::setByID called for id "<<id<<endl;
00167 
00168    try {
00169      Statement* stmt = m_conn->createStatement();
00170 
00171      stmt->setSQL("SELECT sequence_id , cycle_num , tag , description FROM ECAL_cycle_DAT WHERE cycle_id = :1 ");
00172      stmt->setInt(1, id);
00173      
00174      ResultSet* rset = stmt->executeQuery();
00175      if (rset->next()) {
00176        m_sequence_id=rset->getInt(1);
00177        m_cycle_num=rset->getInt(2);
00178        m_tag = rset->getString(3);
00179        m_description= rset->getString(4);
00180        m_ID = id;
00181      } else {
00182        throw(std::runtime_error("ODRunConfigCycleInfo::setByID:  Given cycle_id is not in the database"));
00183      }
00184      m_conn->terminateStatement(stmt);
00185    } catch (SQLException &e) {
00186      throw(std::runtime_error("ODRunConfigCycleInfo::setByID:  "+e.getMessage()));
00187    }
00188 }
00189 
00190 
00191 void ODRunConfigCycleInfo::fetchData(ODRunConfigCycleInfo * result)
00192   throw(std::runtime_error)
00193 {
00194   this->checkConnection();
00195   result->clear();
00196   if(result->getId()==0){
00197     throw(std::runtime_error("ODRunConfigCycleInfo::fetchData(): no Id defined for this ODRunConfigCycleInfo "));
00198   }
00199 
00200   try {
00201     m_readStmt->setSQL("SELECT sequence_id , cycle_num , tag , description FROM ECAL_cycle_DAT WHERE cycle_id = :1 ");
00202 
00203     m_readStmt->setInt(1, result->getId());
00204     ResultSet* rset = m_readStmt->executeQuery();
00205 
00206     rset->next();
00207 
00208     result->setSequenceID(       rset->getInt(1) );
00209     result->setCycleNumber(      rset->getInt(2) );
00210     result->setTag(              rset->getString(3) );
00211     result->setDescription(      rset->getString(4) );
00212  
00213   } catch (SQLException &e) {
00214     throw(std::runtime_error("ODRunConfigCycleInfo::fetchData():  "+e.getMessage()));
00215   }
00216 }
00217 
00218  void ODRunConfigCycleInfo::insertConfig()
00219   throw(std::runtime_error)
00220 {
00221   try {
00222 
00223     prepareWrite();
00224     writeDB();
00225     m_conn->commit();
00226     terminateWriteStatement();
00227   } catch (std::runtime_error &e) {
00228     m_conn->rollback();
00229     throw(e);
00230   } catch (...) {
00231     m_conn->rollback();
00232     throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00233   }
00234 }
00235