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