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