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