forked from bersler/OpenLogReplicator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseStatement.cpp
More file actions
229 lines (194 loc) · 9.37 KB
/
Copy pathDatabaseStatement.cpp
File metadata and controls
229 lines (194 loc) · 9.37 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Class to handle SQL statement
Copyright (C) 2018-2024 Adam Leszczynski ([email protected])
This file is part of OpenLogReplicator.
OpenLogReplicator is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your option)
any later version.
OpenLogReplicator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenLogReplicator; see the file LICENSE; If not see
<http://www.gnu.org/licenses/>. */
#include <cstring>
#include "DatabaseConnection.h"
#include "DatabaseEnvironment.h"
#include "DatabaseStatement.h"
namespace OpenLogReplicator {
DatabaseStatement::DatabaseStatement(DatabaseConnection* newConn) :
conn(newConn),
executed(false),
stmthp(nullptr) {
conn->env->checkErr(conn->errhp, OCIHandleAlloc(conn->env->envhp, reinterpret_cast<dvoid**>(&stmthp), OCI_HTYPE_STMT,
0, nullptr));
}
DatabaseStatement::~DatabaseStatement() {
unbindAll();
if (executed) {
OCIStmtRelease(stmthp, conn->errhp, nullptr, 0, OCI_DEFAULT);
executed = false;
}
if (stmthp != nullptr) {
OCIHandleFree(stmthp, OCI_HTYPE_STMT);
stmthp = nullptr;
}
}
void DatabaseStatement::createStatement(const char* sql) {
unbindAll();
if (executed) {
OCIStmtRelease(stmthp, conn->errhp, nullptr, 0, OCI_DEFAULT);
executed = false;
}
conn->env->checkErr(conn->errhp, OCIStmtPrepare2(conn->svchp, &stmthp, conn->errhp, reinterpret_cast<const OraText*>(sql),
strlen(sql), nullptr, 0, OCI_NTV_SYNTAX, OCI_DEFAULT));
}
int64_t DatabaseStatement::executeQuery() {
sword status = OCIStmtExecute(conn->svchp, stmthp, conn->errhp, 1, 0, nullptr, nullptr,
OCI_DEFAULT); // COMMIT_ON_SUCCESS
executed = true;
if (status == OCI_NO_DATA)
return 0;
conn->env->checkErr(conn->errhp, status);
return 1;
}
void DatabaseStatement::unbindAll() {
for (OCIBind* bindp: binds)
OCIHandleFree(reinterpret_cast<dvoid*>(bindp), OCI_HTYPE_BIND);
binds.clear();
for (OCIDefine* defp: defines)
OCIHandleFree(reinterpret_cast<dvoid*>(defp), OCI_HTYPE_DEFINE);
defines.clear();
}
int64_t DatabaseStatement::next() {
sword status = OCIStmtFetch2(stmthp, conn->errhp, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT);
if (status == OCI_NO_DATA)
return 0;
conn->env->checkErr(conn->errhp, status);
return 1;
}
void DatabaseStatement::bindString(uint64_t col, const char* val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, const_cast<void*>(reinterpret_cast<const void*>(val)),
strlen(val) + 1, SQLT_STR, nullptr, nullptr, nullptr, 0, nullptr,
OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::bindString(uint64_t col, std::string& val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col,
const_cast<void*>(reinterpret_cast<const void*>(val.c_str())), val.length() + 1,
SQLT_STR, nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
[[maybe_unused]] void DatabaseStatement::bindInt32(uint64_t col, int32_t& val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, reinterpret_cast<void*>(&val), sizeof(val),
SQLT_INT, nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::bindUInt32(uint64_t col, uint32_t& val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, reinterpret_cast<void*>(&val), sizeof(val),
SQLT_UIN, nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::bindInt64(uint64_t col, int64_t& val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, reinterpret_cast<void*>(&val), sizeof(val),
SQLT_INT, nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::bindUInt64(uint64_t col, uint64_t& val) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, reinterpret_cast<void*>(&val), sizeof(val),
SQLT_UIN, nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::bindBinary(uint64_t col, uint8_t* buf, uint64_t size) {
OCIBind* bindp = nullptr;
sword ret = OCIBindByPos(stmthp, &bindp, conn->errhp, col, reinterpret_cast<void*>(buf), size, SQLT_BIN,
nullptr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT);
if (bindp != nullptr)
binds.push_back(bindp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineString(uint64_t col, char* val, uint64_t len) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, val, len, SQLT_STR, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineUInt16(uint64_t col, uint16_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_UIN, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineInt16(uint64_t col, int16_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_INT, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineUInt32(uint64_t col, uint32_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_UIN, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineInt32(uint64_t col, int32_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_INT, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineUInt64(uint64_t col, uint64_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_UIN, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
void DatabaseStatement::defineInt64(uint64_t col, int64_t& val) {
OCIDefine* defp = nullptr;
sword ret = OCIDefineByPos(stmthp, &defp, conn->errhp, col, &val, sizeof(val), SQLT_INT, nullptr,
nullptr, nullptr, OCI_DEFAULT);
if (defp != nullptr)
defines.push_back(defp);
conn->env->checkErr(conn->errhp, ret);
}
bool DatabaseStatement::isNull(uint64_t col) {
OCIParam* paramdp;
conn->env->checkErr(conn->errhp, OCIParamGet(stmthp, OCI_HTYPE_STMT, conn->errhp,
reinterpret_cast<void**>(¶mdp), col));
uint32_t fieldLength = 0;
conn->env->checkErr(conn->errhp, OCIAttrGet(paramdp, OCI_DTYPE_PARAM, reinterpret_cast<dvoid*>(&fieldLength), nullptr,
OCI_ATTR_DATA_SIZE, conn->errhp));
return (fieldLength == 0);
}
}