-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsqlite.cpp
More file actions
308 lines (261 loc) · 7.83 KB
/
sqlite.cpp
File metadata and controls
308 lines (261 loc) · 7.83 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
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
/******************************************************************************
* src/sqlite.cpp
*
* Encapsulate SQLite queries into a C++ class, which is a specialization of
* the generic SQL database interface.
*
******************************************************************************
* Copyright (C) 2014 Timo Bingmann <[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 3 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, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "sqlite.h"
#include "common.h"
#include "strtools.h"
#include <cassert>
#include <cstring>
#include <iomanip>
#include <vector>
//! Execute a SQL query without parameters, throws on errors.
SQLiteQuery::SQLiteQuery(class SQLiteDatabase& db, const std::string& query)
: SqlQueryImpl(query),
m_db(db)
{
const char* zTail = 0;
int rc = sqlite3_prepare_v2(m_db.m_db, query.c_str(), query.size()+1,
&m_stmt, &zTail);
if (rc != SQLITE_OK)
{
OUT_THROW("SQL query parse " << query << "\n" <<
"Failed at " << zTail << " : " << m_db.errmsg());
}
rc = sqlite3_step(m_stmt);
if (rc == SQLITE_ROW)
{
m_row = -1;
}
else if (rc == SQLITE_DONE)
{
m_row = -2;
}
else
{
OUT_THROW("SQL query " << query << "\n" <<
"Failed : " << m_db.errmsg());
}
}
//! Execute a SQL query with parameters, throws on errors.
SQLiteQuery::SQLiteQuery(class SQLiteDatabase& db, const std::string& query,
const std::vector<std::string>& params)
: SqlQueryImpl(query),
m_db(db)
{
const char* zTail = 0;
int rc = sqlite3_prepare_v2(m_db.m_db, query.c_str(), query.size()+1,
&m_stmt, &zTail);
if (rc != SQLITE_OK)
{
OUT_THROW("SQL query parse " << query << "\n" <<
"Failed at " << zTail << " : " << m_db.errmsg());
}
for (size_t i = 0; i < params.size(); ++i)
{
sqlite3_bind_text(m_stmt, i+1,
params[i].data(), params[i].size(), NULL);
}
rc = sqlite3_step(m_stmt);
if (rc == SQLITE_ROW)
{
m_row = -1;
}
else if (rc == SQLITE_DONE)
{
m_row = -2;
}
else
{
OUT_THROW("SQL query " << query << "\n" <<
"Failed : " << m_db.errmsg());
}
}
//! Free result
SQLiteQuery::~SQLiteQuery()
{
sqlite3_finalize(m_stmt);
}
//! Return number of rows in result, throws if no tuples.
unsigned int SQLiteQuery::num_rows() const
{
if (SqlDataCache::is_complete())
return SqlDataCache::num_rows();
assert(!"Row number not available without cached result.");
return -1;
}
//! Return number of columns in result, throws if no tuples.
unsigned int SQLiteQuery::num_cols() const
{
return sqlite3_column_count(m_stmt);
}
//! Return column name of col
std::string SQLiteQuery::col_name(unsigned int col) const
{
return sqlite3_column_name(m_stmt, col);
}
//! Return the current row number
unsigned int SQLiteQuery::current_row() const
{
return m_row;
}
//! Advance current result row to next (or first if uninitialized)
bool SQLiteQuery::step()
{
if (m_row == -1) {
m_row++;
return true;
}
else if (m_row == -2) {
return false;
}
int rc = sqlite3_step(m_stmt);
if (rc == SQLITE_ROW)
{
++m_row;
return true;
}
else if (rc == SQLITE_DONE)
{
++m_row;
return false;
}
else
{
OUT_THROW("SQL query " << query() << "\n" <<
"Step failed : " << m_db.errmsg());
}
}
//! Returns true if cell (row,col) is NULL.
bool SQLiteQuery::isNULL(unsigned int col) const
{
assert(col < num_cols());
return sqlite3_column_type(m_stmt, col) == SQLITE_NULL;
}
//! Return text representation of column col of current row.
std::string SQLiteQuery::text(unsigned int col) const
{
assert(col < num_cols());
const unsigned char* data = sqlite3_column_text(m_stmt, col);
size_t size = sqlite3_column_bytes(m_stmt, col);
return std::string((const char*)data, size);
}
//! read complete result into memory
void SQLiteQuery::read_complete()
{
return SqlDataCache::read_complete(*this);
}
//! Returns true if cell (row,col) is NULL.
bool SQLiteQuery::isNULL(unsigned int row, unsigned int col) const
{
return SqlDataCache::isNULL(row, col);
}
//! Return text representation of cell (row,col).
std::string SQLiteQuery::text(unsigned int row, unsigned int col) const
{
return SqlDataCache::text(row, col);
}
////////////////////////////////////////////////////////////////////////////////
extern int RegisterExtensionFunctions(sqlite3 *db);
//! try to connect to the database with default parameters
bool SQLiteDatabase::initialize(const std::string& params)
{
OUT("Connecting to SQLite3 database \"" << params << "\".");
// make connection to in-memory database
int rc = sqlite3_open_v2(params.c_str(), &m_db,
SQLITE_OPEN_READWRITE, NULL);
if (rc != SQLITE_OK)
{
OUT("Connection to SQLite3 failed: " << sqlite3_errmsg(m_db));
sqlite3_close(m_db);
m_db = NULL;
return false;
}
// register additional math functions
RegisterExtensionFunctions(m_db);
return true;
}
//! destructor to free connection
SQLiteDatabase::~SQLiteDatabase()
{
if (m_db) {
sqlite3_close(m_db);
}
}
//! return type of SQL database
SQLiteDatabase::db_type SQLiteDatabase::type() const
{
return DB_SQLITE;
}
//! return string for the i-th placeholder, where i starts at 0.
std::string SQLiteDatabase::placeholder(unsigned int i) const
{
return '$' + to_str(i+1);
}
//! return quoted table or field identifier
std::string SQLiteDatabase::quote_field(const std::string& field) const
{
return '"' + field + '"';
}
//! execute SQL query without result
bool SQLiteDatabase::execute(const std::string& query)
{
char* zTail = 0;
int rc = sqlite3_exec(m_db, query.c_str(), NULL, NULL, &zTail);
if (rc != SQLITE_OK)
{
OUT_THROW("SQL query \"" << query << "\"\n" <<
"Failed at " << zTail << " : " << errmsg());
}
return true;
}
//! construct query object for given string
SqlQuery SQLiteDatabase::query(const std::string& query)
{
return SqlQuery( new SQLiteQuery(*this, query) );
}
//! construct query object for given string with placeholder parameters
SqlQuery SQLiteDatabase::query(const std::string& query,
const std::vector<std::string>& params)
{
return SqlQuery( new SQLiteQuery(*this, query, params) );
}
//! test if a table exists in the database
bool SQLiteDatabase::exist_table(const std::string& table)
{
std::vector<std::string> params;
params.push_back(table);
SQLiteQuery sql(*this,
"SELECT COUNT(*) FROM sqlite_master "
"WHERE type='table' AND name = $1",
params);
assert(sql.num_cols() == 1);
if (!sql.step()) {
OUT_THROW("exist_table() failed.");
}
return (sql.text(0) != "0");
}
//! return last error message string
const char* SQLiteDatabase::errmsg() const
{
return sqlite3_errmsg(m_db);
}
////////////////////////////////////////////////////////////////////////////////