blob: 5a34e65c3a4f97e8423775065fb94c36a7481cf8 [file] [log] [blame]
[email protected]94f06382012-03-07 20:51:371// Copyright (c) 2012 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#ifndef SQL_TRANSACTION_H_
6#define SQL_TRANSACTION_H_
[email protected]e5ffd0e42009-09-11 21:30:567
tfarina720d4f32015-05-11 22:31:268#include "base/macros.h"
[email protected]d4526962011-11-10 21:40:289#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5610
11namespace sql {
12
13class Connection;
14
[email protected]d4526962011-11-10 21:40:2815class SQL_EXPORT Transaction {
[email protected]e5ffd0e42009-09-11 21:30:5616 public:
17 // Creates the scoped transaction object. You MUST call Begin() to begin the
18 // transaction. If you have begun a transaction and not committed it, the
19 // constructor will roll back the transaction. If you want to commit, you
20 // need to manually call Commit before this goes out of scope.
[email protected]94f06382012-03-07 20:51:3721 //
22 // Nested transactions are supported. See sql::Connection::BeginTransaction
23 // for details.
[email protected]a5b58f52009-11-17 22:15:4424 explicit Transaction(Connection* connection);
[email protected]e5ffd0e42009-09-11 21:30:5625 ~Transaction();
26
27 // Returns true when there is a transaction that has been successfully begun.
28 bool is_open() const { return is_open_; }
29
30 // Begins the transaction. This uses the default sqlite "deferred" transaction
31 // type, which means that the DB lock is lazily acquired the next time the
32 // database is accessed, not in the begin transaction command.
33 //
34 // Returns false on failure. Note that if this fails, you shouldn't do
35 // anything you expect to be actually transactional, because it won't be!
36 bool Begin();
37
38 // Rolls back the transaction. This will happen automatically if you do
39 // nothing when the transaction goes out of scope.
40 void Rollback();
41
42 // Commits the transaction, returning true on success. This will return
43 // false if sqlite could not commit it, or if another transaction in the
44 // same outermost transaction has been rolled back (which necessitates a
45 // rollback of all transactions in that outermost one).
46 bool Commit();
47
48 private:
49 Connection* connection_;
50
51 // True when the transaction is open, false when it's already been committed
52 // or rolled back.
53 bool is_open_;
54
55 DISALLOW_COPY_AND_ASSIGN(Transaction);
56};
57
58} // namespace sql
59
[email protected]f0a54b22011-07-19 18:40:2160#endif // SQL_TRANSACTION_H_