Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/Statement.cpp @ 5044:6fed78e13233
Refactored DicomMap to handle sequences when needed
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 28 Jun 2022 17:45:09 +0200 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
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 * |
3063 | 4 * Copyright (C) 2012-2016 Sebastien Jodogne <s.jodogne@orthanc-labs.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 |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
7 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 8 * |
17 | 9 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
10 * | |
11 * Redistribution and use in source and binary forms, with or without | |
12 * modification, are permitted provided that the following conditions are | |
13 * met: | |
0 | 14 * |
17 | 15 * * Redistributions of source code must retain the above copyright |
16 * notice, this list of conditions and the following disclaimer. | |
17 * * Redistributions in binary form must reproduce the above | |
18 * copyright notice, this list of conditions and the following disclaimer | |
19 * in the documentation and/or other materials provided with the | |
20 * distribution. | |
21 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
22 * nor the names of its contributors may be used to endorse or promote | |
23 * products derived from this software without specific prior written | |
24 * permission. | |
25 * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 37 **/ |
38 | |
39 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 #if ORTHANC_SQLITE_STANDALONE != 1 |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
724
diff
changeset
|
41 #include "../PrecompiledHeaders.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 #endif |
0 | 43 |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 #include "Statement.h" |
0 | 45 #include "Connection.h" |
46 | |
47 #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
|
48 #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
|
49 #include <algorithm> |
0 | 50 |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
51 #if (ORTHANC_SQLITE_STANDALONE == 1) |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
52 // Trace logging is disabled if this SQLite wrapper is used |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
53 // independently of Orthanc |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
54 # define LOG_CREATE(message); |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
55 # define LOG_APPLY(message); |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
56 #elif defined(NDEBUG) |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
57 // Trace logging is disabled in release builds |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
58 # include "../Logging.h" |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
59 # define LOG_CREATE(message); |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
60 # define LOG_APPLY(message); |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
61 #else |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
62 // Trace logging is enabled in debug builds |
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
63 # include "../Logging.h" |
4269
c7bd2f21ccc3
new macro CLOG, and sharing more code between logging engines
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4268
diff
changeset
|
64 # define LOG_CREATE(message) CLOG(TRACE, SQLITE) << "SQLite::Statement create: " << message; |
c7bd2f21ccc3
new macro CLOG, and sharing more code between logging engines
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4268
diff
changeset
|
65 # define LOG_APPLY(message); // CLOG(TRACE, SQLITE) << "SQLite::Statement apply: " << message; |
1226 | 66 #endif |
67 | |
1474
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
68 #include "sqlite3.h" |
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
69 |
1222
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
70 #if defined(_MSC_VER) |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
71 #define snprintf _snprintf |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
72 #endif |
410c27e04a23
fix visual studio buil
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1221
diff
changeset
|
73 |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
74 |
59 | 75 namespace Orthanc |
0 | 76 { |
77 namespace SQLite | |
78 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
79 int Statement::CheckError(int err, ErrorCode code) const |
0 | 80 { |
81 bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | |
82 if (!succeeded) | |
83 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
84 #if ORTHANC_SQLITE_STANDALONE != 1 |
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); |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
87 LOG(ERROR) << buffer; |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
88 #endif |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
89 |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
90 throw OrthancSQLiteException(code); |
0 | 91 } |
92 | |
93 return err; | |
94 } | |
95 | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
96 void Statement::CheckOk(int err, ErrorCode code) const |
0 | 97 { |
98 if (err == SQLITE_RANGE) | |
99 { | |
100 // Binding to a non-existent variable is evidence of a serious error. | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
101 throw OrthancSQLiteException(ErrorCode_SQLiteBindOutOfRange); |
0 | 102 } |
103 else if (err != SQLITE_OK) | |
104 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
105 #if ORTHANC_SQLITE_STANDALONE != 1 |
1221
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
106 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
|
107 snprintf(buffer, sizeof(buffer) - 1, "SQLite error code %d", err); |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
108 LOG(ERROR) << buffer; |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
109 #endif |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
110 |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
111 throw OrthancSQLiteException(code); |
0 | 112 } |
113 } | |
114 | |
115 | |
116 Statement::Statement(Connection& database, | |
117 const StatementId& id, | |
118 const std::string& sql) : | |
119 reference_(database.GetCachedStatement(id, sql.c_str())) | |
120 { | |
121 Reset(true); | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
122 LOG_CREATE(sql); |
0 | 123 } |
124 | |
125 | |
126 Statement::Statement(Connection& database, | |
127 const StatementId& id, | |
128 const char* sql) : | |
129 reference_(database.GetCachedStatement(id, sql)) | |
130 { | |
131 Reset(true); | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
132 LOG_CREATE(sql); |
0 | 133 } |
134 | |
4304 | 135 Statement::~Statement() |
136 { | |
137 Reset(); | |
138 } | |
139 | |
0 | 140 |
141 Statement::Statement(Connection& database, | |
142 const std::string& sql) : | |
143 reference_(database.GetWrappedObject(), sql.c_str()) | |
144 { | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
145 LOG_CREATE(sql); |
0 | 146 } |
147 | |
148 | |
149 Statement::Statement(Connection& database, | |
150 const char* sql) : | |
151 reference_(database.GetWrappedObject(), sql) | |
152 { | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
153 LOG_CREATE(sql); |
0 | 154 } |
155 | |
156 | |
157 bool Statement::Run() | |
158 { | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
159 LOG_APPLY(sqlite3_sql(GetStatement())); |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
160 |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
161 return CheckError(sqlite3_step(GetStatement()), ErrorCode_SQLiteCannotRun) == SQLITE_DONE; |
0 | 162 } |
163 | |
164 bool Statement::Step() | |
165 { | |
2588
1f7b459b247b
improved trace logging in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
166 LOG_APPLY(sqlite3_sql(GetStatement())); |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
167 |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
168 return CheckError(sqlite3_step(GetStatement()), ErrorCode_SQLiteCannotStep) == SQLITE_ROW; |
0 | 169 } |
170 | |
171 void Statement::Reset(bool clear_bound_vars) | |
172 { | |
173 // We don't call CheckError() here because sqlite3_reset() returns | |
174 // the last error that Step() caused thereby generating a second | |
175 // spurious error callback. | |
176 if (clear_bound_vars) | |
177 sqlite3_clear_bindings(GetStatement()); | |
4269
c7bd2f21ccc3
new macro CLOG, and sharing more code between logging engines
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4268
diff
changeset
|
178 //CLOG(TRACE, SQLITE) << "SQLite::Statement::Reset"; |
0 | 179 sqlite3_reset(GetStatement()); |
180 } | |
181 | |
182 std::string Statement::GetOriginalSQLStatement() | |
183 { | |
184 return std::string(sqlite3_sql(GetStatement())); | |
185 } | |
186 | |
187 | |
188 void Statement::BindNull(int col) | |
189 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
190 CheckOk(sqlite3_bind_null(GetStatement(), col + 1), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
191 ErrorCode_BadParameterType); |
0 | 192 } |
193 | |
194 void Statement::BindBool(int col, bool val) | |
195 { | |
196 BindInt(col, val ? 1 : 0); | |
197 } | |
198 | |
199 void Statement::BindInt(int col, int val) | |
200 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
201 CheckOk(sqlite3_bind_int(GetStatement(), col + 1, val), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
202 ErrorCode_BadParameterType); |
0 | 203 } |
204 | |
205 void Statement::BindInt64(int col, int64_t val) | |
206 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
207 CheckOk(sqlite3_bind_int64(GetStatement(), col + 1, val), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
208 ErrorCode_BadParameterType); |
0 | 209 } |
210 | |
211 void Statement::BindDouble(int col, double val) | |
212 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
213 CheckOk(sqlite3_bind_double(GetStatement(), col + 1, val), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
214 ErrorCode_BadParameterType); |
0 | 215 } |
216 | |
217 void Statement::BindCString(int col, const char* val) | |
218 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
219 CheckOk(sqlite3_bind_text(GetStatement(), col + 1, val, -1, SQLITE_TRANSIENT), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
220 ErrorCode_BadParameterType); |
0 | 221 } |
222 | |
223 void Statement::BindString(int col, const std::string& val) | |
224 { | |
225 CheckOk(sqlite3_bind_text(GetStatement(), | |
226 col + 1, | |
227 val.data(), | |
3585
113a9643e8bb
Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3063
diff
changeset
|
228 static_cast<int>(val.size()), |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
229 SQLITE_TRANSIENT), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
230 ErrorCode_BadParameterType); |
0 | 231 } |
232 | |
233 /*void Statement::BindString16(int col, const string16& value) | |
234 { | |
235 BindString(col, UTF16ToUTF8(value)); | |
236 }*/ | |
237 | |
238 void Statement::BindBlob(int col, const void* val, int val_len) | |
239 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
240 CheckOk(sqlite3_bind_blob(GetStatement(), col + 1, val, val_len, SQLITE_TRANSIENT), |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
241 ErrorCode_BadParameterType); |
0 | 242 } |
243 | |
244 | |
245 int Statement::ColumnCount() const | |
246 { | |
247 return sqlite3_column_count(GetStatement()); | |
248 } | |
249 | |
250 | |
251 ColumnType Statement::GetColumnType(int col) const | |
252 { | |
253 // Verify that our enum matches sqlite's values. | |
254 assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER); | |
255 assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT); | |
256 assert(COLUMN_TYPE_TEXT == SQLITE_TEXT); | |
257 assert(COLUMN_TYPE_BLOB == SQLITE_BLOB); | |
258 assert(COLUMN_TYPE_NULL == SQLITE_NULL); | |
259 | |
260 return static_cast<ColumnType>(sqlite3_column_type(GetStatement(), col)); | |
261 } | |
262 | |
263 ColumnType Statement::GetDeclaredColumnType(int col) const | |
264 { | |
265 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
|
266 std::transform(column_type.begin(), column_type.end(), column_type.begin(), tolower); |
0 | 267 |
268 if (column_type == "integer") | |
269 return COLUMN_TYPE_INTEGER; | |
270 else if (column_type == "float") | |
271 return COLUMN_TYPE_FLOAT; | |
272 else if (column_type == "text") | |
273 return COLUMN_TYPE_TEXT; | |
274 else if (column_type == "blob") | |
275 return COLUMN_TYPE_BLOB; | |
276 | |
277 return COLUMN_TYPE_NULL; | |
278 } | |
279 | |
80 | 280 bool Statement::ColumnIsNull(int col) const |
281 { | |
282 return sqlite3_column_type(GetStatement(), col) == SQLITE_NULL; | |
283 } | |
284 | |
0 | 285 bool Statement::ColumnBool(int col) const |
286 { | |
287 return !!ColumnInt(col); | |
288 } | |
289 | |
290 int Statement::ColumnInt(int col) const | |
291 { | |
292 return sqlite3_column_int(GetStatement(), col); | |
293 } | |
294 | |
295 int64_t Statement::ColumnInt64(int col) const | |
296 { | |
297 return sqlite3_column_int64(GetStatement(), col); | |
298 } | |
299 | |
300 double Statement::ColumnDouble(int col) const | |
301 { | |
302 return sqlite3_column_double(GetStatement(), col); | |
303 } | |
304 | |
305 std::string Statement::ColumnString(int col) const | |
306 { | |
307 const char* str = reinterpret_cast<const char*>( | |
308 sqlite3_column_text(GetStatement(), col)); | |
309 int len = sqlite3_column_bytes(GetStatement(), col); | |
310 | |
311 std::string result; | |
312 if (str && len > 0) | |
313 result.assign(str, len); | |
314 return result; | |
315 } | |
316 | |
317 /*string16 Statement::ColumnString16(int col) const | |
318 { | |
319 std::string s = ColumnString(col); | |
320 return !s.empty() ? UTF8ToUTF16(s) : string16(); | |
321 }*/ | |
322 | |
323 int Statement::ColumnByteLength(int col) const | |
324 { | |
325 return sqlite3_column_bytes(GetStatement(), col); | |
326 } | |
327 | |
328 const void* Statement::ColumnBlob(int col) const | |
329 { | |
330 return sqlite3_column_blob(GetStatement(), col); | |
331 } | |
332 | |
333 bool Statement::ColumnBlobAsString(int col, std::string* blob) | |
334 { | |
335 const void* p = ColumnBlob(col); | |
336 size_t len = ColumnByteLength(col); | |
337 blob->resize(len); | |
338 if (blob->size() != len) { | |
339 return false; | |
340 } | |
341 blob->assign(reinterpret_cast<const char*>(p), len); | |
342 return true; | |
343 } | |
344 | |
345 /*bool Statement::ColumnBlobAsString16(int col, string16* val) const | |
346 { | |
347 const void* data = ColumnBlob(col); | |
348 size_t len = ColumnByteLength(col) / sizeof(char16); | |
349 val->resize(len); | |
350 if (val->size() != len) | |
351 return false; | |
352 val->assign(reinterpret_cast<const char16*>(data), len); | |
353 return true; | |
354 }*/ | |
355 | |
724 | 356 /*bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const |
0 | 357 { |
358 val->clear(); | |
359 | |
360 const void* data = sqlite3_column_blob(GetStatement(), col); | |
361 int len = sqlite3_column_bytes(GetStatement(), col); | |
362 if (data && len > 0) { | |
363 val->resize(len); | |
364 memcpy(&(*val)[0], data, len); | |
365 } | |
366 return true; | |
724 | 367 }*/ |
0 | 368 |
724 | 369 /*bool Statement::ColumnBlobAsVector( |
0 | 370 int col, |
371 std::vector<unsigned char>* val) const | |
372 { | |
373 return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | |
724 | 374 }*/ |
0 | 375 |
376 } | |
377 } |