Home | History | Annotate | Download | only in sql
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef APP_SQL_TRANSACTION_H_
      6 #define APP_SQL_TRANSACTION_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 
     11 namespace sql {
     12 
     13 class Connection;
     14 
     15 class Transaction {
     16  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.
     21   explicit Transaction(Connection* connection);
     22   ~Transaction();
     23 
     24   // Returns true when there is a transaction that has been successfully begun.
     25   bool is_open() const { return is_open_; }
     26 
     27   // Begins the transaction. This uses the default sqlite "deferred" transaction
     28   // type, which means that the DB lock is lazily acquired the next time the
     29   // database is accessed, not in the begin transaction command.
     30   //
     31   // Returns false on failure. Note that if this fails, you shouldn't do
     32   // anything you expect to be actually transactional, because it won't be!
     33   bool Begin();
     34 
     35   // Rolls back the transaction. This will happen automatically if you do
     36   // nothing when the transaction goes out of scope.
     37   void Rollback();
     38 
     39   // Commits the transaction, returning true on success. This will return
     40   // false if sqlite could not commit it, or if another transaction in the
     41   // same outermost transaction has been rolled back (which necessitates a
     42   // rollback of all transactions in that outermost one).
     43   bool Commit();
     44 
     45  private:
     46   Connection* connection_;
     47 
     48   // True when the transaction is open, false when it's already been committed
     49   // or rolled back.
     50   bool is_open_;
     51 
     52   DISALLOW_COPY_AND_ASSIGN(Transaction);
     53 };
     54 
     55 }  // namespace sql
     56 
     57 #endif  // APP_SQL_TRANSACTION_H_
     58