blob: c9d90f1ccbc2f9b9ab5fa46e3d4cebeac32c40c2 [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"
Victor Costancfbfa602018-08-01 23:24:468#include "sql/database.h"
[email protected]e5ffd0e42009-09-11 21:30:569
10namespace sql {
11
Victor Costancfbfa602018-08-01 23:24:4612Transaction::Transaction(Database* database)
13 : database_(database), is_open_(false) {}
[email protected]e5ffd0e42009-09-11 21:30:5614
15Transaction::~Transaction() {
16 if (is_open_)
Victor Costancfbfa602018-08-01 23:24:4617 database_->RollbackTransaction();
[email protected]e5ffd0e42009-09-11 21:30:5618}
19
20bool Transaction::Begin() {
[email protected]4ab952f2014-04-01 20:18:1621 DCHECK(!is_open_) << "Beginning a transaction twice!";
Victor Costancfbfa602018-08-01 23:24:4622 is_open_ = database_->BeginTransaction();
[email protected]e5ffd0e42009-09-11 21:30:5623 return is_open_;
24}
25
26void Transaction::Rollback() {
[email protected]4ab952f2014-04-01 20:18:1627 DCHECK(is_open_) << "Attempting to roll back a nonexistent transaction. "
28 << "Did you remember to call Begin() and check its return?";
[email protected]e5ffd0e42009-09-11 21:30:5629 is_open_ = false;
Victor Costancfbfa602018-08-01 23:24:4630 database_->RollbackTransaction();
[email protected]e5ffd0e42009-09-11 21:30:5631}
32
33bool Transaction::Commit() {
[email protected]4ab952f2014-04-01 20:18:1634 DCHECK(is_open_) << "Attempting to commit a nonexistent transaction. "
35 << "Did you remember to call Begin() and check its return?";
[email protected]e5ffd0e42009-09-11 21:30:5636 is_open_ = false;
Victor Costancfbfa602018-08-01 23:24:4637 return database_->CommitTransaction();
[email protected]e5ffd0e42009-09-11 21:30:5638}
39
40} // namespace sql