CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/OnlineDB/EcalCondDB/interface/IODConfig.h

Go to the documentation of this file.
00001 #ifndef IODCONFIG_H
00002 #define IODCONFIG_H
00003 #include <stdexcept>
00004 #include <string>
00005 #include <fstream>
00006 #include <iostream>
00007 #include <stdio.h>
00008 #include <cstring>
00009 
00010 
00011 #include "OnlineDB/Oracle/interface/Oracle.h"
00012 
00013 #include "OnlineDB/EcalCondDB/interface/IDBObject.h"
00014 
00019 class IODConfig : public IDBObject {
00020 
00021  public:
00022   typedef oracle::occi::SQLException SQLException;
00023   typedef oracle::occi::Statement Statement;
00024   typedef oracle::occi::Stream Stream;
00025   typedef oracle::occi::Clob Clob;
00026 
00027   std::string   m_config_tag;
00028 
00029   virtual std::string getTable() =0;
00030 
00031   inline void setConfigTag(std::string x) {m_config_tag=x;}
00032   inline std::string getConfigTag() {return m_config_tag;}
00033  
00034 
00035  protected:
00036   Statement* m_writeStmt;
00037   Statement* m_readStmt;
00038 
00039   inline void checkPrepare() 
00040     throw(std::runtime_error) 
00041     {
00042       if (m_writeStmt == NULL) {
00043         throw(std::runtime_error("Write statement not prepared"));
00044       }
00045     }
00046 
00047   inline void terminateWriteStatement()
00048     throw(std::runtime_error)
00049   {
00050     if (m_writeStmt != NULL) {
00051       m_conn->terminateStatement(m_writeStmt);
00052     } else {
00053       std::cout << "Warning from IDataItem: statement was aleady closed"<< std::endl;
00054     }
00055   }
00056 
00057 
00058   inline void createReadStatement()
00059     throw(std::runtime_error)
00060   {
00061       m_readStmt=m_conn->createStatement();
00062   }
00063 
00064   inline void setPrefetchRowCount(int ncount)
00065     throw(std::runtime_error)
00066   {
00067     m_readStmt->setPrefetchRowCount(ncount);
00068   }
00069 
00070   inline void terminateReadStatement()
00071     throw(std::runtime_error)
00072   {
00073     if (m_readStmt != NULL) {
00074       m_conn->terminateStatement(m_readStmt);
00075     } else {
00076       std::cout << "Warning from IDataItem: statement was aleady closed"<< std::endl;
00077     }
00078   }
00079 
00080 
00081 
00082   // Prepare a statement for writing operations
00083   virtual void prepareWrite() throw(std::runtime_error) =0;
00084 
00085   //  virtual void writeDB() throw(std::runtime_error) ;
00086 
00087 
00088 void populateClob (Clob &clob, std::string fname, unsigned int bufsize)
00089  throw (std::runtime_error)
00090 {
00091 
00092   try{
00093       // Uses stream here
00094       std::cout << "Populating the Clob using writeBuffer(Stream) method" << std::endl;
00095       std::cout<<"we are here0"<<std::endl; 
00096 
00097       char *file = (char *)fname.c_str();
00098       std::cout<<"we are here0.5 file is:"<<fname<<std::endl; 
00099 
00100       std::ifstream inFile;
00101       inFile.open(file,std::ios::in);
00102       if (!inFile)
00103         {
00104           std::cout << fname <<" file not found\n";
00105           inFile.close();
00106 
00107           std::string fname2="/nfshome0/ecaldev/francesca/null_file.txt";
00108           inFile.open((char*)fname2.c_str(),std::ios::in);
00109           
00110 
00111           
00112         }
00113       if(bufsize==0){
00114 
00115 
00116         inFile.seekg( 0,std::ios::end ); 
00117         bufsize = inFile.tellg(); 
00118         std::cout <<" bufsize ="<<bufsize<< std::endl;
00119         // set file pointer to start again 
00120         inFile.seekg( 0,std::ios::beg ); 
00121         
00122       }
00123 
00124       char *buffer = new char[bufsize + 1];
00125 
00126 
00127       std::cout<<"we are here1"<<std::endl; 
00128       unsigned int size;
00129       Stream *strm=clob.getStream();
00130       std::cout<<"we are here2"<<std::endl; 
00131       //    while(inFile)
00132       //        {
00133       int buf=0;
00134       memset (buffer, buf, bufsize + 1);
00135       inFile.read(buffer,bufsize);
00136       std::cout<<"we are here2.5"<<std::endl; 
00137       
00138       strm->writeBuffer(buffer,strlen(buffer));
00139       std::cout<<"we are here2.6"<<std::endl; 
00140 
00141       //}
00142       std::cout<<"we are here3"<<std::endl; 
00143       strcpy(buffer," ");
00144       size=strlen(buffer);
00145       strm->writeLastBuffer(buffer,size);
00146       clob.closeStream(strm);
00147       inFile.close();
00148       std::cout<<"we are here4"<<std::endl; 
00149       delete[] buffer;
00150 
00151 
00152   }catch (SQLException &e) {
00153     throw(std::runtime_error("populateClob():  "+e.getMessage()));
00154   }
00155 
00156   std::cout << "Populating the Clob - Success" << std::endl;
00157 }
00158 
00159 
00160 unsigned char* readClob (Clob &clob, int size)
00161   throw (std::runtime_error)
00162 {
00163 
00164   try{
00165     Stream *instream = clob.getStream (1,0);
00166     unsigned char *buffer= new unsigned char[size]; 
00167     int buf=0;
00168     memset (buffer, buf, size);
00169     
00170     instream->readBuffer ((char*)buffer, size);
00171     std::cout << "remember to delete the char* at the end of the program ";
00172        for (int i = 0; i < size; ++i)
00173        std::cout << (char) buffer[i];
00174      std::cout << std::endl;
00175     
00176 
00177     clob.closeStream (instream);
00178 
00179     return  buffer;
00180 
00181   }catch (SQLException &e) {
00182     throw(std::runtime_error("readClob():  "+e.getMessage()));
00183   }
00184 
00185 }
00186 
00187 
00188 
00189 };
00190 
00191 #endif
00192 
00193