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