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