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