summaryrefslogtreecommitdiff
path: root/chromium/sql/transaction.cc
blob: c9d90f1ccbc2f9b9ab5fa46e3d4cebeac32c40c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sql/transaction.h"

#include "base/logging.h"
#include "sql/database.h"

namespace sql {

Transaction::Transaction(Database* database)
    : database_(database), is_open_(false) {}

Transaction::~Transaction() {
  if (is_open_)
    database_->RollbackTransaction();
}

bool Transaction::Begin() {
  DCHECK(!is_open_) << "Beginning a transaction twice!";
  is_open_ = database_->BeginTransaction();
  return is_open_;
}

void Transaction::Rollback() {
  DCHECK(is_open_) << "Attempting to roll back a nonexistent transaction. "
                   << "Did you remember to call Begin() and check its return?";
  is_open_ = false;
  database_->RollbackTransaction();
}

bool Transaction::Commit() {
  DCHECK(is_open_) << "Attempting to commit a nonexistent transaction. "
                   << "Did you remember to call Begin() and check its return?";
  is_open_ = false;
  return database_->CommitTransaction();
}

}  // namespace sql