CMS 3D CMS Logo

IODConfig.h
Go to the documentation of this file.
1 #ifndef IODCONFIG_H
2 #define IODCONFIG_H
3 #include <stdexcept>
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 #include <cstdio>
8 #include <cstring>
9 
10 
12 
14 
19 class IODConfig : public IDBObject {
20 
21  public:
22  typedef oracle::occi::SQLException SQLException;
23  typedef oracle::occi::Statement Statement;
24  typedef oracle::occi::Stream Stream;
25  typedef oracle::occi::Clob Clob;
26 
28 
29  virtual std::string getTable() =0;
30 
31  inline void setConfigTag(std::string x) {m_config_tag=x;}
33 
34 
35  protected:
36  Statement* m_writeStmt;
37  Statement* m_readStmt;
38 
39  inline void checkPrepare() noexcept(false)
40  {
41  if (m_writeStmt == nullptr) {
42  throw(std::runtime_error("Write statement not prepared"));
43  }
44  }
45 
47  {
48  if (m_writeStmt != nullptr) {
49  m_conn->terminateStatement(m_writeStmt);
50  } else {
51  std::cout << "Warning from IDataItem: statement was aleady closed"<< std::endl;
52  }
53  }
54 
55 
57  {
58  m_readStmt=m_conn->createStatement();
59  }
60 
61  inline void setPrefetchRowCount(int ncount) noexcept(false)
62  {
63  m_readStmt->setPrefetchRowCount(ncount);
64  }
65 
67  {
68  if (m_readStmt != nullptr) {
69  m_conn->terminateStatement(m_readStmt);
70  } else {
71  std::cout << "Warning from IDataItem: statement was aleady closed"<< std::endl;
72  }
73  }
74 
75 
76 
77  // Prepare a statement for writing operations
78  virtual void prepareWrite() noexcept(false) = 0;
79 
80  // virtual void writeDB() noexcept(false) ;
81 
82 
83 void populateClob (Clob &clob, std::string fname, unsigned int bufsize) noexcept(false)
84 {
85 
86  try{
87  // Uses stream here
88  std::cout << "Populating the Clob using writeBuffer(Stream) method" << std::endl;
89  std::cout<<"we are here0"<<std::endl;
90 
91  const char *file = fname.c_str();
92  std::cout<<"we are here0.5 file is:"<<fname<<std::endl;
93 
94  std::ifstream inFile;
95  inFile.open(file,std::ios::in);
96  if (!inFile)
97  {
98  std::cout << fname <<" file not found\n";
99  inFile.close();
100 
101  std::string fname2="/nfshome0/ecaldev/francesca/null_file.txt";
102  inFile.open(fname2.c_str(),std::ios::in);
103 
104 
105 
106  }
107  if(bufsize==0){
108 
109 
110  inFile.seekg( 0,std::ios::end );
111  bufsize = inFile.tellg();
112  std::cout <<" bufsize ="<<bufsize<< std::endl;
113  // set file pointer to start again
114  inFile.seekg( 0,std::ios::beg );
115 
116  }
117 
118  char *buffer = new char[bufsize + 1];
119 
120 
121  std::cout<<"we are here1"<<std::endl;
122  unsigned int size;
123  Stream *strm=clob.getStream();
124  std::cout<<"we are here2"<<std::endl;
125  // while(inFile)
126  // {
127  int buf=0;
128  memset (buffer, buf, bufsize + 1);
129  inFile.read(buffer,bufsize);
130  std::cout<<"we are here2.5"<<std::endl;
131 
132  strm->writeBuffer(buffer,strlen(buffer));
133  std::cout<<"we are here2.6"<<std::endl;
134 
135  //}
136  std::cout<<"we are here3"<<std::endl;
137  strcpy(buffer," ");
138  size=strlen(buffer);
139  strm->writeLastBuffer(buffer,size);
140  clob.closeStream(strm);
141  inFile.close();
142  std::cout<<"we are here4"<<std::endl;
143  delete[] buffer;
144 
145 
146  }catch (SQLException &e) {
147  throw(std::runtime_error(std::string("populateClob(): ")+e.getMessage()));
148  }
149 
150  std::cout << "Populating the Clob - Success" << std::endl;
151 }
152 
153 
154 unsigned char* readClob (Clob &clob, int size) noexcept(false)
155 {
156 
157  try{
158  Stream *instream = clob.getStream (1,0);
159  unsigned char *buffer= new unsigned char[size];
160  int buf=0;
161  memset (buffer, buf, size);
162 
163  instream->readBuffer ((char*)buffer, size);
164  std::cout << "remember to delete the char* at the end of the program ";
165  for (int i = 0; i < size; ++i)
166  std::cout << (char) buffer[i];
167  std::cout << std::endl;
168 
169 
170  clob.closeStream (instream);
171 
172  return buffer;
173 
174  }catch (SQLException &e) {
175  throw(std::runtime_error(std::string("readClob(): ")+e.getMessage()));
176  }
177 
178 }
179 
180 
181 
182 };
183 
184 #endif
185 
186 
size
Write out results.
oracle::occi::Connection * m_conn
Definition: IDBObject.h:39
Statement * m_writeStmt
Definition: IODConfig.h:36
oracle::occi::Stream Stream
Definition: IODConfig.h:24
unsigned char * readClob(Clob &clob, int size) noexcept(false)
Definition: IODConfig.h:154
void createReadStatement() noexcept(false)
Definition: IODConfig.h:56
virtual std::string getTable()=0
void terminateWriteStatement() noexcept(false)
Definition: IODConfig.h:46
void terminateReadStatement() noexcept(false)
Definition: IODConfig.h:66
std::string m_config_tag
Definition: IODConfig.h:27
void populateClob(Clob &clob, std::string fname, unsigned int bufsize) noexcept(false)
Definition: IODConfig.h:83
#define end
Definition: vmac.h:39
void setPrefetchRowCount(int ncount) noexcept(false)
Definition: IODConfig.h:61
#define noexcept
oracle::occi::Statement Statement
Definition: IODConfig.h:23
oracle::occi::Clob Clob
Definition: IODConfig.h:25
string fname
main script
void setConfigTag(std::string x)
Definition: IODConfig.h:31
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
Statement * m_readStmt
Definition: IODConfig.h:37
std::string getConfigTag()
Definition: IODConfig.h:32
void checkPrepare() noexcept(false)
Definition: IODConfig.h:39
virtual void prepareWrite() noexcept(false)=0