Mercurial > hg > orthanc
annotate Core/SQLite/Statement.cpp @ 952:98d6ba37c7dc dicom-rt
integration mainline -> dicom-rt
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Jun 2014 15:33:02 +0200 |
parents | a811bdf8b8eb |
children | 9b9026560a5f |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
724
diff
changeset
|
37 #include "../PrecompiledHeaders.h" |
0 | 38 #include "Statement.h" |
39 | |
40 #include "Connection.h" | |
41 #include "../Toolbox.h" | |
42 | |
43 #include <boost/lexical_cast.hpp> | |
44 #include <sqlite3.h> | |
45 #include <string.h> | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
46 #include <glog/logging.h> |
0 | 47 |
59 | 48 namespace Orthanc |
0 | 49 { |
50 namespace SQLite | |
51 { | |
52 int Statement::CheckError(int err) const | |
53 { | |
54 bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | |
55 if (!succeeded) | |
56 { | |
59 | 57 throw OrthancException("SQLite error code " + boost::lexical_cast<std::string>(err)); |
0 | 58 } |
59 | |
60 return err; | |
61 } | |
62 | |
63 void Statement::CheckOk(int err) const | |
64 { | |
65 if (err == SQLITE_RANGE) | |
66 { | |
67 // Binding to a non-existent variable is evidence of a serious error. | |
59 | 68 throw OrthancException("Bind value out of range"); |
0 | 69 } |
70 else if (err != SQLITE_OK) | |
71 { | |
59 | 72 throw OrthancException("SQLite error code " + boost::lexical_cast<std::string>(err)); |
0 | 73 } |
74 } | |
75 | |
76 | |
77 Statement::Statement(Connection& database, | |
78 const StatementId& id, | |
79 const std::string& sql) : | |
80 reference_(database.GetCachedStatement(id, sql.c_str())) | |
81 { | |
82 Reset(true); | |
83 } | |
84 | |
85 | |
86 Statement::Statement(Connection& database, | |
87 const StatementId& id, | |
88 const char* sql) : | |
89 reference_(database.GetCachedStatement(id, sql)) | |
90 { | |
91 Reset(true); | |
92 } | |
93 | |
94 | |
95 Statement::Statement(Connection& database, | |
96 const std::string& sql) : | |
97 reference_(database.GetWrappedObject(), sql.c_str()) | |
98 { | |
99 } | |
100 | |
101 | |
102 Statement::Statement(Connection& database, | |
103 const char* sql) : | |
104 reference_(database.GetWrappedObject(), sql) | |
105 { | |
106 } | |
107 | |
108 | |
109 bool Statement::Run() | |
110 { | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
111 VLOG(1) << "SQLite::Statement::Run " << sqlite3_sql(GetStatement()); |
0 | 112 return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE; |
113 } | |
114 | |
115 bool Statement::Step() | |
116 { | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
117 VLOG(1) << "SQLite::Statement::Step " << sqlite3_sql(GetStatement()); |
0 | 118 return CheckError(sqlite3_step(GetStatement())) == SQLITE_ROW; |
119 } | |
120 | |
121 void Statement::Reset(bool clear_bound_vars) | |
122 { | |
123 // We don't call CheckError() here because sqlite3_reset() returns | |
124 // the last error that Step() caused thereby generating a second | |
125 // spurious error callback. | |
126 if (clear_bound_vars) | |
127 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
|
128 //VLOG(1) << "SQLite::Statement::Reset"; |
0 | 129 sqlite3_reset(GetStatement()); |
130 } | |
131 | |
132 std::string Statement::GetOriginalSQLStatement() | |
133 { | |
134 return std::string(sqlite3_sql(GetStatement())); | |
135 } | |
136 | |
137 | |
138 void Statement::BindNull(int col) | |
139 { | |
140 CheckOk(sqlite3_bind_null(GetStatement(), col + 1)); | |
141 } | |
142 | |
143 void Statement::BindBool(int col, bool val) | |
144 { | |
145 BindInt(col, val ? 1 : 0); | |
146 } | |
147 | |
148 void Statement::BindInt(int col, int val) | |
149 { | |
150 CheckOk(sqlite3_bind_int(GetStatement(), col + 1, val)); | |
151 } | |
152 | |
153 void Statement::BindInt64(int col, int64_t val) | |
154 { | |
155 CheckOk(sqlite3_bind_int64(GetStatement(), col + 1, val)); | |
156 } | |
157 | |
158 void Statement::BindDouble(int col, double val) | |
159 { | |
160 CheckOk(sqlite3_bind_double(GetStatement(), col + 1, val)); | |
161 } | |
162 | |
163 void Statement::BindCString(int col, const char* val) | |
164 { | |
165 CheckOk(sqlite3_bind_text(GetStatement(), col + 1, val, -1, SQLITE_TRANSIENT)); | |
166 } | |
167 | |
168 void Statement::BindString(int col, const std::string& val) | |
169 { | |
170 CheckOk(sqlite3_bind_text(GetStatement(), | |
171 col + 1, | |
172 val.data(), | |
173 val.size(), | |
174 SQLITE_TRANSIENT)); | |
175 } | |
176 | |
177 /*void Statement::BindString16(int col, const string16& value) | |
178 { | |
179 BindString(col, UTF16ToUTF8(value)); | |
180 }*/ | |
181 | |
182 void Statement::BindBlob(int col, const void* val, int val_len) | |
183 { | |
184 CheckOk(sqlite3_bind_blob(GetStatement(), col + 1, val, val_len, SQLITE_TRANSIENT)); | |
185 } | |
186 | |
187 | |
188 int Statement::ColumnCount() const | |
189 { | |
190 return sqlite3_column_count(GetStatement()); | |
191 } | |
192 | |
193 | |
194 ColumnType Statement::GetColumnType(int col) const | |
195 { | |
196 // Verify that our enum matches sqlite's values. | |
197 assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER); | |
198 assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT); | |
199 assert(COLUMN_TYPE_TEXT == SQLITE_TEXT); | |
200 assert(COLUMN_TYPE_BLOB == SQLITE_BLOB); | |
201 assert(COLUMN_TYPE_NULL == SQLITE_NULL); | |
202 | |
203 return static_cast<ColumnType>(sqlite3_column_type(GetStatement(), col)); | |
204 } | |
205 | |
206 ColumnType Statement::GetDeclaredColumnType(int col) const | |
207 { | |
208 std::string column_type(sqlite3_column_decltype(GetStatement(), col)); | |
209 Toolbox::ToLowerCase(column_type); | |
210 | |
211 if (column_type == "integer") | |
212 return COLUMN_TYPE_INTEGER; | |
213 else if (column_type == "float") | |
214 return COLUMN_TYPE_FLOAT; | |
215 else if (column_type == "text") | |
216 return COLUMN_TYPE_TEXT; | |
217 else if (column_type == "blob") | |
218 return COLUMN_TYPE_BLOB; | |
219 | |
220 return COLUMN_TYPE_NULL; | |
221 } | |
222 | |
80 | 223 bool Statement::ColumnIsNull(int col) const |
224 { | |
225 return sqlite3_column_type(GetStatement(), col) == SQLITE_NULL; | |
226 } | |
227 | |
0 | 228 bool Statement::ColumnBool(int col) const |
229 { | |
230 return !!ColumnInt(col); | |
231 } | |
232 | |
233 int Statement::ColumnInt(int col) const | |
234 { | |
235 return sqlite3_column_int(GetStatement(), col); | |
236 } | |
237 | |
238 int64_t Statement::ColumnInt64(int col) const | |
239 { | |
240 return sqlite3_column_int64(GetStatement(), col); | |
241 } | |
242 | |
243 double Statement::ColumnDouble(int col) const | |
244 { | |
245 return sqlite3_column_double(GetStatement(), col); | |
246 } | |
247 | |
248 std::string Statement::ColumnString(int col) const | |
249 { | |
250 const char* str = reinterpret_cast<const char*>( | |
251 sqlite3_column_text(GetStatement(), col)); | |
252 int len = sqlite3_column_bytes(GetStatement(), col); | |
253 | |
254 std::string result; | |
255 if (str && len > 0) | |
256 result.assign(str, len); | |
257 return result; | |
258 } | |
259 | |
260 /*string16 Statement::ColumnString16(int col) const | |
261 { | |
262 std::string s = ColumnString(col); | |
263 return !s.empty() ? UTF8ToUTF16(s) : string16(); | |
264 }*/ | |
265 | |
266 int Statement::ColumnByteLength(int col) const | |
267 { | |
268 return sqlite3_column_bytes(GetStatement(), col); | |
269 } | |
270 | |
271 const void* Statement::ColumnBlob(int col) const | |
272 { | |
273 return sqlite3_column_blob(GetStatement(), col); | |
274 } | |
275 | |
276 bool Statement::ColumnBlobAsString(int col, std::string* blob) | |
277 { | |
278 const void* p = ColumnBlob(col); | |
279 size_t len = ColumnByteLength(col); | |
280 blob->resize(len); | |
281 if (blob->size() != len) { | |
282 return false; | |
283 } | |
284 blob->assign(reinterpret_cast<const char*>(p), len); | |
285 return true; | |
286 } | |
287 | |
288 /*bool Statement::ColumnBlobAsString16(int col, string16* val) const | |
289 { | |
290 const void* data = ColumnBlob(col); | |
291 size_t len = ColumnByteLength(col) / sizeof(char16); | |
292 val->resize(len); | |
293 if (val->size() != len) | |
294 return false; | |
295 val->assign(reinterpret_cast<const char16*>(data), len); | |
296 return true; | |
297 }*/ | |
298 | |
724 | 299 /*bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const |
0 | 300 { |
301 val->clear(); | |
302 | |
303 const void* data = sqlite3_column_blob(GetStatement(), col); | |
304 int len = sqlite3_column_bytes(GetStatement(), col); | |
305 if (data && len > 0) { | |
306 val->resize(len); | |
307 memcpy(&(*val)[0], data, len); | |
308 } | |
309 return true; | |
724 | 310 }*/ |
0 | 311 |
724 | 312 /*bool Statement::ColumnBlobAsVector( |
0 | 313 int col, |
314 std::vector<unsigned char>* val) const | |
315 { | |
316 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | |
724 | 317 }*/ |
0 | 318 |
319 } | |
320 } |