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