Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sql/statement.h" | 5 #include "sql/statement.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "third_party/sqlite/sqlite3.h" | 10 #include "third_party/sqlite/sqlite3.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool Statement::CheckValid() const { | 46 bool Statement::CheckValid() const { |
| 47 // Allow operations to fail silently if a statement was invalidated | 47 // Allow operations to fail silently if a statement was invalidated |
| 48 // because the database was closed by an error handler. | 48 // because the database was closed by an error handler. |
| 49 DLOG_IF(FATAL, !ref_->was_valid()) | 49 DLOG_IF(FATAL, !ref_->was_valid()) |
| 50 << "Cannot call mutating statements on an invalid statement."; | 50 << "Cannot call mutating statements on an invalid statement."; |
| 51 return is_valid(); | 51 return is_valid(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 int Statement::InnerStep(bool timer_flag) { | |
| 55 ref_->AssertIOAllowed(); | |
| 56 if (!CheckValid()) | |
| 57 return SQLITE_ERROR; | |
| 58 | |
| 59 const bool was_stepped = stepped_; | |
| 60 stepped_ = true; | |
| 61 const base::TimeTicks before = base::TimeTicks::Now(); | |
|
rmcilroy
2015/05/21 22:54:48
You should probably only call base::TimerTicks::No
Scott Hess - ex-Googler
2015/05/21 23:42:37
Ugh, that maybe makes the logic icky because of th
rmcilroy
2015/05/22 08:48:23
Jared put together a list showing the cost of vari
| |
| 62 int ret = sqlite3_step(ref_->stmt()); | |
| 63 const base::TimeDelta delta = base::TimeTicks::Now() - before; | |
| 64 if (ref_->connection()) { | |
| 65 if (timer_flag) { | |
| 66 const int read_only = sqlite3_stmt_readonly(ref_->stmt()); | |
| 67 ref_->connection()->ChangeHelper(delta, read_only); | |
| 68 } | |
| 69 | |
| 70 if (!was_stepped) | |
| 71 ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_RUN); | |
| 72 | |
| 73 if (ret == SQLITE_ROW) | |
| 74 ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_ROWS); | |
| 75 } | |
| 76 return CheckError(ret); | |
| 77 } | |
| 78 | |
| 54 bool Statement::Run() { | 79 bool Statement::Run() { |
| 55 DCHECK(!stepped_); | 80 DCHECK(!stepped_); |
| 56 ref_->AssertIOAllowed(); | 81 return InnerStep(true) == SQLITE_DONE; |
| 57 if (!CheckValid()) | 82 } |
| 58 return false; | |
| 59 | 83 |
| 60 stepped_ = true; | 84 bool Statement::RunWithoutTimers() { |
| 61 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; | 85 DCHECK(!stepped_); |
| 86 return InnerStep(false) == SQLITE_DONE; | |
| 62 } | 87 } |
| 63 | 88 |
| 64 bool Statement::Step() { | 89 bool Statement::Step() { |
| 65 ref_->AssertIOAllowed(); | 90 return InnerStep(true) == SQLITE_ROW; |
| 66 if (!CheckValid()) | |
| 67 return false; | |
| 68 | |
| 69 stepped_ = true; | |
| 70 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; | |
| 71 } | 91 } |
| 72 | 92 |
| 73 void Statement::Reset(bool clear_bound_vars) { | 93 void Statement::Reset(bool clear_bound_vars) { |
| 74 ref_->AssertIOAllowed(); | 94 ref_->AssertIOAllowed(); |
| 75 if (is_valid()) { | 95 if (is_valid()) { |
| 76 // We don't call CheckError() here because sqlite3_reset() returns | |
| 77 // the last error that Step() caused thereby generating a second | |
| 78 // spurious error callback. | |
| 79 if (clear_bound_vars) | 96 if (clear_bound_vars) |
| 80 sqlite3_clear_bindings(ref_->stmt()); | 97 sqlite3_clear_bindings(ref_->stmt()); |
| 81 sqlite3_reset(ref_->stmt()); | 98 |
| 99 // InnerStep() cannot track success because statements may be reset before | |
| 100 // reaching SQLITE_DONE. Don't call CheckError() because sqlite3_reset() | |
| 101 // returns the last step error, which InnerStep() already checked. | |
| 102 const int rc =sqlite3_reset(ref_->stmt()); | |
| 103 if (rc == SQLITE_OK && ref_->connection()) | |
| 104 ref_->connection()->RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS); | |
| 82 } | 105 } |
| 83 | 106 |
| 84 succeeded_ = false; | 107 succeeded_ = false; |
| 85 stepped_ = false; | 108 stepped_ = false; |
| 86 } | 109 } |
| 87 | 110 |
| 88 bool Statement::Succeeded() const { | 111 bool Statement::Succeeded() const { |
| 89 if (!is_valid()) | 112 if (!is_valid()) |
| 90 return false; | 113 return false; |
| 91 | 114 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 | 343 |
| 321 int Statement::CheckError(int err) { | 344 int Statement::CheckError(int err) { |
| 322 // Please don't add DCHECKs here, OnSqliteError() already has them. | 345 // Please don't add DCHECKs here, OnSqliteError() already has them. |
| 323 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 346 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
| 324 if (!succeeded_ && ref_.get() && ref_->connection()) | 347 if (!succeeded_ && ref_.get() && ref_->connection()) |
| 325 return ref_->connection()->OnSqliteError(err, this, NULL); | 348 return ref_->connection()->OnSqliteError(err, this, NULL); |
| 326 return err; | 349 return err; |
| 327 } | 350 } |
| 328 | 351 |
| 329 } // namespace sql | 352 } // namespace sql |
| OLD | NEW |