comparison Core/SQLite/Statement.cpp @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children db4d996ea264
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "Statement.h"
22
23 #include "Connection.h"
24 #include "../Toolbox.h"
25
26 #include <boost/lexical_cast.hpp>
27 #include <sqlite3.h>
28 #include <string.h>
29
30 namespace Palantir
31 {
32 namespace SQLite
33 {
34 int Statement::CheckError(int err) const
35 {
36 bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE);
37 if (!succeeded)
38 {
39 throw PalantirException("SQLite error code " + boost::lexical_cast<std::string>(err));
40 }
41
42 return err;
43 }
44
45 void Statement::CheckOk(int err) const
46 {
47 if (err == SQLITE_RANGE)
48 {
49 // Binding to a non-existent variable is evidence of a serious error.
50 throw PalantirException("Bind value out of range");
51 }
52 else if (err != SQLITE_OK)
53 {
54 throw PalantirException("SQLite error code " + boost::lexical_cast<std::string>(err));
55 }
56 }
57
58
59 Statement::Statement(Connection& database,
60 const StatementId& id,
61 const std::string& sql) :
62 reference_(database.GetCachedStatement(id, sql.c_str()))
63 {
64 Reset(true);
65 }
66
67
68 Statement::Statement(Connection& database,
69 const StatementId& id,
70 const char* sql) :
71 reference_(database.GetCachedStatement(id, sql))
72 {
73 Reset(true);
74 }
75
76
77 Statement::Statement(Connection& database,
78 const std::string& sql) :
79 reference_(database.GetWrappedObject(), sql.c_str())
80 {
81 }
82
83
84 Statement::Statement(Connection& database,
85 const char* sql) :
86 reference_(database.GetWrappedObject(), sql)
87 {
88 }
89
90
91 bool Statement::Run()
92 {
93 return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE;
94 }
95
96 bool Statement::Step()
97 {
98 return CheckError(sqlite3_step(GetStatement())) == SQLITE_ROW;
99 }
100
101 void Statement::Reset(bool clear_bound_vars)
102 {
103 // We don't call CheckError() here because sqlite3_reset() returns
104 // the last error that Step() caused thereby generating a second
105 // spurious error callback.
106 if (clear_bound_vars)
107 sqlite3_clear_bindings(GetStatement());
108 sqlite3_reset(GetStatement());
109 }
110
111 std::string Statement::GetOriginalSQLStatement()
112 {
113 return std::string(sqlite3_sql(GetStatement()));
114 }
115
116
117 void Statement::BindNull(int col)
118 {
119 CheckOk(sqlite3_bind_null(GetStatement(), col + 1));
120 }
121
122 void Statement::BindBool(int col, bool val)
123 {
124 BindInt(col, val ? 1 : 0);
125 }
126
127 void Statement::BindInt(int col, int val)
128 {
129 CheckOk(sqlite3_bind_int(GetStatement(), col + 1, val));
130 }
131
132 void Statement::BindInt64(int col, int64_t val)
133 {
134 CheckOk(sqlite3_bind_int64(GetStatement(), col + 1, val));
135 }
136
137 void Statement::BindDouble(int col, double val)
138 {
139 CheckOk(sqlite3_bind_double(GetStatement(), col + 1, val));
140 }
141
142 void Statement::BindCString(int col, const char* val)
143 {
144 CheckOk(sqlite3_bind_text(GetStatement(), col + 1, val, -1, SQLITE_TRANSIENT));
145 }
146
147 void Statement::BindString(int col, const std::string& val)
148 {
149 CheckOk(sqlite3_bind_text(GetStatement(),
150 col + 1,
151 val.data(),
152 val.size(),
153 SQLITE_TRANSIENT));
154 }
155
156 /*void Statement::BindString16(int col, const string16& value)
157 {
158 BindString(col, UTF16ToUTF8(value));
159 }*/
160
161 void Statement::BindBlob(int col, const void* val, int val_len)
162 {
163 CheckOk(sqlite3_bind_blob(GetStatement(), col + 1, val, val_len, SQLITE_TRANSIENT));
164 }
165
166
167 int Statement::ColumnCount() const
168 {
169 return sqlite3_column_count(GetStatement());
170 }
171
172
173 ColumnType Statement::GetColumnType(int col) const
174 {
175 // Verify that our enum matches sqlite's values.
176 assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER);
177 assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT);
178 assert(COLUMN_TYPE_TEXT == SQLITE_TEXT);
179 assert(COLUMN_TYPE_BLOB == SQLITE_BLOB);
180 assert(COLUMN_TYPE_NULL == SQLITE_NULL);
181
182 return static_cast<ColumnType>(sqlite3_column_type(GetStatement(), col));
183 }
184
185 ColumnType Statement::GetDeclaredColumnType(int col) const
186 {
187 std::string column_type(sqlite3_column_decltype(GetStatement(), col));
188 Toolbox::ToLowerCase(column_type);
189
190 if (column_type == "integer")
191 return COLUMN_TYPE_INTEGER;
192 else if (column_type == "float")
193 return COLUMN_TYPE_FLOAT;
194 else if (column_type == "text")
195 return COLUMN_TYPE_TEXT;
196 else if (column_type == "blob")
197 return COLUMN_TYPE_BLOB;
198
199 return COLUMN_TYPE_NULL;
200 }
201
202 bool Statement::ColumnBool(int col) const
203 {
204 return !!ColumnInt(col);
205 }
206
207 int Statement::ColumnInt(int col) const
208 {
209 return sqlite3_column_int(GetStatement(), col);
210 }
211
212 int64_t Statement::ColumnInt64(int col) const
213 {
214 return sqlite3_column_int64(GetStatement(), col);
215 }
216
217 double Statement::ColumnDouble(int col) const
218 {
219 return sqlite3_column_double(GetStatement(), col);
220 }
221
222 std::string Statement::ColumnString(int col) const
223 {
224 const char* str = reinterpret_cast<const char*>(
225 sqlite3_column_text(GetStatement(), col));
226 int len = sqlite3_column_bytes(GetStatement(), col);
227
228 std::string result;
229 if (str && len > 0)
230 result.assign(str, len);
231 return result;
232 }
233
234 /*string16 Statement::ColumnString16(int col) const
235 {
236 std::string s = ColumnString(col);
237 return !s.empty() ? UTF8ToUTF16(s) : string16();
238 }*/
239
240 int Statement::ColumnByteLength(int col) const
241 {
242 return sqlite3_column_bytes(GetStatement(), col);
243 }
244
245 const void* Statement::ColumnBlob(int col) const
246 {
247 return sqlite3_column_blob(GetStatement(), col);
248 }
249
250 bool Statement::ColumnBlobAsString(int col, std::string* blob)
251 {
252 const void* p = ColumnBlob(col);
253 size_t len = ColumnByteLength(col);
254 blob->resize(len);
255 if (blob->size() != len) {
256 return false;
257 }
258 blob->assign(reinterpret_cast<const char*>(p), len);
259 return true;
260 }
261
262 /*bool Statement::ColumnBlobAsString16(int col, string16* val) const
263 {
264 const void* data = ColumnBlob(col);
265 size_t len = ColumnByteLength(col) / sizeof(char16);
266 val->resize(len);
267 if (val->size() != len)
268 return false;
269 val->assign(reinterpret_cast<const char16*>(data), len);
270 return true;
271 }*/
272
273 bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const
274 {
275 val->clear();
276
277 const void* data = sqlite3_column_blob(GetStatement(), col);
278 int len = sqlite3_column_bytes(GetStatement(), col);
279 if (data && len > 0) {
280 val->resize(len);
281 memcpy(&(*val)[0], data, len);
282 }
283 return true;
284 }
285
286 bool Statement::ColumnBlobAsVector(
287 int col,
288 std::vector<unsigned char>* val) const
289 {
290 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val));
291 }
292
293 }
294 }