[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" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 8 | #include "sql/database.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | |
10 | namespace sql { | ||||
11 | |||||
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 12 | Transaction::Transaction(Database* database) |
13 | : database_(database), is_open_(false) {} | ||||
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 14 | |
15 | Transaction::~Transaction() { | ||||
16 | if (is_open_) | ||||
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 17 | database_->RollbackTransaction(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 18 | } |
19 | |||||
20 | bool Transaction::Begin() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 21 | DCHECK(!is_open_) << "Beginning a transaction twice!"; |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 22 | is_open_ = database_->BeginTransaction(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 23 | return is_open_; |
24 | } | ||||
25 | |||||
26 | void Transaction::Rollback() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 27 | DCHECK(is_open_) << "Attempting to roll back a nonexistent transaction. " |
28 | << "Did you remember to call Begin() and check its return?"; | ||||
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 29 | is_open_ = false; |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 30 | database_->RollbackTransaction(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 31 | } |
32 | |||||
33 | bool Transaction::Commit() { | ||||
[email protected] | 4ab952f | 2014-04-01 20:18:16 | [diff] [blame] | 34 | DCHECK(is_open_) << "Attempting to commit a nonexistent transaction. " |
35 | << "Did you remember to call Begin() and check its return?"; | ||||
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 36 | is_open_ = false; |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 37 | return database_->CommitTransaction(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 38 | } |
39 | |||||
40 | } // namespace sql |