[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #include "sql/transaction.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 6 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 8 | #include "sql/connection.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | |
10 | namespace sql { | ||||
11 | |||||
12 | Transaction::Transaction(Connection* connection) | ||||
13 | : connection_(connection), | ||||
14 | is_open_(false) { | ||||
15 | } | ||||
16 | |||||
17 | Transaction::~Transaction() { | ||||
18 | if (is_open_) | ||||
19 | connection_->RollbackTransaction(); | ||||
20 | } | ||||
21 | |||||
22 | bool Transaction::Begin() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 23 | DCHECK(!is_open_) << "Beginning a transaction twice!"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 24 | is_open_ = connection_->BeginTransaction(); |
25 | return is_open_; | ||||
26 | } | ||||
27 | |||||
28 | void Transaction::Rollback() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 29 | DCHECK(is_open_) << "Attempting to roll back a nonexistent transaction. " |
30 | << "Did you remember to call Begin() and check its return?"; | ||||
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 31 | is_open_ = false; |
32 | connection_->RollbackTransaction(); | ||||
33 | } | ||||
34 | |||||
35 | bool Transaction::Commit() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 36 | DCHECK(is_open_) << "Attempting to commit a nonexistent transaction. " |
37 | << "Did you remember to call Begin() and check its return?"; | ||||
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 38 | is_open_ = false; |
39 | return connection_->CommitTransaction(); | ||||
40 | } | ||||
41 | |||||
42 | } // namespace sql |