Mercurial > hg > orthanc-webviewer
comparison Orthanc/SQLite/Statement.cpp @ 0:02f7a0400a91
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Feb 2015 13:45:35 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:02f7a0400a91 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * | |
4 * Copyright (C) 2012-2015 Sebastien Jodogne <s.jodogne@gmail.com>, | |
5 * Medical Physics Department, CHU of Liege, Belgium | |
6 * | |
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: | |
12 * | |
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. | |
35 **/ | |
36 | |
37 | |
38 #if ORTHANC_SQLITE_STANDALONE != 1 | |
39 #include "../PrecompiledHeaders.h" | |
40 #endif | |
41 | |
42 #include "Statement.h" | |
43 #include "Connection.h" | |
44 | |
45 #include <sqlite3.h> | |
46 #include <string.h> | |
47 #include <stdio.h> | |
48 #include <algorithm> | |
49 | |
50 #if ORTHANC_SQLITE_STANDALONE != 1 | |
51 #include <glog/logging.h> | |
52 #endif | |
53 | |
54 #if defined(_MSC_VER) | |
55 #define snprintf _snprintf | |
56 #endif | |
57 | |
58 namespace Orthanc | |
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 { | |
67 char buffer[128]; | |
68 snprintf(buffer, sizeof(buffer) - 1, "SQLite error code %d", err); | |
69 throw OrthancSQLiteException(buffer); | |
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. | |
80 throw OrthancSQLiteException("Bind value out of range"); | |
81 } | |
82 else if (err != SQLITE_OK) | |
83 { | |
84 char buffer[128]; | |
85 snprintf(buffer, sizeof(buffer) - 1, "SQLite error code %d", err); | |
86 throw OrthancSQLiteException(buffer); | |
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 { | |
125 #if ORTHANC_SQLITE_STANDALONE != 1 | |
126 VLOG(1) << "SQLite::Statement::Run " << sqlite3_sql(GetStatement()); | |
127 #endif | |
128 | |
129 return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE; | |
130 } | |
131 | |
132 bool Statement::Step() | |
133 { | |
134 #if ORTHANC_SQLITE_STANDALONE != 1 | |
135 VLOG(1) << "SQLite::Statement::Step " << sqlite3_sql(GetStatement()); | |
136 #endif | |
137 | |
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()); | |
148 //VLOG(1) << "SQLite::Statement::Reset"; | |
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)); | |
229 std::transform(column_type.begin(), column_type.end(), column_type.begin(), tolower); | |
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 | |
243 bool Statement::ColumnIsNull(int col) const | |
244 { | |
245 return sqlite3_column_type(GetStatement(), col) == SQLITE_NULL; | |
246 } | |
247 | |
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 | |
319 /*bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const | |
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; | |
330 }*/ | |
331 | |
332 /*bool Statement::ColumnBlobAsVector( | |
333 int col, | |
334 std::vector<unsigned char>* val) const | |
335 { | |
336 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | |
337 }*/ | |
338 | |
339 } | |
340 } |