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::StepInternal(bool timer_flag) { |
| 55 ref_->AssertIOAllowed(); |
| 56 if (!CheckValid()) |
| 57 return SQLITE_ERROR; |
| 58 |
| 59 const bool was_stepped = stepped_; |
| 60 stepped_ = true; |
| 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 |
54 bool Statement::Run() { | 84 bool Statement::Run() { |
55 DCHECK(!stepped_); | 85 DCHECK(!stepped_); |
56 ref_->AssertIOAllowed(); | 86 return StepInternal(true) == SQLITE_DONE; |
57 if (!CheckValid()) | 87 } |
58 return false; | |
59 | 88 |
60 stepped_ = true; | 89 bool Statement::RunWithoutTimers() { |
61 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; | 90 DCHECK(!stepped_); |
| 91 return StepInternal(false) == SQLITE_DONE; |
62 } | 92 } |
63 | 93 |
64 bool Statement::Step() { | 94 bool Statement::Step() { |
65 ref_->AssertIOAllowed(); | 95 return StepInternal(true) == SQLITE_ROW; |
66 if (!CheckValid()) | |
67 return false; | |
68 | |
69 stepped_ = true; | |
70 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; | |
71 } | 96 } |
72 | 97 |
73 void Statement::Reset(bool clear_bound_vars) { | 98 void Statement::Reset(bool clear_bound_vars) { |
74 ref_->AssertIOAllowed(); | 99 ref_->AssertIOAllowed(); |
75 if (is_valid()) { | 100 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) | 101 if (clear_bound_vars) |
80 sqlite3_clear_bindings(ref_->stmt()); | 102 sqlite3_clear_bindings(ref_->stmt()); |
81 sqlite3_reset(ref_->stmt()); | 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); |
82 } | 111 } |
83 | 112 |
84 succeeded_ = false; | 113 succeeded_ = false; |
85 stepped_ = false; | 114 stepped_ = false; |
86 } | 115 } |
87 | 116 |
88 bool Statement::Succeeded() const { | 117 bool Statement::Succeeded() const { |
89 if (!is_valid()) | 118 if (!is_valid()) |
90 return false; | 119 return false; |
91 | 120 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 349 |
321 int Statement::CheckError(int err) { | 350 int Statement::CheckError(int err) { |
322 // Please don't add DCHECKs here, OnSqliteError() already has them. | 351 // Please don't add DCHECKs here, OnSqliteError() already has them. |
323 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 352 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
324 if (!succeeded_ && ref_.get() && ref_->connection()) | 353 if (!succeeded_ && ref_.get() && ref_->connection()) |
325 return ref_->connection()->OnSqliteError(err, this, NULL); | 354 return ref_->connection()->OnSqliteError(err, this, NULL); |
326 return err; | 355 return err; |
327 } | 356 } |
328 | 357 |
329 } // namespace sql | 358 } // namespace sql |
OLD | NEW |