CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/OnlineDB/EcalCondDB/src/RunDat.cc

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