CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/OnlineDB/EcalCondDB/src/MODCCSHFDat.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <fstream>
00004 #include <iostream>
00005 #include <stdio.h>
00006 #include <cstring>
00007 
00008 #include "OnlineDB/Oracle/interface/Oracle.h"
00009 
00010 #include "OnlineDB/EcalCondDB/interface/MODCCSHFDat.h"
00011 #include "OnlineDB/EcalCondDB/interface/MODRunIOV.h"
00012 
00013 
00014 using namespace std;
00015 using namespace oracle::occi;
00016 
00017 MODCCSHFDat::MODCCSHFDat()
00018 {
00019   m_env = NULL;
00020   m_conn = NULL;
00021   m_writeStmt = NULL;
00022   m_readStmt = NULL;
00023 
00024   //  m_clob = 0;
00025   m_size=0;
00026   m_file="";
00027 
00028 }
00029 
00030 
00031 
00032 MODCCSHFDat::~MODCCSHFDat()
00033 {
00034 }
00035 
00036 
00037 void MODCCSHFDat::setFile(std::string x) {
00038   m_file=x;
00039   //try {
00040   std::cout<< "file is "<< m_file<<endl;
00041   // }catch (Exception &e) {
00042   //throw(std::runtime_error("MODCCSHFDat::setFile():  "+e.getMessage()));
00043   //} 
00044     // here we must open the file and read the CCS Clob
00045     std::cout << "Going to read CCS file: " << m_file << endl;
00046     ifstream inpFile;
00047     inpFile.open(m_file.c_str());
00048     // tell me size of file
00049     int bufsize = 0;
00050     inpFile.seekg( 0,ios::end );
00051     bufsize = inpFile.tellg();
00052     std::cout <<" bufsize ="<<bufsize<< std::endl;
00053     // set file pointer to start again
00054     inpFile.seekg( 0,ios::beg );
00055     m_size=bufsize;
00056     inpFile.close();
00057     std::cout << "Going to read CCS file: " << m_file << endl;
00058 
00059 }
00060 
00061 void MODCCSHFDat::prepareWrite()
00062   throw(std::runtime_error)
00063 {
00064   this->checkConnection();
00065 
00066   try {
00067     m_writeStmt = m_conn->createStatement();
00068     m_writeStmt->setSQL("INSERT INTO OD_CCS_HF_dat (iov_id, logic_id, "
00069                         "ccs_log) "
00070                         "VALUES (:iov_id, :logic_id, "
00071                         ":ccs_log )");
00072 
00073 
00074   } catch (SQLException &e) {
00075     throw(std::runtime_error("MODCCSHFDat::prepareWrite():  "+e.getMessage()));
00076   }
00077 }
00078 
00079 
00080 
00081 void MODCCSHFDat::writeDB(const EcalLogicID* ecid, const MODCCSHFDat* item, MODRunIOV* iov )
00082   throw(std::runtime_error)
00083 {
00084 
00085 
00086 
00087   this->checkConnection();
00088   this->checkPrepare();
00089 
00090   int iovID = iov->fetchID();
00091   if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeDB:  IOV not in DB")); }
00092 
00093   int logicID = ecid->getLogicID();
00094   if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeDB:  Bad EcalLogicID")); }
00095   
00096   std::string fname=item->getFile();
00097   std::cout << "Going to read CCS file: " << fname << endl;
00098 
00099 
00100   try {
00101 
00102     m_writeStmt->setInt(1, iovID);
00103     m_writeStmt->setInt(2, logicID);
00104 
00105     // and now the clob
00106     oracle::occi::Clob clob(m_conn);
00107     clob.setEmpty();
00108     m_writeStmt->setClob(3,clob);
00109     m_writeStmt->executeUpdate ();
00110 
00111     m_conn->terminateStatement(m_writeStmt);
00112     std::cout<<"empty CCS Clob inserted into DB" <<std::endl;
00113 
00114     // now we read and update it
00115     m_writeStmt = m_conn->createStatement();
00116     m_writeStmt->setSQL ("SELECT CCS_LOG FROM OD_CCS_HF_DAT WHERE"
00117                          " iov_ID=:1 and logic_ID=:2 FOR UPDATE");
00118     m_writeStmt->setInt(1, iovID);
00119     m_writeStmt->setInt(2, logicID);
00120     ResultSet* rset = m_writeStmt->executeQuery();
00121     rset->next ();
00122     oracle::occi::Clob clob2 = rset->getClob (1);
00123     cout << "Opening the clob in read write mode" << endl;
00124 
00125     unsigned int clob_size=item->getSize();
00126 
00127     populateClob (clob2 , fname, clob_size);
00128 
00129     int clobLength=clob2.length ();
00130     cout << "Length of the clob is: " << clobLength << endl;
00131 
00132     m_writeStmt->executeUpdate();
00133     m_writeStmt->closeResultSet (rset);
00134 
00135   } catch (SQLException &e) {
00136     throw(std::runtime_error("MODCCSHFDat::writeDB():  "+e.getMessage()));
00137   }
00138 }
00139 
00140 
00141 
00142 void MODCCSHFDat::fetchData(std::map< EcalLogicID, MODCCSHFDat >* fillMap, MODRunIOV* iov)
00143   throw(std::runtime_error)
00144 {
00145   this->checkConnection();
00146   fillMap->clear();
00147 
00148   iov->setConnection(m_env, m_conn);
00149   int iovID = iov->fetchID();
00150   if (!iovID) { 
00151     //  throw(std::runtime_error("MODCCSHFDat::writeDB:  IOV not in DB")); 
00152     return;
00153   }
00154 
00155   try {
00156 
00157     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00158                  " d.ccs_log " 
00159                  "FROM channelview cv JOIN OD_CCS_HF_dat d "
00160                  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00161                  "WHERE d.iov_id = :iov_id");
00162     m_readStmt->setInt(1, iovID);
00163     ResultSet* rset = m_readStmt->executeQuery();
00164     
00165     std::pair< EcalLogicID, MODCCSHFDat > p;
00166     MODCCSHFDat dat;
00167     while(rset->next()) {
00168       p.first = EcalLogicID( rset->getString(1),     // name
00169                              rset->getInt(2),        // logic_id
00170                              rset->getInt(3),        // id1
00171                              rset->getInt(4),        // id2
00172                              rset->getInt(5),        // id3
00173                              rset->getString(6));    // maps_to
00174       // to be corrected 
00175       //      dat.setClob( rset->getString(7) );
00176 
00177       p.second = dat;
00178       fillMap->insert(p);
00179     }
00180   } catch (SQLException &e) {
00181     throw(std::runtime_error("MODCCSHFDat::fetchData():  "+e.getMessage()));
00182   }
00183 }
00184 
00185 void MODCCSHFDat::writeArrayDB(const std::map< EcalLogicID, MODCCSHFDat >* data, MODRunIOV* iov)
00186   throw(std::runtime_error)
00187 {
00188   this->checkConnection();
00189   this->checkPrepare();
00190 
00191   int iovID = iov->fetchID();
00192   if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB:  IOV not in DB")); }
00193 
00194 
00195   int nrows=data->size(); 
00196   int* ids= new int[nrows];
00197   int* iovid_vec= new int[nrows];
00198   int* xx= new int[nrows];
00199 
00200   ub2* ids_len= new ub2[nrows];
00201   ub2* iov_len= new ub2[nrows];
00202   ub2* x_len= new ub2[nrows];
00203 
00204   const EcalLogicID* channel;
00205   const MODCCSHFDat* dataitem;
00206   int count=0;
00207   typedef map< EcalLogicID, MODCCSHFDat >::const_iterator CI;
00208   for (CI p = data->begin(); p != data->end(); ++p) {
00209         channel = &(p->first);
00210         int logicID = channel->getLogicID();
00211         if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB:  Bad EcalLogicID")); }
00212         ids[count]=logicID;
00213         iovid_vec[count]=iovID;
00214 
00215         dataitem = &(p->second);
00216         // dataIface.writeDB( channel, dataitem, iov);
00217         // to be corrected 
00218 
00219         int x=0;
00220         //=dataitem->getWord();
00221 
00222         xx[count]=x;
00223 
00224         ids_len[count]=sizeof(ids[count]);
00225         iov_len[count]=sizeof(iovid_vec[count]);
00226         
00227         x_len[count]=sizeof(xx[count]);
00228 
00229         count++;
00230      }
00231 
00232 
00233   try {
00234     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00235     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00236     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00237 
00238     m_writeStmt->executeArrayUpdate(nrows);
00239 
00240     delete [] ids;
00241     delete [] iovid_vec;
00242     delete [] xx;
00243 
00244 
00245     delete [] ids_len;
00246     delete [] iov_len;
00247     delete [] x_len;
00248 
00249 
00250 
00251   } catch (SQLException &e) {
00252     throw(std::runtime_error("MonPedestalsDat::writeArrayDB():  "+e.getMessage()));
00253   }
00254 }
00255 
00256 
00257 
00258 void MODCCSHFDat::populateClob (Clob &clob, std::string fname, unsigned int clob_size )
00259  throw (std::runtime_error)
00260 {
00261 
00262   try{
00263       // Uses stream here
00264       cout << "Populating the Clob using writeBuffer(Stream) method" << endl;
00265       std::cout<<"we are here0"<<std::endl; 
00266 
00267       char *file = (char *)fname.c_str();
00268       std::cout<<"we are here0.5 file is:"<<fname<<std::endl; 
00269 
00270       ifstream inFile;
00271       inFile.open(file,ios::in);
00272       if (!inFile)
00273         {
00274           cout << fname<<" file not found\n";
00275           inFile.close();
00276 
00277           std::string fname2="/u1/fra/null_file.txt";
00278           inFile.open((char*)fname2.c_str(),ios::in);
00279           
00280 
00281           
00282         }
00283       if(clob_size==0){
00284 
00285 
00286         inFile.seekg( 0,ios::end ); 
00287         clob_size = inFile.tellg(); 
00288         std::cout <<" bufsize ="<<clob_size<< std::endl;
00289         // set file pointer to start again 
00290         inFile.seekg( 0,ios::beg ); 
00291         
00292       }
00293 
00294       char *buffer = new char[clob_size + 1];
00295 
00296 
00297       std::cout<<"we are here1"<<std::endl; 
00298       unsigned int size;
00299       Stream *strm=clob.getStream();
00300       std::cout<<"we are here2"<<std::endl; 
00301       //    while(inFile)
00302       //        {
00303       int buf=0;
00304       memset (buffer, buf, clob_size + 1);
00305       inFile.read(buffer,clob_size);
00306       std::cout<<"we are here2.5"<<std::endl; 
00307       
00308       strm->writeBuffer(buffer,strlen(buffer));
00309       std::cout<<"we are here2.6"<<std::endl; 
00310 
00311       //}
00312       std::cout<<"we are here3"<<std::endl; 
00313       strcpy(buffer," ");
00314       size=strlen(buffer);
00315       strm->writeLastBuffer(buffer,size);
00316       clob.closeStream(strm);
00317       inFile.close();
00318       std::cout<<"we are here4"<<std::endl; 
00319       delete[] buffer;
00320 
00321 
00322   }catch (SQLException &e) {
00323     throw(std::runtime_error("populateClob():  "+e.getMessage()));
00324   }
00325 
00326   cout << "Populating the Clob - Success" << endl;
00327 }
00328 
00329 
00330 unsigned char* MODCCSHFDat::readClob (oracle::occi::Clob &clob, int size)
00331   throw (std::runtime_error)
00332 {
00333 
00334   try{
00335     Stream *instream = clob.getStream (1,0);
00336     unsigned char *buffer= new unsigned char[size]; 
00337     int buf=0;
00338     memset (buffer, buf, size);
00339     
00340     instream->readBuffer ((char*)buffer, size);
00341     cout << "remember to delete the char* at the end of the program ";
00342        for (int i = 0; i < size; ++i)
00343        cout << (char) buffer[i];
00344      cout << endl;
00345     
00346 
00347     clob.closeStream (instream);
00348 
00349     return  buffer;
00350 
00351   }catch (SQLException &e) {
00352     throw(std::runtime_error("readClob():  "+e.getMessage()));
00353   }
00354 
00355 }