0
|
1 /**
|
|
2 * Palantir - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
17
|
6 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are
|
|
10 * met:
|
0
|
11 *
|
17
|
12 * * Redistributions of source code must retain the above copyright
|
|
13 * notice, this list of conditions and the following disclaimer.
|
|
14 * * Redistributions in binary form must reproduce the above
|
|
15 * copyright notice, this list of conditions and the following disclaimer
|
|
16 * in the documentation and/or other materials provided with the
|
|
17 * distribution.
|
|
18 * * Neither the name of Google Inc., the name of the CHU of Liege,
|
|
19 * nor the names of its contributors may be used to endorse or promote
|
|
20 * products derived from this software without specific prior written
|
|
21 * permission.
|
|
22 *
|
|
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
0
|
34 **/
|
|
35
|
|
36
|
|
37 #include "Statement.h"
|
|
38
|
|
39 #include "Connection.h"
|
|
40 #include "../Toolbox.h"
|
|
41
|
|
42 #include <boost/lexical_cast.hpp>
|
|
43 #include <sqlite3.h>
|
|
44 #include <string.h>
|
|
45
|
|
46 namespace Palantir
|
|
47 {
|
|
48 namespace SQLite
|
|
49 {
|
|
50 int Statement::CheckError(int err) const
|
|
51 {
|
|
52 bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE);
|
|
53 if (!succeeded)
|
|
54 {
|
|
55 throw PalantirException("SQLite error code " + boost::lexical_cast<std::string>(err));
|
|
56 }
|
|
57
|
|
58 return err;
|
|
59 }
|
|
60
|
|
61 void Statement::CheckOk(int err) const
|
|
62 {
|
|
63 if (err == SQLITE_RANGE)
|
|
64 {
|
|
65 // Binding to a non-existent variable is evidence of a serious error.
|
|
66 throw PalantirException("Bind value out of range");
|
|
67 }
|
|
68 else if (err != SQLITE_OK)
|
|
69 {
|
|
70 throw PalantirException("SQLite error code " + boost::lexical_cast<std::string>(err));
|
|
71 }
|
|
72 }
|
|
73
|
|
74
|
|
75 Statement::Statement(Connection& database,
|
|
76 const StatementId& id,
|
|
77 const std::string& sql) :
|
|
78 reference_(database.GetCachedStatement(id, sql.c_str()))
|
|
79 {
|
|
80 Reset(true);
|
|
81 }
|
|
82
|
|
83
|
|
84 Statement::Statement(Connection& database,
|
|
85 const StatementId& id,
|
|
86 const char* sql) :
|
|
87 reference_(database.GetCachedStatement(id, sql))
|
|
88 {
|
|
89 Reset(true);
|
|
90 }
|
|
91
|
|
92
|
|
93 Statement::Statement(Connection& database,
|
|
94 const std::string& sql) :
|
|
95 reference_(database.GetWrappedObject(), sql.c_str())
|
|
96 {
|
|
97 }
|
|
98
|
|
99
|
|
100 Statement::Statement(Connection& database,
|
|
101 const char* sql) :
|
|
102 reference_(database.GetWrappedObject(), sql)
|
|
103 {
|
|
104 }
|
|
105
|
|
106
|
|
107 bool Statement::Run()
|
|
108 {
|
|
109 return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE;
|
|
110 }
|
|
111
|
|
112 bool Statement::Step()
|
|
113 {
|
|
114 return CheckError(sqlite3_step(GetStatement())) == SQLITE_ROW;
|
|
115 }
|
|
116
|
|
117 void Statement::Reset(bool clear_bound_vars)
|
|
118 {
|
|
119 // We don't call CheckError() here because sqlite3_reset() returns
|
|
120 // the last error that Step() caused thereby generating a second
|
|
121 // spurious error callback.
|
|
122 if (clear_bound_vars)
|
|
123 sqlite3_clear_bindings(GetStatement());
|
|
124 sqlite3_reset(GetStatement());
|
|
125 }
|
|
126
|
|
127 std::string Statement::GetOriginalSQLStatement()
|
|
128 {
|
|
129 return std::string(sqlite3_sql(GetStatement()));
|
|
130 }
|
|
131
|
|
132
|
|
133 void Statement::BindNull(int col)
|
|
134 {
|
|
135 CheckOk(sqlite3_bind_null(GetStatement(), col + 1));
|
|
136 }
|
|
137
|
|
138 void Statement::BindBool(int col, bool val)
|
|
139 {
|
|
140 BindInt(col, val ? 1 : 0);
|
|
141 }
|
|
142
|
|
143 void Statement::BindInt(int col, int val)
|
|
144 {
|
|
145 CheckOk(sqlite3_bind_int(GetStatement(), col + 1, val));
|
|
146 }
|
|
147
|
|
148 void Statement::BindInt64(int col, int64_t val)
|
|
149 {
|
|
150 CheckOk(sqlite3_bind_int64(GetStatement(), col + 1, val));
|
|
151 }
|
|
152
|
|
153 void Statement::BindDouble(int col, double val)
|
|
154 {
|
|
155 CheckOk(sqlite3_bind_double(GetStatement(), col + 1, val));
|
|
156 }
|
|
157
|
|
158 void Statement::BindCString(int col, const char* val)
|
|
159 {
|
|
160 CheckOk(sqlite3_bind_text(GetStatement(), col + 1, val, -1, SQLITE_TRANSIENT));
|
|
161 }
|
|
162
|
|
163 void Statement::BindString(int col, const std::string& val)
|
|
164 {
|
|
165 CheckOk(sqlite3_bind_text(GetStatement(),
|
|
166 col + 1,
|
|
167 val.data(),
|
|
168 val.size(),
|
|
169 SQLITE_TRANSIENT));
|
|
170 }
|
|
171
|
|
172 /*void Statement::BindString16(int col, const string16& value)
|
|
173 {
|
|
174 BindString(col, UTF16ToUTF8(value));
|
|
175 }*/
|
|
176
|
|
177 void Statement::BindBlob(int col, const void* val, int val_len)
|
|
178 {
|
|
179 CheckOk(sqlite3_bind_blob(GetStatement(), col + 1, val, val_len, SQLITE_TRANSIENT));
|
|
180 }
|
|
181
|
|
182
|
|
183 int Statement::ColumnCount() const
|
|
184 {
|
|
185 return sqlite3_column_count(GetStatement());
|
|
186 }
|
|
187
|
|
188
|
|
189 ColumnType Statement::GetColumnType(int col) const
|
|
190 {
|
|
191 // Verify that our enum matches sqlite's values.
|
|
192 assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER);
|
|
193 assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT);
|
|
194 assert(COLUMN_TYPE_TEXT == SQLITE_TEXT);
|
|
195 assert(COLUMN_TYPE_BLOB == SQLITE_BLOB);
|
|
196 assert(COLUMN_TYPE_NULL == SQLITE_NULL);
|
|
197
|
|
198 return static_cast<ColumnType>(sqlite3_column_type(GetStatement(), col));
|
|
199 }
|
|
200
|
|
201 ColumnType Statement::GetDeclaredColumnType(int col) const
|
|
202 {
|
|
203 std::string column_type(sqlite3_column_decltype(GetStatement(), col));
|
|
204 Toolbox::ToLowerCase(column_type);
|
|
205
|
|
206 if (column_type == "integer")
|
|
207 return COLUMN_TYPE_INTEGER;
|
|
208 else if (column_type == "float")
|
|
209 return COLUMN_TYPE_FLOAT;
|
|
210 else if (column_type == "text")
|
|
211 return COLUMN_TYPE_TEXT;
|
|
212 else if (column_type == "blob")
|
|
213 return COLUMN_TYPE_BLOB;
|
|
214
|
|
215 return COLUMN_TYPE_NULL;
|
|
216 }
|
|
217
|
|
218 bool Statement::ColumnBool(int col) const
|
|
219 {
|
|
220 return !!ColumnInt(col);
|
|
221 }
|
|
222
|
|
223 int Statement::ColumnInt(int col) const
|
|
224 {
|
|
225 return sqlite3_column_int(GetStatement(), col);
|
|
226 }
|
|
227
|
|
228 int64_t Statement::ColumnInt64(int col) const
|
|
229 {
|
|
230 return sqlite3_column_int64(GetStatement(), col);
|
|
231 }
|
|
232
|
|
233 double Statement::ColumnDouble(int col) const
|
|
234 {
|
|
235 return sqlite3_column_double(GetStatement(), col);
|
|
236 }
|
|
237
|
|
238 std::string Statement::ColumnString(int col) const
|
|
239 {
|
|
240 const char* str = reinterpret_cast<const char*>(
|
|
241 sqlite3_column_text(GetStatement(), col));
|
|
242 int len = sqlite3_column_bytes(GetStatement(), col);
|
|
243
|
|
244 std::string result;
|
|
245 if (str && len > 0)
|
|
246 result.assign(str, len);
|
|
247 return result;
|
|
248 }
|
|
249
|
|
250 /*string16 Statement::ColumnString16(int col) const
|
|
251 {
|
|
252 std::string s = ColumnString(col);
|
|
253 return !s.empty() ? UTF8ToUTF16(s) : string16();
|
|
254 }*/
|
|
255
|
|
256 int Statement::ColumnByteLength(int col) const
|
|
257 {
|
|
258 return sqlite3_column_bytes(GetStatement(), col);
|
|
259 }
|
|
260
|
|
261 const void* Statement::ColumnBlob(int col) const
|
|
262 {
|
|
263 return sqlite3_column_blob(GetStatement(), col);
|
|
264 }
|
|
265
|
|
266 bool Statement::ColumnBlobAsString(int col, std::string* blob)
|
|
267 {
|
|
268 const void* p = ColumnBlob(col);
|
|
269 size_t len = ColumnByteLength(col);
|
|
270 blob->resize(len);
|
|
271 if (blob->size() != len) {
|
|
272 return false;
|
|
273 }
|
|
274 blob->assign(reinterpret_cast<const char*>(p), len);
|
|
275 return true;
|
|
276 }
|
|
277
|
|
278 /*bool Statement::ColumnBlobAsString16(int col, string16* val) const
|
|
279 {
|
|
280 const void* data = ColumnBlob(col);
|
|
281 size_t len = ColumnByteLength(col) / sizeof(char16);
|
|
282 val->resize(len);
|
|
283 if (val->size() != len)
|
|
284 return false;
|
|
285 val->assign(reinterpret_cast<const char16*>(data), len);
|
|
286 return true;
|
|
287 }*/
|
|
288
|
|
289 bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const
|
|
290 {
|
|
291 val->clear();
|
|
292
|
|
293 const void* data = sqlite3_column_blob(GetStatement(), col);
|
|
294 int len = sqlite3_column_bytes(GetStatement(), col);
|
|
295 if (data && len > 0) {
|
|
296 val->resize(len);
|
|
297 memcpy(&(*val)[0], data, len);
|
|
298 }
|
|
299 return true;
|
|
300 }
|
|
301
|
|
302 bool Statement::ColumnBlobAsVector(
|
|
303 int col,
|
|
304 std::vector<unsigned char>* val) const
|
|
305 {
|
|
306 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val));
|
|
307 }
|
|
308
|
|
309 }
|
|
310 }
|