File indexing completed on 2024-04-06 12:22:54
0001 #ifndef IODCONFIG_H
0002 #define IODCONFIG_H
0003 #include <stdexcept>
0004 #include <string>
0005 #include <fstream>
0006 #include <iostream>
0007 #include <cstdio>
0008 #include <cstring>
0009
0010 #include "OnlineDB/Oracle/interface/Oracle.h"
0011
0012 #include "OnlineDB/EcalCondDB/interface/IDBObject.h"
0013
0014
0015
0016
0017
0018 class IODConfig : public IDBObject {
0019 public:
0020 typedef oracle::occi::SQLException SQLException;
0021 typedef oracle::occi::Statement Statement;
0022 typedef oracle::occi::Stream Stream;
0023 typedef oracle::occi::Clob Clob;
0024
0025 std::string m_config_tag;
0026
0027 virtual std::string getTable() = 0;
0028
0029 inline void setConfigTag(std::string x) { m_config_tag = x; }
0030 inline std::string getConfigTag() { return m_config_tag; }
0031
0032 protected:
0033 Statement *m_writeStmt;
0034 Statement *m_readStmt;
0035
0036 inline void checkPrepare() noexcept(false) {
0037 if (m_writeStmt == nullptr) {
0038 throw(std::runtime_error("Write statement not prepared"));
0039 }
0040 }
0041
0042 inline void terminateWriteStatement() noexcept(false) {
0043 if (m_writeStmt != nullptr) {
0044 m_conn->terminateStatement(m_writeStmt);
0045 } else {
0046 std::cout << "Warning from IDataItem: statement was aleady closed" << std::endl;
0047 }
0048 }
0049
0050 inline void createReadStatement() noexcept(false) { m_readStmt = m_conn->createStatement(); }
0051
0052 inline void setPrefetchRowCount(int ncount) noexcept(false) { m_readStmt->setPrefetchRowCount(ncount); }
0053
0054 inline void terminateReadStatement() noexcept(false) {
0055 if (m_readStmt != nullptr) {
0056 m_conn->terminateStatement(m_readStmt);
0057 } else {
0058 std::cout << "Warning from IDataItem: statement was aleady closed" << std::endl;
0059 }
0060 }
0061
0062
0063 virtual void prepareWrite() noexcept(false) = 0;
0064
0065
0066
0067 void populateClob(Clob &clob, std::string fname, unsigned int bufsize) noexcept(false) {
0068 try {
0069
0070 std::cout << "Populating the Clob using writeBuffer(Stream) method" << std::endl;
0071 std::cout << "we are here0" << std::endl;
0072
0073 const char *file = fname.c_str();
0074 std::cout << "we are here0.5 file is:" << fname << std::endl;
0075
0076 std::ifstream inFile;
0077 inFile.open(file, std::ios::in);
0078 if (!inFile) {
0079 std::cout << fname << " file not found\n";
0080 inFile.close();
0081
0082 std::string fname2 = "/nfshome0/ecaldev/francesca/null_file.txt";
0083 inFile.open(fname2.c_str(), std::ios::in);
0084 }
0085 if (bufsize == 0) {
0086 inFile.seekg(0, std::ios::end);
0087 bufsize = inFile.tellg();
0088 std::cout << " bufsize =" << bufsize << std::endl;
0089
0090 inFile.seekg(0, std::ios::beg);
0091 }
0092
0093 char *buffer = new char[bufsize + 1];
0094
0095 std::cout << "we are here1" << std::endl;
0096 unsigned int size;
0097 Stream *strm = clob.getStream();
0098 std::cout << "we are here2" << std::endl;
0099
0100
0101 int buf = 0;
0102 memset(buffer, buf, bufsize + 1);
0103 inFile.read(buffer, bufsize);
0104 std::cout << "we are here2.5" << std::endl;
0105
0106 strm->writeBuffer(buffer, strlen(buffer));
0107 std::cout << "we are here2.6" << std::endl;
0108
0109
0110 std::cout << "we are here3" << std::endl;
0111 strcpy(buffer, " ");
0112 size = strlen(buffer);
0113 strm->writeLastBuffer(buffer, size);
0114 clob.closeStream(strm);
0115 inFile.close();
0116 std::cout << "we are here4" << std::endl;
0117 delete[] buffer;
0118 } catch (SQLException &e) {
0119 throw(std::runtime_error(std::string("populateClob(): ") + e.getMessage()));
0120 }
0121 std::cout << "Populating the Clob - Success" << std::endl;
0122 }
0123
0124 unsigned char *readClob(Clob &clob, int size) noexcept(false) {
0125 try {
0126 Stream *instream = clob.getStream(1, 0);
0127 unsigned char *buffer = new unsigned char[size];
0128 int buf = 0;
0129 memset(buffer, buf, size);
0130
0131 instream->readBuffer((char *)buffer, size);
0132 std::cout << "remember to delete the char* at the end of the program ";
0133 for (int i = 0; i < size; ++i)
0134 std::cout << (char)buffer[i];
0135 std::cout << std::endl;
0136
0137 clob.closeStream(instream);
0138
0139 return buffer;
0140
0141 } catch (SQLException &e) {
0142 throw(std::runtime_error(std::string("readClob(): ") + e.getMessage()));
0143 }
0144 }
0145 };
0146
0147 #endif