[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 6 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | a4bbc1f9 | 2013-06-11 07:28:19 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 13 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 14 | |
| 15 | namespace sql { |
| 16 | |
| 17 | // This empty constructor initializes our reference with an empty one so that |
| 18 | // we don't have to NULL-check the ref_ to see if the statement is valid: we |
| 19 | // only have to check the ref's validity bit. |
| 20 | Statement::Statement() |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 21 | : ref_(new Connection::StatementRef(NULL, NULL, false)), |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 22 | stepped_(false), |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 23 | succeeded_(false) { |
| 24 | } |
| 25 | |
| 26 | Statement::Statement(scoped_refptr<Connection::StatementRef> ref) |
| 27 | : ref_(ref), |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 28 | stepped_(false), |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 29 | succeeded_(false) { |
| 30 | } |
| 31 | |
| 32 | Statement::~Statement() { |
| 33 | // Free the resources associated with this statement. We assume there's only |
| 34 | // one statement active for a given sqlite3_stmt at any time, so this won't |
| 35 | // mess with anything. |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 36 | Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) { |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 40 | Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 41 | ref_ = ref; |
| 42 | } |
| 43 | |
[email protected] | 85fc27b0 | 2012-02-17 02:15:09 | [diff] [blame] | 44 | void Statement::Clear() { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 45 | Assign(new Connection::StatementRef(NULL, NULL, false)); |
[email protected] | 85fc27b0 | 2012-02-17 02:15:09 | [diff] [blame] | 46 | succeeded_ = false; |
| 47 | } |
| 48 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 49 | bool Statement::CheckValid() const { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 50 | // Allow operations to fail silently if a statement was invalidated |
| 51 | // because the database was closed by an error handler. |
| 52 | DLOG_IF(FATAL, !ref_->was_valid()) |
| 53 | << "Cannot call mutating statements on an invalid statement."; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 54 | return is_valid(); |
| 55 | } |
| 56 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 57 | int Statement::StepInternal(bool timer_flag) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 58 | ref_->AssertIOAllowed(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 59 | if (!CheckValid()) |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 60 | return SQLITE_ERROR; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 61 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 62 | const bool was_stepped = stepped_; |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 63 | stepped_ = true; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 64 | int ret = SQLITE_ERROR; |
| 65 | if (!ref_->connection()) { |
| 66 | ret = sqlite3_step(ref_->stmt()); |
| 67 | } else { |
| 68 | if (!timer_flag) { |
| 69 | ret = sqlite3_step(ref_->stmt()); |
| 70 | } else { |
| 71 | const base::TimeTicks before = ref_->connection()->Now(); |
| 72 | ret = sqlite3_step(ref_->stmt()); |
| 73 | const base::TimeTicks after = ref_->connection()->Now(); |
| 74 | const bool read_only = !!sqlite3_stmt_readonly(ref_->stmt()); |
| 75 | ref_->connection()->RecordTimeAndChanges(after - before, read_only); |
| 76 | } |
| 77 | |
| 78 | if (!was_stepped) |
| 79 | ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_RUN); |
| 80 | |
| 81 | if (ret == SQLITE_ROW) |
| 82 | ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_ROWS); |
| 83 | } |
| 84 | return CheckError(ret); |
| 85 | } |
| 86 | |
| 87 | bool Statement::Run() { |
| 88 | DCHECK(!stepped_); |
| 89 | return StepInternal(true) == SQLITE_DONE; |
| 90 | } |
| 91 | |
| 92 | bool Statement::RunWithoutTimers() { |
| 93 | DCHECK(!stepped_); |
| 94 | return StepInternal(false) == SQLITE_DONE; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool Statement::Step() { |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 98 | return StepInternal(true) == SQLITE_ROW; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 101 | void Statement::Reset(bool clear_bound_vars) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 102 | ref_->AssertIOAllowed(); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 103 | if (is_valid()) { |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 104 | if (clear_bound_vars) |
| 105 | sqlite3_clear_bindings(ref_->stmt()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 106 | |
| 107 | // StepInternal() cannot track success because statements may be reset |
| 108 | // before reaching SQLITE_DONE. Don't call CheckError() because |
| 109 | // sqlite3_reset() returns the last step error, which StepInternal() already |
| 110 | // checked. |
| 111 | const int rc =sqlite3_reset(ref_->stmt()); |
| 112 | if (rc == SQLITE_OK && ref_->connection()) |
| 113 | ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 114 | } |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 115 | |
shess | 7dbd4dee | 2015-10-06 17:39:16 | [diff] [blame] | 116 | // Potentially release dirty cache pages if an autocommit statement made |
| 117 | // changes. |
| 118 | if (ref_->connection()) |
| 119 | ref_->connection()->ReleaseCacheMemoryIfNeeded(false); |
| 120 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 121 | succeeded_ = false; |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 122 | stepped_ = false; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool Statement::Succeeded() const { |
| 126 | if (!is_valid()) |
| 127 | return false; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 128 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 129 | return succeeded_; |
| 130 | } |
| 131 | |
| 132 | bool Statement::BindNull(int col) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 133 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 134 | if (!is_valid()) |
| 135 | return false; |
| 136 | |
| 137 | return CheckOk(sqlite3_bind_null(ref_->stmt(), col + 1)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 140 | bool Statement::BindBool(int col, bool val) { |
| 141 | return BindInt(col, val ? 1 : 0); |
| 142 | } |
| 143 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 144 | bool Statement::BindInt(int col, int val) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 145 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 146 | if (!is_valid()) |
| 147 | return false; |
| 148 | |
| 149 | return CheckOk(sqlite3_bind_int(ref_->stmt(), col + 1, val)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 150 | } |
| 151 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 152 | bool Statement::BindInt64(int col, int64_t val) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 153 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 154 | if (!is_valid()) |
| 155 | return false; |
| 156 | |
| 157 | return CheckOk(sqlite3_bind_int64(ref_->stmt(), col + 1, val)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool Statement::BindDouble(int col, double val) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 161 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 162 | if (!is_valid()) |
| 163 | return false; |
| 164 | |
| 165 | return CheckOk(sqlite3_bind_double(ref_->stmt(), col + 1, val)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | bool Statement::BindCString(int col, const char* val) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 169 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 170 | if (!is_valid()) |
| 171 | return false; |
| 172 | |
| 173 | return CheckOk( |
| 174 | sqlite3_bind_text(ref_->stmt(), col + 1, val, -1, SQLITE_TRANSIENT)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool Statement::BindString(int col, const std::string& val) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 178 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 179 | if (!is_valid()) |
| 180 | return false; |
| 181 | |
| 182 | return CheckOk(sqlite3_bind_text(ref_->stmt(), |
| 183 | col + 1, |
| 184 | val.data(), |
| 185 | val.size(), |
| 186 | SQLITE_TRANSIENT)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 189 | bool Statement::BindString16(int col, const base::string16& value) { |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 190 | return BindString(col, base::UTF16ToUTF8(value)); |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 193 | bool Statement::BindBlob(int col, const void* val, int val_len) { |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 194 | DCHECK(!stepped_); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 195 | if (!is_valid()) |
| 196 | return false; |
| 197 | |
| 198 | return CheckOk( |
| 199 | sqlite3_bind_blob(ref_->stmt(), col + 1, val, val_len, SQLITE_TRANSIENT)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int Statement::ColumnCount() const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 203 | if (!is_valid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 204 | return 0; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 205 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 206 | return sqlite3_column_count(ref_->stmt()); |
| 207 | } |
| 208 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 209 | ColType Statement::ColumnType(int col) const { |
| 210 | // Verify that our enum matches sqlite's values. |
mostynb | ddbabe7 | 2015-01-06 00:20:50 | [diff] [blame] | 211 | static_assert(COLUMN_TYPE_INTEGER == SQLITE_INTEGER, "integer no match"); |
| 212 | static_assert(COLUMN_TYPE_FLOAT == SQLITE_FLOAT, "float no match"); |
| 213 | static_assert(COLUMN_TYPE_TEXT == SQLITE_TEXT, "integer no match"); |
| 214 | static_assert(COLUMN_TYPE_BLOB == SQLITE_BLOB, "blob no match"); |
| 215 | static_assert(COLUMN_TYPE_NULL == SQLITE_NULL, "null no match"); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 216 | |
| 217 | return static_cast<ColType>(sqlite3_column_type(ref_->stmt(), col)); |
| 218 | } |
| 219 | |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 220 | ColType Statement::DeclaredColumnType(int col) const { |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 221 | std::string column_type = base::ToLowerASCII( |
| 222 | sqlite3_column_decltype(ref_->stmt(), col)); |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 223 | |
| 224 | if (column_type == "integer") |
| 225 | return COLUMN_TYPE_INTEGER; |
| 226 | else if (column_type == "float") |
| 227 | return COLUMN_TYPE_FLOAT; |
| 228 | else if (column_type == "text") |
| 229 | return COLUMN_TYPE_TEXT; |
| 230 | else if (column_type == "blob") |
| 231 | return COLUMN_TYPE_BLOB; |
| 232 | |
| 233 | return COLUMN_TYPE_NULL; |
| 234 | } |
| 235 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 236 | bool Statement::ColumnBool(int col) const { |
| 237 | return !!ColumnInt(col); |
| 238 | } |
| 239 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 240 | int Statement::ColumnInt(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 241 | if (!CheckValid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 242 | return 0; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 243 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 244 | return sqlite3_column_int(ref_->stmt(), col); |
| 245 | } |
| 246 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 247 | int64_t Statement::ColumnInt64(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 248 | if (!CheckValid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 249 | return 0; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 250 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 251 | return sqlite3_column_int64(ref_->stmt(), col); |
| 252 | } |
| 253 | |
| 254 | double Statement::ColumnDouble(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 255 | if (!CheckValid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 256 | return 0; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 257 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 258 | return sqlite3_column_double(ref_->stmt(), col); |
| 259 | } |
| 260 | |
| 261 | std::string Statement::ColumnString(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 262 | if (!CheckValid()) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 263 | return std::string(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 264 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 265 | const char* str = reinterpret_cast<const char*>( |
| 266 | sqlite3_column_text(ref_->stmt(), col)); |
| 267 | int len = sqlite3_column_bytes(ref_->stmt(), col); |
| 268 | |
| 269 | std::string result; |
| 270 | if (str && len > 0) |
| 271 | result.assign(str, len); |
| 272 | return result; |
| 273 | } |
| 274 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 275 | base::string16 Statement::ColumnString16(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 276 | if (!CheckValid()) |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 277 | return base::string16(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 278 | |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 279 | std::string s = ColumnString(col); |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 280 | return !s.empty() ? base::UTF8ToUTF16(s) : base::string16(); |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 283 | int Statement::ColumnByteLength(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 284 | if (!CheckValid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 285 | return 0; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 286 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 287 | return sqlite3_column_bytes(ref_->stmt(), col); |
| 288 | } |
| 289 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 290 | const void* Statement::ColumnBlob(int col) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 291 | if (!CheckValid()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 292 | return NULL; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 293 | |
| 294 | return sqlite3_column_blob(ref_->stmt(), col); |
| 295 | } |
| 296 | |
vabr | b194fc56 | 2016-07-13 08:46:37 | [diff] [blame] | 297 | bool Statement::ColumnBlobAsString(int col, std::string* blob) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 298 | if (!CheckValid()) |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 299 | return false; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 300 | |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 301 | const void* p = ColumnBlob(col); |
| 302 | size_t len = ColumnByteLength(col); |
| 303 | blob->resize(len); |
| 304 | if (blob->size() != len) { |
| 305 | return false; |
| 306 | } |
| 307 | blob->assign(reinterpret_cast<const char*>(p), len); |
| 308 | return true; |
| 309 | } |
| 310 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 311 | bool Statement::ColumnBlobAsString16(int col, base::string16* val) const { |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 312 | if (!CheckValid()) |
| 313 | return false; |
| 314 | |
| 315 | const void* data = ColumnBlob(col); |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 316 | size_t len = ColumnByteLength(col) / sizeof(base::char16); |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 317 | val->resize(len); |
| 318 | if (val->size() != len) |
| 319 | return false; |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 320 | val->assign(reinterpret_cast<const base::char16*>(data), len); |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 324 | bool Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 325 | val->clear(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 326 | |
| 327 | if (!CheckValid()) |
| 328 | return false; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 329 | |
| 330 | const void* data = sqlite3_column_blob(ref_->stmt(), col); |
| 331 | int len = sqlite3_column_bytes(ref_->stmt(), col); |
| 332 | if (data && len > 0) { |
| 333 | val->resize(len); |
| 334 | memcpy(&(*val)[0], data, len); |
| 335 | } |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 336 | return true; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 337 | } |
| 338 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 339 | bool Statement::ColumnBlobAsVector( |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 340 | int col, |
| 341 | std::vector<unsigned char>* val) const { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 342 | return ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 345 | const char* Statement::GetSQLStatement() { |
| 346 | return sqlite3_sql(ref_->stmt()); |
| 347 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 348 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 349 | bool Statement::CheckOk(int err) const { |
[email protected] | bed29d94 | 2011-12-22 19:25:51 | [diff] [blame] | 350 | // Binding to a non-existent variable is evidence of a serious error. |
| 351 | // TODO(gbillock,shess): make this invalidate the statement so it |
| 352 | // can't wreak havoc. |
| 353 | if (err == SQLITE_RANGE) |
| 354 | DLOG(FATAL) << "Bind value out of range"; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 355 | return err == SQLITE_OK; |
| 356 | } |
| 357 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 358 | int Statement::CheckError(int err) { |
| 359 | // Please don't add DCHECKs here, OnSqliteError() already has them. |
| 360 | succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
[email protected] | 7131880 | 2013-06-03 00:20:05 | [diff] [blame] | 361 | if (!succeeded_ && ref_.get() && ref_->connection()) |
[email protected] | 2f496b4 | 2013-09-26 18:36:58 | [diff] [blame] | 362 | return ref_->connection()->OnSqliteError(err, this, NULL); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 363 | return err; |
| 364 | } |
| 365 | |
| 366 | } // namespace sql |