Mercurial > hg > orthanc
annotate Core/SQLite/Statement.cpp @ 1380:31b884468b05
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 29 May 2015 13:23:54 +0200 |
parents | 63d47b1fa239 |
children | 3b68924ffb24 |
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 <sqlite3.h> | |
46 #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
|
47 #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
|
48 #include <algorithm> |
0 | 49 |
1226 | 50 #if ORTHANC_SQLITE_STANDALONE != 1 |
51 #include <glog/logging.h> | |
52 #endif | |
53 | |
1222
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
54 #if defined(_MSC_VER) |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
55 #define snprintf _snprintf |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
56 #endif |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
57 |
59 | 58 namespace Orthanc |
0 | 59 { |
60 namespace SQLite | |
61 { | |
62 int Statement::CheckError(int err) const | |
63 { | |
64 bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | |
65 if (!succeeded) | |
66 { | |
1221
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
67 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
|
68 snprintf(buffer, sizeof(buffer) - 1, "SQLite error code %d", err); |
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
69 throw OrthancSQLiteException(buffer); |
0 | 70 } |
71 | |
72 return err; | |
73 } | |
74 | |
75 void Statement::CheckOk(int err) const | |
76 { | |
77 if (err == SQLITE_RANGE) | |
78 { | |
79 // Binding to a non-existent variable is evidence of a serious error. | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
80 throw OrthancSQLiteException("Bind value out of range"); |
0 | 81 } |
82 else if (err != SQLITE_OK) | |
83 { | |
1221
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
84 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
|
85 snprintf(buffer, sizeof(buffer) - 1, "SQLite error code %d", err); |
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
86 throw OrthancSQLiteException(buffer); |
0 | 87 } |
88 } | |
89 | |
90 | |
91 Statement::Statement(Connection& database, | |
92 const StatementId& id, | |
93 const std::string& sql) : | |
94 reference_(database.GetCachedStatement(id, sql.c_str())) | |
95 { | |
96 Reset(true); | |
97 } | |
98 | |
99 | |
100 Statement::Statement(Connection& database, | |
101 const StatementId& id, | |
102 const char* sql) : | |
103 reference_(database.GetCachedStatement(id, sql)) | |
104 { | |
105 Reset(true); | |
106 } | |
107 | |
108 | |
109 Statement::Statement(Connection& database, | |
110 const std::string& sql) : | |
111 reference_(database.GetWrappedObject(), sql.c_str()) | |
112 { | |
113 } | |
114 | |
115 | |
116 Statement::Statement(Connection& database, | |
117 const char* sql) : | |
118 reference_(database.GetWrappedObject(), sql) | |
119 { | |
120 } | |
121 | |
122 | |
123 bool Statement::Run() | |
124 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
125 #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
|
126 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
|
127 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
128 |
0 | 129 return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE; |
130 } | |
131 | |
132 bool Statement::Step() | |
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::Step " << 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 |
0 | 138 return CheckError(sqlite3_step(GetStatement())) == SQLITE_ROW; |
139 } | |
140 | |
141 void Statement::Reset(bool clear_bound_vars) | |
142 { | |
143 // We don't call CheckError() here because sqlite3_reset() returns | |
144 // the last error that Step() caused thereby generating a second | |
145 // spurious error callback. | |
146 if (clear_bound_vars) | |
147 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
|
148 //VLOG(1) << "SQLite::Statement::Reset"; |
0 | 149 sqlite3_reset(GetStatement()); |
150 } | |
151 | |
152 std::string Statement::GetOriginalSQLStatement() | |
153 { | |
154 return std::string(sqlite3_sql(GetStatement())); | |
155 } | |
156 | |
157 | |
158 void Statement::BindNull(int col) | |
159 { | |
160 CheckOk(sqlite3_bind_null(GetStatement(), col + 1)); | |
161 } | |
162 | |
163 void Statement::BindBool(int col, bool val) | |
164 { | |
165 BindInt(col, val ? 1 : 0); | |
166 } | |
167 | |
168 void Statement::BindInt(int col, int val) | |
169 { | |
170 CheckOk(sqlite3_bind_int(GetStatement(), col + 1, val)); | |
171 } | |
172 | |
173 void Statement::BindInt64(int col, int64_t val) | |
174 { | |
175 CheckOk(sqlite3_bind_int64(GetStatement(), col + 1, val)); | |
176 } | |
177 | |
178 void Statement::BindDouble(int col, double val) | |
179 { | |
180 CheckOk(sqlite3_bind_double(GetStatement(), col + 1, val)); | |
181 } | |
182 | |
183 void Statement::BindCString(int col, const char* val) | |
184 { | |
185 CheckOk(sqlite3_bind_text(GetStatement(), col + 1, val, -1, SQLITE_TRANSIENT)); | |
186 } | |
187 | |
188 void Statement::BindString(int col, const std::string& val) | |
189 { | |
190 CheckOk(sqlite3_bind_text(GetStatement(), | |
191 col + 1, | |
192 val.data(), | |
193 val.size(), | |
194 SQLITE_TRANSIENT)); | |
195 } | |
196 | |
197 /*void Statement::BindString16(int col, const string16& value) | |
198 { | |
199 BindString(col, UTF16ToUTF8(value)); | |
200 }*/ | |
201 | |
202 void Statement::BindBlob(int col, const void* val, int val_len) | |
203 { | |
204 CheckOk(sqlite3_bind_blob(GetStatement(), col + 1, val, val_len, SQLITE_TRANSIENT)); | |
205 } | |
206 | |
207 | |
208 int Statement::ColumnCount() const | |
209 { | |
210 return sqlite3_column_count(GetStatement()); | |
211 } | |
212 | |
213 | |
214 ColumnType Statement::GetColumnType(int col) const | |
215 { | |
216 // Verify that our enum matches sqlite's values. | |
217 assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER); | |
218 assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT); | |
219 assert(COLUMN_TYPE_TEXT == SQLITE_TEXT); | |
220 assert(COLUMN_TYPE_BLOB == SQLITE_BLOB); | |
221 assert(COLUMN_TYPE_NULL == SQLITE_NULL); | |
222 | |
223 return static_cast<ColumnType>(sqlite3_column_type(GetStatement(), col)); | |
224 } | |
225 | |
226 ColumnType Statement::GetDeclaredColumnType(int col) const | |
227 { | |
228 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
|
229 std::transform(column_type.begin(), column_type.end(), column_type.begin(), tolower); |
0 | 230 |
231 if (column_type == "integer") | |
232 return COLUMN_TYPE_INTEGER; | |
233 else if (column_type == "float") | |
234 return COLUMN_TYPE_FLOAT; | |
235 else if (column_type == "text") | |
236 return COLUMN_TYPE_TEXT; | |
237 else if (column_type == "blob") | |
238 return COLUMN_TYPE_BLOB; | |
239 | |
240 return COLUMN_TYPE_NULL; | |
241 } | |
242 | |
80 | 243 bool Statement::ColumnIsNull(int col) const |
244 { | |
245 return sqlite3_column_type(GetStatement(), col) == SQLITE_NULL; | |
246 } | |
247 | |
0 | 248 bool Statement::ColumnBool(int col) const |
249 { | |
250 return !!ColumnInt(col); | |
251 } | |
252 | |
253 int Statement::ColumnInt(int col) const | |
254 { | |
255 return sqlite3_column_int(GetStatement(), col); | |
256 } | |
257 | |
258 int64_t Statement::ColumnInt64(int col) const | |
259 { | |
260 return sqlite3_column_int64(GetStatement(), col); | |
261 } | |
262 | |
263 double Statement::ColumnDouble(int col) const | |
264 { | |
265 return sqlite3_column_double(GetStatement(), col); | |
266 } | |
267 | |
268 std::string Statement::ColumnString(int col) const | |
269 { | |
270 const char* str = reinterpret_cast<const char*>( | |
271 sqlite3_column_text(GetStatement(), col)); | |
272 int len = sqlite3_column_bytes(GetStatement(), col); | |
273 | |
274 std::string result; | |
275 if (str && len > 0) | |
276 result.assign(str, len); | |
277 return result; | |
278 } | |
279 | |
280 /*string16 Statement::ColumnString16(int col) const | |
281 { | |
282 std::string s = ColumnString(col); | |
283 return !s.empty() ? UTF8ToUTF16(s) : string16(); | |
284 }*/ | |
285 | |
286 int Statement::ColumnByteLength(int col) const | |
287 { | |
288 return sqlite3_column_bytes(GetStatement(), col); | |
289 } | |
290 | |
291 const void* Statement::ColumnBlob(int col) const | |
292 { | |
293 return sqlite3_column_blob(GetStatement(), col); | |
294 } | |
295 | |
296 bool Statement::ColumnBlobAsString(int col, std::string* blob) | |
297 { | |
298 const void* p = ColumnBlob(col); | |
299 size_t len = ColumnByteLength(col); | |
300 blob->resize(len); | |
301 if (blob->size() != len) { | |
302 return false; | |
303 } | |
304 blob->assign(reinterpret_cast<const char*>(p), len); | |
305 return true; | |
306 } | |
307 | |
308 /*bool Statement::ColumnBlobAsString16(int col, string16* val) const | |
309 { | |
310 const void* data = ColumnBlob(col); | |
311 size_t len = ColumnByteLength(col) / sizeof(char16); | |
312 val->resize(len); | |
313 if (val->size() != len) | |
314 return false; | |
315 val->assign(reinterpret_cast<const char16*>(data), len); | |
316 return true; | |
317 }*/ | |
318 | |
724 | 319 /*bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const |
0 | 320 { |
321 val->clear(); | |
322 | |
323 const void* data = sqlite3_column_blob(GetStatement(), col); | |
324 int len = sqlite3_column_bytes(GetStatement(), col); | |
325 if (data && len > 0) { | |
326 val->resize(len); | |
327 memcpy(&(*val)[0], data, len); | |
328 } | |
329 return true; | |
724 | 330 }*/ |
0 | 331 |
724 | 332 /*bool Statement::ColumnBlobAsVector( |
0 | 333 int col, |
334 std::vector<unsigned char>* val) const | |
335 { | |
336 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | |
724 | 337 }*/ |
0 | 338 |
339 } | |
340 } |