CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/OnlineDB/EcalCondDB/src/ODWeightsSamplesDat.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/ODWeightsSamplesDat.h"
00006 
00007 using namespace std;
00008 using namespace oracle::occi;
00009 
00010 ODWeightsSamplesDat::ODWeightsSamplesDat()
00011 {
00012   m_env = NULL;
00013   m_conn = NULL;
00014   m_writeStmt = NULL;
00015   m_readStmt = NULL;
00016 
00017   m_fed = 0;
00018 
00019 }
00020 
00021 
00022 
00023 ODWeightsSamplesDat::~ODWeightsSamplesDat()
00024 {
00025 }
00026 
00027 void ODWeightsSamplesDat::clear() {
00028   m_fed = 0;
00029   m_ss = 0;
00030   m_sn = 0;
00031 }
00032 
00033 void ODWeightsSamplesDat::prepareWrite()
00034   throw(std::runtime_error)
00035 {
00036   this->checkConnection();
00037 
00038   try {
00039     m_writeStmt = m_conn->createStatement();
00040     m_writeStmt->setSQL("INSERT INTO "+getTable()+" (rec_id, fed_id, sample_id, weight_number ) "
00041                         "VALUES (:1, :2, :3, :4 )");
00042   } catch (SQLException &e) {
00043     throw(std::runtime_error("ODWeightsSamplesDat::prepareWrite():  "+e.getMessage()));
00044   }
00045 }
00046 
00047 
00048 
00049 void ODWeightsSamplesDat::writeDB(const ODWeightsSamplesDat* item, ODFEWeightsInfo* iov )
00050   throw(std::runtime_error)
00051 {
00052   this->checkConnection();
00053 
00054   try {
00055     m_writeStmt->setInt(1, item->getId());
00056     m_writeStmt->setInt(2, item->getFedId() );
00057     m_writeStmt->setInt(3, item->getSampleId() );
00058     m_writeStmt->setInt(4, item->getWeightNumber() );
00059     
00060     m_writeStmt->executeUpdate();
00061   } catch (SQLException &e) {
00062     throw(std::runtime_error("ODWeightsSamplesDat::writeDB():  "+e.getMessage()));
00063   }
00064 }
00065 
00066 
00067 
00068 void ODWeightsSamplesDat::fetchData(std::vector< ODWeightsSamplesDat >* p, ODFEWeightsInfo* iov)
00069   throw(std::runtime_error)
00070 {
00071   this->checkConnection();
00072 
00073   iov->setConnection(m_env, m_conn);
00074   int iovID = iov->fetchID();
00075   if (!iovID) { 
00076     std::cout <<"ID not in the DB"<< endl; 
00077     return;
00078   }
00079 
00080   try {
00081     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :rec_id order by fed_id, sample_id");
00082     m_readStmt->setInt(1, iovID);
00083     ResultSet* rset = m_readStmt->executeQuery();
00084     
00085     //    std::vector< ODWeightsSamplesDat > p;
00086     ODWeightsSamplesDat dat;
00087     while(rset->next()) {
00088       // dat.setId( rset->getInt(1) );
00089       dat.setFedId( rset->getInt(2) );
00090       dat.setSampleId( rset->getInt(3) );
00091       dat.setWeightNumber( rset->getInt(4) );
00092 
00093       p->push_back( dat);
00094 
00095     }
00096 
00097   } catch (SQLException &e) {
00098     throw(std::runtime_error("ODWeightsSamplesDat::fetchData():  "+e.getMessage()));
00099   }
00100 }
00101 
00102 
00103 void ODWeightsSamplesDat::fetchData(ODWeightsSamplesDat * p)
00104   throw(std::runtime_error)
00105 {
00106   this->checkConnection();
00107 
00108   try {
00109     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :1 order by fed_id, sample_id");
00110     m_readStmt->setInt(1, p->getId());
00111     ResultSet* rset = m_readStmt->executeQuery();
00112     
00113     while(rset->next()) {
00114       p->setFedId(        rset->getInt(2) );
00115       p->setSampleId(     rset->getInt(3) );
00116       p->setWeightNumber( rset->getInt(4) );
00117     }
00118 
00119   } catch (SQLException &e) {
00120     throw(std::runtime_error("ODWeightsSamplesDat::fetchData():  "+e.getMessage()));
00121   }
00122 }
00123 
00124 //  ************************************************************************   // 
00125 
00126 void ODWeightsSamplesDat::writeArrayDB(const std::vector< ODWeightsSamplesDat > data, ODFEWeightsInfo* iov)
00127     throw(std::runtime_error)
00128 {
00129   this->checkConnection();
00130 
00131   int iovID = iov->fetchID();
00132   if (!iovID) { throw(std::runtime_error("ODDelays::writeArrayDB:  ODFEDelaysInfo not in DB")); }
00133 
00134 
00135   int nrows=data.size(); 
00136   int* ids= new int[nrows];
00137   int* xx= new int[nrows];
00138   int* yy= new int[nrows];
00139   int* zz= new int[nrows];
00140 
00141   ub2* ids_len= new ub2[nrows];
00142   ub2* x_len= new ub2[nrows];
00143   ub2* y_len= new ub2[nrows];
00144   ub2* z_len= new ub2[nrows];
00145 
00146   ODWeightsSamplesDat dataitem;
00147   
00148   int n_data= (int) data.size();
00149   for (int count = 0; count <n_data ; count++) {
00150     dataitem=data[count];
00151     ids[count]=iovID;
00152     xx[count]=dataitem.getFedId();
00153     yy[count]=dataitem.getSampleId();
00154     zz[count]=dataitem.getWeightNumber();
00155 
00156 
00157         ids_len[count]=sizeof(ids[count]);
00158         x_len[count]=sizeof(xx[count]);
00159         y_len[count]=sizeof(yy[count]);
00160         z_len[count]=sizeof(zz[count]);
00161 
00162      }
00163 
00164 
00165   try {
00166     m_writeStmt->setDataBuffer(1, (dvoid*)ids, OCCIINT, sizeof(ids[0]),ids_len);
00167     m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00168     m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00169     m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT , sizeof(zz[0]), z_len );
00170    
00171 
00172     m_writeStmt->executeArrayUpdate(nrows);
00173 
00174     delete [] ids;
00175     delete [] xx;
00176     delete [] yy;
00177     delete [] zz;
00178 
00179     delete [] ids_len;
00180     delete [] x_len;
00181     delete [] y_len;
00182     delete [] z_len;
00183 
00184   } catch (SQLException &e) {
00185     throw(std::runtime_error("ODWeightsSamplesDat::writeArrayDB():  "+e.getMessage()));
00186   }
00187 }