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/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "third_party/sqlite/sqlite3.h" | 10 #include "third_party/sqlite/sqlite3.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 Statement::Statement(scoped_refptr<Connection::StatementRef> ref) | 22 Statement::Statement(scoped_refptr<Connection::StatementRef> ref) |
23 : ref_(ref), | 23 : ref_(ref), |
24 succeeded_(false) { | 24 succeeded_(false) { |
25 } | 25 } |
26 | 26 |
27 Statement::~Statement() { | 27 Statement::~Statement() { |
28 // Free the resources associated with this statement. We assume there's only | 28 // Free the resources associated with this statement. We assume there's only |
29 // one statement active for a given sqlite3_stmt at any time, so this won't | 29 // one statement active for a given sqlite3_stmt at any time, so this won't |
30 // mess with anything. | 30 // mess with anything. |
31 Reset(); | 31 Reset(true); |
32 } | 32 } |
33 | 33 |
34 void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) { | 34 void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) { |
35 Reset(); | 35 Reset(true); |
36 ref_ = ref; | 36 ref_ = ref; |
37 } | 37 } |
38 | 38 |
39 void Statement::Clear() { | 39 void Statement::Clear() { |
40 Assign(new Connection::StatementRef); | 40 Assign(new Connection::StatementRef); |
41 succeeded_ = false; | 41 succeeded_ = false; |
42 } | 42 } |
43 | 43 |
44 bool Statement::CheckValid() const { | 44 bool Statement::CheckValid() const { |
45 if (!is_valid()) | 45 if (!is_valid()) |
46 DLOG(FATAL) << "Cannot call mutating statements on an invalid statement."; | 46 DLOG(FATAL) << "Cannot call mutating statements on an invalid statement."; |
47 return is_valid(); | 47 return is_valid(); |
48 } | 48 } |
49 | 49 |
50 bool Statement::Run() { | 50 bool Statement::Run() { |
51 if (!CheckValid()) | 51 if (!CheckValid()) |
52 return false; | 52 return false; |
53 | 53 |
54 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; | 54 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; |
55 } | 55 } |
56 | 56 |
57 bool Statement::Step() { | 57 bool Statement::Step() { |
58 if (!CheckValid()) | 58 if (!CheckValid()) |
59 return false; | 59 return false; |
60 | 60 |
61 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; | 61 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; |
62 } | 62 } |
63 | 63 |
64 void Statement::Reset() { | 64 void Statement::Reset(bool clear_bound_vars) { |
65 if (is_valid()) { | 65 if (is_valid()) { |
66 // We don't call CheckError() here because sqlite3_reset() returns | 66 // We don't call CheckError() here because sqlite3_reset() returns |
67 // the last error that Step() caused thereby generating a second | 67 // the last error that Step() caused thereby generating a second |
68 // spurious error callback. | 68 // spurious error callback. |
69 sqlite3_clear_bindings(ref_->stmt()); | 69 if (clear_bound_vars) |
| 70 sqlite3_clear_bindings(ref_->stmt()); |
70 sqlite3_reset(ref_->stmt()); | 71 sqlite3_reset(ref_->stmt()); |
71 } | 72 } |
72 | 73 |
73 succeeded_ = false; | 74 succeeded_ = false; |
74 } | 75 } |
75 | 76 |
76 bool Statement::Succeeded() const { | 77 bool Statement::Succeeded() const { |
77 if (!is_valid()) | 78 if (!is_valid()) |
78 return false; | 79 return false; |
79 | 80 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 302 |
302 int Statement::CheckError(int err) { | 303 int Statement::CheckError(int err) { |
303 // Please don't add DCHECKs here, OnSqliteError() already has them. | 304 // Please don't add DCHECKs here, OnSqliteError() already has them. |
304 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 305 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
305 if (!succeeded_ && is_valid()) | 306 if (!succeeded_ && is_valid()) |
306 return ref_->connection()->OnSqliteError(err, this); | 307 return ref_->connection()->OnSqliteError(err, this); |
307 return err; | 308 return err; |
308 } | 309 } |
309 | 310 |
310 } // namespace sql | 311 } // namespace sql |
OLD | NEW |