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