blob: 06bcbebebddde5c7fc504c1a2050f1fd2fce97c1 [file] [log] [blame]
[email protected]f0a54b22011-07-19 18:40:211// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#include "sql/transaction.h"
[email protected]e5ffd0e42009-09-11 21:30:566
[email protected]e5ffd0e42009-09-11 21:30:567#include "base/logging.h"
[email protected]f0a54b22011-07-19 18:40:218#include "sql/connection.h"
[email protected]e5ffd0e42009-09-11 21:30:569
10namespace sql {
11
12Transaction::Transaction(Connection* connection)
13 : connection_(connection),
14 is_open_(false) {
15}
16
17Transaction::~Transaction() {
18 if (is_open_)
19 connection_->RollbackTransaction();
20}
21
22bool Transaction::Begin() {
23 if (is_open_) {
24 NOTREACHED() << "Beginning a transaction twice!";
25 return false;
26 }
27 is_open_ = connection_->BeginTransaction();
28 return is_open_;
29}
30
31void Transaction::Rollback() {
32 if (!is_open_) {
[email protected]e7afe2452010-08-22 16:19:1333 NOTREACHED() << "Attempting to roll back a nonexistent transaction. "
[email protected]e5ffd0e42009-09-11 21:30:5634 << "Did you remember to call Begin() and check its return?";
35 return;
36 }
37 is_open_ = false;
38 connection_->RollbackTransaction();
39}
40
41bool Transaction::Commit() {
42 if (!is_open_) {
[email protected]e7afe2452010-08-22 16:19:1343 NOTREACHED() << "Attempting to commit a nonexistent transaction. "
[email protected]e5ffd0e42009-09-11 21:30:5644 << "Did you remember to call Begin() and check its return?";
45 return false;
46 }
47 is_open_ = false;
48 return connection_->CommitTransaction();
49}
50
51} // namespace sql