CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/OnlineDB/EcalCondDB/src/RunPNErrorsDat.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004 #include <boost/lexical_cast.hpp>
00005 
00006 #include "OnlineDB/EcalCondDB/interface/RunPNErrorsDat.h"
00007 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 RunPNErrorsDat::RunPNErrorsDat()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018 
00019   m_errorBits = 0;
00020 }
00021 
00022 
00023 
00024 RunPNErrorsDat::~RunPNErrorsDat()
00025 {
00026 }
00027 
00028 
00029 
00030 void RunPNErrorsDat::prepareWrite()
00031   throw(std::runtime_error)
00032 {
00033   this->checkConnection();
00034 
00035   try {
00036     m_writeStmt = m_conn->createStatement();
00037     /* Using TO_NUMBER because OCCI does not support 64-bit integers well */
00038     m_writeStmt->setSQL("INSERT INTO run_pn_errors_dat (iov_id, logic_id, "
00039                         "error_bits) "
00040                         "VALUES (:iov_id, :logic_id, "
00041                         "to_number(:error_bits))");
00042   } catch (SQLException &e) {
00043     throw(std::runtime_error("RunPNErrorsDat::prepareWrite():  "+e.getMessage()));
00044   }
00045 }
00046 
00047 
00048 
00049 void RunPNErrorsDat::writeDB(const EcalLogicID* ecid, const RunPNErrorsDat* item, RunIOV* iov)
00050   throw(std::runtime_error)
00051 {
00052   this->checkConnection();
00053   this->checkPrepare();
00054 
00055   int iovID = iov->fetchID();
00056   if (!iovID) { throw(std::runtime_error("RunPNErrorsDat::writeDB:  IOV not in DB")); }
00057 
00058   int logicID = ecid->getLogicID();
00059   if (!logicID) { throw(std::runtime_error("RunPNErrorsDat::writeDB:  Bad EcalLogicID")); }
00060   
00061   try {
00062     m_writeStmt->setInt(1, iovID);
00063     m_writeStmt->setInt(2, logicID);
00064     m_writeStmt->setString(3, ( boost::lexical_cast<std::string>(item->getErrorBits()) ).c_str());
00065     m_writeStmt->executeUpdate();
00066   } catch (SQLException &e) {
00067     throw(std::runtime_error("RunPNErrorsDat::writeDB():  "+e.getMessage()));
00068   }
00069 }
00070 
00071 
00072 
00073 void RunPNErrorsDat::fetchData(map< EcalLogicID, RunPNErrorsDat >* fillMap, RunIOV* iov)
00074   throw(std::runtime_error)
00075 {
00076   this->checkConnection();
00077   fillMap->clear();
00078 
00079   iov->setConnection(m_env, m_conn);
00080   int iovID = iov->fetchID();
00081   if (!iovID) { 
00082     //  throw(std::runtime_error("RunPNErrorsDat::writeDB:  IOV not in DB")); 
00083     return;
00084   }
00085 
00086   try {
00087 
00088     /* Using TO_CHAR because OCCI does not support 64-bit integers well */
00089     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00090                  "to_char(d.error_bits) "
00091                  "FROM channelview cv JOIN run_pn_errors_dat d "
00092                  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00093                  "WHERE d.iov_id = :iov_id");
00094     m_readStmt->setInt(1, iovID);
00095     ResultSet* rset = m_readStmt->executeQuery();
00096     
00097     std::pair< EcalLogicID, RunPNErrorsDat > p;
00098     RunPNErrorsDat dat;
00099     while(rset->next()) {
00100       p.first = EcalLogicID( rset->getString(1),     // name
00101                              rset->getInt(2),        // logic_id
00102                              rset->getInt(3),        // id1
00103                              rset->getInt(4),        // id2
00104                              rset->getInt(5),        // id3
00105                              rset->getString(6));    // maps_to
00106 
00107       dat.setErrorBits( boost::lexical_cast<uint64_t>(rset->getString(7)) );
00108 
00109       p.second = dat;
00110       fillMap->insert(p);
00111     }
00112 
00113 
00114   } catch (SQLException &e) {
00115     throw(std::runtime_error("RunPNErrorsDat::fetchData():  "+e.getMessage()));
00116   }
00117 }