OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/sql/statement.h" | 5 #include "app/sql/statement.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "third_party/sqlite/preprocessed/sqlite3.h" | 8 #include "third_party/sqlite/preprocessed/sqlite3.h" |
9 | 9 |
10 namespace sql { | 10 namespace sql { |
(...skipping 28 matching lines...) Expand all Loading... |
39 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; | 39 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; |
40 } | 40 } |
41 | 41 |
42 bool Statement::Step() { | 42 bool Statement::Step() { |
43 if (!is_valid()) | 43 if (!is_valid()) |
44 return false; | 44 return false; |
45 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; | 45 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW; |
46 } | 46 } |
47 | 47 |
48 void Statement::Reset() { | 48 void Statement::Reset() { |
49 if (is_valid()) | 49 if (is_valid()) { |
50 CheckError(sqlite3_reset(ref_->stmt())); | 50 // We don't call CheckError() here because sqlite3_reset() returns |
| 51 // the last error that Step() caused thereby generating a second |
| 52 // spurious error callback. |
| 53 sqlite3_clear_bindings(ref_->stmt()); |
| 54 sqlite3_reset(ref_->stmt()); |
| 55 } |
51 succeeded_ = false; | 56 succeeded_ = false; |
52 } | 57 } |
53 | 58 |
54 bool Statement::Succeeded() const { | 59 bool Statement::Succeeded() const { |
55 if (!is_valid()) | 60 if (!is_valid()) |
56 return false; | 61 return false; |
57 return succeeded_; | 62 return succeeded_; |
58 } | 63 } |
59 | 64 |
60 bool Statement::BindNull(int col) { | 65 bool Statement::BindNull(int col) { |
61 if (is_valid()) { | 66 if (is_valid()) { |
62 int err = CheckError(sqlite3_bind_null(ref_->stmt(), col + 1)); | 67 int err = CheckError(sqlite3_bind_null(ref_->stmt(), col + 1)); |
63 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
64 return err == SQLITE_OK; | 68 return err == SQLITE_OK; |
65 } | 69 } |
66 return false; | 70 return false; |
67 } | 71 } |
68 | 72 |
69 bool Statement::BindInt(int col, int val) { | 73 bool Statement::BindInt(int col, int val) { |
70 if (is_valid()) { | 74 if (is_valid()) { |
71 int err = CheckError(sqlite3_bind_int(ref_->stmt(), col + 1, val)); | 75 int err = CheckError(sqlite3_bind_int(ref_->stmt(), col + 1, val)); |
72 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
73 return err == SQLITE_OK; | 76 return err == SQLITE_OK; |
74 } | 77 } |
75 return false; | 78 return false; |
76 } | 79 } |
77 | 80 |
78 bool Statement::BindInt64(int col, int64 val) { | 81 bool Statement::BindInt64(int col, int64 val) { |
79 if (is_valid()) { | 82 if (is_valid()) { |
80 int err = CheckError(sqlite3_bind_int64(ref_->stmt(), col + 1, val)); | 83 int err = CheckError(sqlite3_bind_int64(ref_->stmt(), col + 1, val)); |
81 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
82 return err == SQLITE_OK; | 84 return err == SQLITE_OK; |
83 } | 85 } |
84 return false; | 86 return false; |
85 } | 87 } |
86 | 88 |
87 bool Statement::BindDouble(int col, double val) { | 89 bool Statement::BindDouble(int col, double val) { |
88 if (is_valid()) { | 90 if (is_valid()) { |
89 int err = CheckError(sqlite3_bind_double(ref_->stmt(), col + 1, val)); | 91 int err = CheckError(sqlite3_bind_double(ref_->stmt(), col + 1, val)); |
90 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
91 return err == SQLITE_OK; | 92 return err == SQLITE_OK; |
92 } | 93 } |
93 return false; | 94 return false; |
94 } | 95 } |
95 | 96 |
96 bool Statement::BindCString(int col, const char* val) { | 97 bool Statement::BindCString(int col, const char* val) { |
97 if (is_valid()) { | 98 if (is_valid()) { |
98 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val, -1, | 99 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val, -1, |
99 SQLITE_TRANSIENT)); | 100 SQLITE_TRANSIENT)); |
100 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
101 return err == SQLITE_OK; | 101 return err == SQLITE_OK; |
102 } | 102 } |
103 return false; | 103 return false; |
104 } | 104 } |
105 | 105 |
106 bool Statement::BindString(int col, const std::string& val) { | 106 bool Statement::BindString(int col, const std::string& val) { |
107 if (is_valid()) { | 107 if (is_valid()) { |
108 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val.data(), | 108 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val.data(), |
109 val.size(), SQLITE_TRANSIENT)); | 109 val.size(), SQLITE_TRANSIENT)); |
110 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
111 return err == SQLITE_OK; | 110 return err == SQLITE_OK; |
112 } | 111 } |
113 return false; | 112 return false; |
114 } | 113 } |
115 | 114 |
116 bool Statement::BindBlob(int col, const void* val, int val_len) { | 115 bool Statement::BindBlob(int col, const void* val, int val_len) { |
117 if (is_valid()) { | 116 if (is_valid()) { |
118 int err = CheckError(sqlite3_bind_blob(ref_->stmt(), col + 1, | 117 int err = CheckError(sqlite3_bind_blob(ref_->stmt(), col + 1, |
119 val, val_len, SQLITE_TRANSIENT)); | 118 val, val_len, SQLITE_TRANSIENT)); |
120 DCHECK(err == SQLITE_OK) << ref_->connection()->GetErrorMessage(); | |
121 return err == SQLITE_OK; | 119 return err == SQLITE_OK; |
122 } | 120 } |
123 return false; | 121 return false; |
124 } | 122 } |
125 | 123 |
126 int Statement::ColumnCount() const { | 124 int Statement::ColumnCount() const { |
127 if (!is_valid()) { | 125 if (!is_valid()) { |
128 NOTREACHED(); | 126 NOTREACHED(); |
129 return 0; | 127 return 0; |
130 } | 128 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 memcpy(&(*val)[0], data, len); | 199 memcpy(&(*val)[0], data, len); |
202 } | 200 } |
203 } | 201 } |
204 | 202 |
205 void Statement::ColumnBlobAsVector( | 203 void Statement::ColumnBlobAsVector( |
206 int col, | 204 int col, |
207 std::vector<unsigned char>* val) const { | 205 std::vector<unsigned char>* val) const { |
208 ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); | 206 ColumnBlobAsVector(col, reinterpret_cast< std::vector<char>* >(val)); |
209 } | 207 } |
210 | 208 |
| 209 const char* Statement::GetSQLStatement() { |
| 210 return sqlite3_sql(ref_->stmt()); |
| 211 } |
| 212 |
211 int Statement::CheckError(int err) { | 213 int Statement::CheckError(int err) { |
| 214 // Please don't add DCHECKs here, OnSqliteError() already has them. |
212 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 215 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
213 | 216 if (!succeeded_ && is_valid()) |
214 // TODO(brettw) enhance this to process the error. | 217 return ref_->connection()->OnSqliteError(err, this); |
215 return err; | 218 return err; |
216 } | 219 } |
217 | 220 |
218 } // namespace sql | 221 } // namespace sql |
OLD | NEW |