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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* Copyright (C) 2009-2012 Geometer Plus <[email protected]>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <iostream>
#include <algorithm>
#include <ZLLogger.h>
#include <ZLStringUtil.h>
#include "SQLiteCommand.h"
#include "SQLiteConnection.h"
#include "SQLiteDataReader.h"
std::string SQLiteCommand::packCommand(const std::string &command) {
static const char _spaces[] = " \t\n";
std::string stripped = command;
ZLStringUtil::stripWhiteSpaces(stripped);
std::size_t pos = 0;
while (true) {
pos = stripped.find_first_of(_spaces, pos);
if (pos == std::string::npos) {
break;
}
stripped[pos++] = ' ';
const std::size_t next = stripped.find_first_not_of(_spaces, pos);
if (pos != std::string::npos && next > pos) {
stripped.erase(pos, next - pos);
}
}
return stripped;
}
SQLiteCommand::~SQLiteCommand() {
SQLiteConnection &con = (SQLiteConnection &) connection();
if (con.isOpened() && myStatements.size() != 0) {
finalizeStatements();
}
}
bool SQLiteCommand::execute() {
ZLLogger::Instance().println("sqlite", "execute: " + commandString());
SQLiteConnection &con = (SQLiteConnection &) connection();
if (!con.isOpened()) {
myStatements.clear();
return false;
}
if (!prepareStatements(con)) {
return false;
}
std::vector<sqlite3_stmt *>::iterator it = myStatements.begin();
std::vector<sqlite3_stmt *>::iterator end = myStatements.end();
while (true) {
int res = sqlite3_step(*it);
switch (res) {
case SQLITE_DONE:
if (++it == end) {
resetStatements();
return true;
}
break;
case SQLITE_OK:
case SQLITE_ROW:
break;
default:
dumpError();
finalizeStatements();
return false;
}
}
}
shared_ptr<DBValue> SQLiteCommand::executeScalar() {
ZLLogger::Instance().println("sqlite", "executeScalar: " + commandString());
SQLiteConnection &con = (SQLiteConnection &) connection();
if (!con.isOpened()) {
myStatements.clear();
return 0;
}
if (!prepareStatements(con)) {
return 0;
}
std::vector<sqlite3_stmt *>::iterator it = myStatements.begin();
std::vector<sqlite3_stmt *>::iterator end = myStatements.end();
while (true) {
int res = sqlite3_step(*it);
switch (res) {
case SQLITE_DONE:
if (++it == end) {
resetStatements();
return 0;
}
break;
case SQLITE_OK:
break;
case SQLITE_ROW: {
shared_ptr<DBValue> val = SQLiteDataReader::makeDBValue(*it, /* column = */ 0);
resetStatements();
return val;
}
default:
dumpError();
finalizeStatements();
return 0;
}
}
}
shared_ptr<DBDataReader> SQLiteCommand::executeReader() {
ZLLogger::Instance().println("sqlite", "executeReader: " + commandString());
SQLiteConnection &con = (SQLiteConnection &) connection();
if (!con.isOpened()) {
myStatements.clear();
return 0;
}
if (!prepareStatements(con)) {
return 0;
}
myLocked = true;
return new SQLiteDataReader(*this);
}
bool SQLiteCommand::prepareStatements(SQLiteConnection &conn) {
sqlite3 *db = conn.database();
if (myLocked) {
return false;
}
if (myStatements.size() != 0) {
const std::size_t size = myStatements.size();
int res = SQLITE_OK;
for (std::size_t i = 0; i < size && res == SQLITE_OK; ++i) {
res = sqlite3_reset(myStatements[i]);
}
if (res == SQLITE_OK) {
bindParameters();
return true;
}
finalizeStatements();
}
const std::string sql = commandString();
const int length = -1;
const char *tail = sql.c_str();
while (true) {
sqlite3_stmt *statement;
int res = sqlite3_prepare_v2(db, tail, length, &statement, &tail);
if (res != SQLITE_OK) {
dumpError();
finalizeStatements();
return false;
}
if (statement == 0) {
break;
}
myStatements.push_back(statement);
conn.addStatement(statement);
}
if (!bindParameters()) {
finalizeStatements();
return false;
}
return true;
}
void SQLiteCommand::prepareBindContext() {
if (myBindContext.size() > 0) {
return;
}
std::size_t number = 0;
for (std::size_t i = 0; i < myStatements.size(); ++i) {
sqlite3_stmt *statement = myStatements[i];
const int count = sqlite3_bind_parameter_count(statement);
for (int j = 1; j <= count; ++j) {
++number;
const char *name = sqlite3_bind_parameter_name(statement, j);
if (name == 0) {
myBindContext.push_back(BindParameter(number));
} else {
const std::string namestr(name);
if (std::find_if(myBindContext.begin(), myBindContext.end(), BindParameterComparator(namestr)) == myBindContext.end()) {
myBindContext.push_back(BindParameter(number, namestr));
}
}
}
}
}
bool SQLiteCommand::bindParameters() {
prepareBindContext();
std::vector<DBCommandParameter> ¶ms = parameters();
bool res = true;
const std::size_t size = params.size();
for (std::size_t i = 0; i < size; ++i) {
DBCommandParameter &p = params[i];
if (p.hasName()) {
const std::string &name = p.name();
if (!bindParameterByName(name, p.value())) {
res = false;
}
} else if (i < myBindContext.size()) {
BindParameter &bp = myBindContext[i];
if (myBindContext[i].hasName()) {
if (!bindParameterByName(bp.Name, p.value())) {
res = false;
}
} else {
if (!bindParameterByIndex(bp.Position, p.value())) {
res = false;
}
}
} else {
res = false;
}
}
return res;
}
bool SQLiteCommand::bindParameterByName(const std::string &name, shared_ptr<DBValue> value) {
const std::size_t size = myStatements.size();
bool res = true;
bool binded = false;
for (std::size_t i = 0; i < size; ++i) {
sqlite3_stmt *statement = myStatements[i];
const int index = sqlite3_bind_parameter_index(statement, name.c_str());
if (index == 0) {
continue;
}
binded = true;
if (!bindParameter(statement, index, value)) {
res = false;
}
}
if (!binded) {
dumpError("parameter \"" + name + "\" is not found");
}
return res;
}
bool SQLiteCommand::bindParameterByIndex(std::size_t index, shared_ptr<DBValue> value) {
if (index == 0) {
return true;
}
const std::size_t size = myStatements.size();
int number = index;
for (std::size_t i = 0; i < size; ++i) {
sqlite3_stmt *statement = myStatements[i];
const int count = sqlite3_bind_parameter_count(statement);
if (number > count) {
number -= count;
continue;
}
return bindParameter(statement, number, value);
}
return true;
}
bool SQLiteCommand::bindParameter(sqlite3_stmt *statement, int number, shared_ptr<DBValue> value) {
DBValue::ValueType type = (value.isNull()) ? (DBValue::DBNULL) : (value->type());
int res;
switch (type) {
case DBValue::DBNULL:
res = sqlite3_bind_null(statement, number);
break;
case DBValue::DBINT:
res = sqlite3_bind_int(statement, number, ((DBIntValue &) *value).value());
break;
case DBValue::DBREAL:
res = sqlite3_bind_double(statement, number, ((DBRealValue &) *value).value());
break;
case DBValue::DBTEXT:
res = sqlite3_bind_text(statement, number, ((DBTextValue &) *value).value().c_str(), -1 /* zero-terminated string */, SQLITE_TRANSIENT);
break;
default:
return false;
}
if (res != SQLITE_OK) {
dumpError();
}
return res == SQLITE_OK;
}
void SQLiteCommand::finalizeStatements() {
SQLiteConnection &con = (SQLiteConnection &) connection();
const std::size_t size = myStatements.size();
for (std::size_t i = 0; i < size; ++i) {
sqlite3_stmt *statement = myStatements[i];
con.removeStatement(statement);
const int res = sqlite3_finalize(statement);
if (res != SQLITE_OK) {
dumpError();
}
}
myStatements.clear();
}
void SQLiteCommand::dumpError() const {
static const std::size_t cmdlimit = 114;
((SQLiteConnection &) connection()).dumpError();
const std::string &cmd = commandString();
if (cmd.length() > cmdlimit) {
std::cerr << "SQLITE IMPLEMENTATION ERROR: in command \"" << cmd.substr(0, cmdlimit - 3) << "...\"" << std::endl;
} else {
std::cerr << "SQLITE IMPLEMENTATION ERROR: in command \"" << cmd << "\"" << std::endl;
}
}
void SQLiteCommand::dumpError(const std::string &msg) const {
static const std::size_t cmdlimit = 129;
std::cerr << "SQLITE ERROR: " << msg << std::endl;
const std::string &cmd = commandString();
if (cmd.length() > cmdlimit) {
std::cerr << "SQLITE ERROR: in command \"" << cmd.substr(0, cmdlimit - 3) << "...\"" << std::endl;
} else {
std::cerr << "SQLITE ERROR: in command \"" << cmd << "\"" << std::endl;
}
}
bool SQLiteCommand::resetStatements() {
if (myStatements.size() == 0) {
return true;
}
const std::size_t size = myStatements.size();
int res = SQLITE_OK;
for (std::size_t i = 0; i < size && res == SQLITE_OK; ++i) {
res = sqlite3_reset(myStatements[i]);
}
if (res == SQLITE_OK) {
return true;
}
dumpError();
finalizeStatements();
return false;
}
|