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" |
11 | 11 |
12 namespace sql { | 12 namespace sql { |
13 | 13 |
14 // This empty constructor initializes our reference with an empty one so that | 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 | 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. | 16 // only have to check the ref's validity bit. |
17 Statement::Statement() | 17 Statement::Statement() |
18 : ref_(new Connection::StatementRef), | 18 : ref_(new Connection::StatementRef(NULL, NULL, false)), |
19 succeeded_(false) { | 19 succeeded_(false) { |
20 } | 20 } |
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(true); | 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(true); | 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(NULL, NULL, false)); |
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 // Allow operations to fail silently if a statement was invalidated |
46 DLOG(FATAL) << "Cannot call mutating statements on an invalid statement."; | 46 // because the database was closed by an error handler. |
| 47 DLOG_IF(FATAL, !ref_->was_valid()) |
| 48 << "Cannot call mutating statements on an invalid statement."; |
47 return is_valid(); | 49 return is_valid(); |
48 } | 50 } |
49 | 51 |
50 bool Statement::Run() { | 52 bool Statement::Run() { |
51 ref_->AssertIOAllowed(); | 53 ref_->AssertIOAllowed(); |
52 if (!CheckValid()) | 54 if (!CheckValid()) |
53 return false; | 55 return false; |
54 | 56 |
55 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; | 57 return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_DONE; |
56 } | 58 } |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 // TODO(gbillock,shess): make this invalidate the statement so it | 301 // TODO(gbillock,shess): make this invalidate the statement so it |
300 // can't wreak havoc. | 302 // can't wreak havoc. |
301 if (err == SQLITE_RANGE) | 303 if (err == SQLITE_RANGE) |
302 DLOG(FATAL) << "Bind value out of range"; | 304 DLOG(FATAL) << "Bind value out of range"; |
303 return err == SQLITE_OK; | 305 return err == SQLITE_OK; |
304 } | 306 } |
305 | 307 |
306 int Statement::CheckError(int err) { | 308 int Statement::CheckError(int err) { |
307 // Please don't add DCHECKs here, OnSqliteError() already has them. | 309 // Please don't add DCHECKs here, OnSqliteError() already has them. |
308 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 310 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
309 if (!succeeded_ && is_valid() && ref_->connection()) | 311 if (!succeeded_ && ref_ && ref_->connection()) |
310 return ref_->connection()->OnSqliteError(err, this); | 312 return ref_->connection()->OnSqliteError(err, this); |
311 return err; | 313 return err; |
312 } | 314 } |
313 | 315 |
314 } // namespace sql | 316 } // namespace sql |
OLD | NEW |