CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/OnlineDB/EcalCondDB/src/MODDCCOperationDat.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/MODDCCOperationDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/MODRunIOV.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 MODDCCOperationDat::MODDCCOperationDat()
00012 {
00013   m_env = NULL;
00014   m_conn = NULL;
00015   m_writeStmt = NULL;
00016   m_readStmt = NULL;
00017 
00018   m_word = "";
00019 }
00020 
00021 
00022 
00023 MODDCCOperationDat::~MODDCCOperationDat()
00024 {
00025 }
00026 
00027 
00028 
00029 void MODDCCOperationDat::prepareWrite()
00030   throw(std::runtime_error)
00031 {
00032   this->checkConnection();
00033 
00034   try {
00035     m_writeStmt = m_conn->createStatement();
00036     m_writeStmt->setSQL("INSERT INTO "+getTable()+" (iov_id, logic_id, "
00037                         "operationMode) "
00038                         "VALUES (:iov_id, :logic_id, "
00039                         ":1) ");
00040   } catch (SQLException &e) {
00041     throw(std::runtime_error("MODDCCOperationDat::prepareWrite():  "+e.getMessage()));
00042   }
00043 }
00044 
00045 
00046 
00047 void MODDCCOperationDat::writeDB(const EcalLogicID* ecid, const MODDCCOperationDat* item, MODRunIOV* iov )
00048   throw(std::runtime_error)
00049 {
00050   this->checkConnection();
00051   this->checkPrepare();
00052 
00053   int iovID = iov->fetchID();
00054   if (!iovID) { throw(std::runtime_error("MODDCCOperationDat::writeDB:  IOV not in DB")); }
00055 
00056   int logicID = ecid->getLogicID();
00057   if (!logicID) { throw(std::runtime_error("MODDCCOperationDat::writeDB:  Bad EcalLogicID")); }
00058   
00059   try {
00060     m_writeStmt->setInt(1, iovID);
00061     m_writeStmt->setInt(2, logicID);
00062     m_writeStmt->setString(3, item->getOperation() );
00063 
00064     m_writeStmt->executeUpdate();
00065   } catch (SQLException &e) {
00066     throw(std::runtime_error("MODDCCOperationDat::writeDB():  "+e.getMessage()));
00067   }
00068 }
00069 
00070 
00071 
00072 void MODDCCOperationDat::fetchData(std::map< EcalLogicID, MODDCCOperationDat >* fillMap, MODRunIOV* iov)
00073   throw(std::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     //  throw(std::runtime_error("MODDCCOperationDat::writeDB:  IOV not in DB")); 
00082     return;
00083   }
00084 
00085   try {
00086 
00087     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00088                  " d.operationMode " 
00089                  "FROM channelview cv JOIN "+getTable()+" d "
00090                  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00091                  "WHERE d.iov_id = :iov_id");
00092     m_readStmt->setInt(1, iovID);
00093     ResultSet* rset = m_readStmt->executeQuery();
00094     
00095     std::pair< EcalLogicID, MODDCCOperationDat > p;
00096     MODDCCOperationDat dat;
00097     while(rset->next()) {
00098       p.first = EcalLogicID( rset->getString(1),     // name
00099                              rset->getInt(2),        // logic_id
00100                              rset->getInt(3),        // id1
00101                              rset->getInt(4),        // id2
00102                              rset->getInt(5),        // id3
00103                              rset->getString(6));    // maps_to
00104 
00105       dat.setOperation( rset->getString(7) );
00106 
00107       p.second = dat;
00108       fillMap->insert(p);
00109     }
00110   } catch (SQLException &e) {
00111     throw(std::runtime_error("MODDCCOperationDat::fetchData():  "+e.getMessage()));
00112   }
00113 }
00114 
00115 void MODDCCOperationDat::writeArrayDB(const std::map< EcalLogicID, MODDCCOperationDat >* data, MODRunIOV* iov)
00116   throw(std::runtime_error)
00117 {
00118   this->checkConnection();
00119   this->checkPrepare();
00120 
00121   int iovID = iov->fetchID();
00122   if (!iovID) { throw(std::runtime_error("MODDCCOperationDat::writeArrayDB:  IOV not in DB")); }
00123 
00124 
00125   int nrows=data->size(); 
00126   int* ids= new int[nrows];
00127   int* iovid_vec= new int[nrows];
00128   std::string* xx= new std::string[nrows];
00129 
00130   ub2* ids_len= new ub2[nrows];
00131   ub2* iov_len= new ub2[nrows];
00132   ub2* x_len= new ub2[nrows];
00133 
00134   const EcalLogicID* channel;
00135   const MODDCCOperationDat* dataitem;
00136   int count=0;
00137   typedef map< EcalLogicID, MODDCCOperationDat >::const_iterator CI;
00138   for (CI p = data->begin(); p != data->end(); ++p) {
00139         channel = &(p->first);
00140         int logicID = channel->getLogicID();
00141         if (!logicID) { throw(std::runtime_error("MODDCCOperationDat::writeArrayDB:  Bad EcalLogicID")); }
00142         ids[count]=logicID;
00143         iovid_vec[count]=iovID;
00144 
00145         dataitem = &(p->second);
00146         // dataIface.writeDB( channel, dataitem, iov);
00147         std::string x=dataitem->getOperation();
00148 
00149         xx[count]=x;
00150 
00151         ids_len[count]=sizeof(ids[count]);
00152         iov_len[count]=sizeof(iovid_vec[count]);
00153         
00154         x_len[count]=sizeof(xx[count]);
00155 
00156         count++;
00157      }
00158 
00159 
00160   try {
00161     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00162     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00163     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCISTRING , sizeof(xx[0]), x_len );
00164 
00165     m_writeStmt->executeArrayUpdate(nrows);
00166 
00167     delete [] ids;
00168     delete [] iovid_vec;
00169     delete [] xx;
00170 
00171 
00172     delete [] ids_len;
00173     delete [] iov_len;
00174     delete [] x_len;
00175 
00176 
00177 
00178   } catch (SQLException &e) {
00179     throw(std::runtime_error("MonPedestalsDat::writeArrayDB():  "+e.getMessage()));
00180   }
00181 }