SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
Transaction.h
Go to the documentation of this file.
1 /**
2  * @file Transaction.h
3  * @brief A Transaction is way to group multiple SQL statements into an atomic secured operation.
4  *
5  * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6  *
7  * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
8  * or copy at http://opensource.org/licenses/MIT)
9  */
10 #pragma once
11 
12 #include <sqlite3.h>
13 #include "Exception.h"
14 
15 namespace SQLite
16 {
17 
18 // Forward declaration
19 class Database;
20 
21 /**
22  * @brief RAII encapsulation of a SQLite Transaction.
23  *
24  * A Transaction is a way to group multiple SQL statements into an atomic secured operation;
25  * either it succeeds, with all the changes committed to the database file,
26  * or if it fails, all the changes are rolled back to the initial state.
27  *
28  * Resource Acquisition Is Initialization (RAII) means that the Transaction
29  * begins in the constructor and is rollbacked in the destructor, so that there is
30  * no need to worry about memory management or the validity of the underlying SQLite Connection.
31  *
32  * This method also offers big performances improvements compared to individually executed statements.
33  */
35 {
36 public:
37  /**
38  * @brief Begins the SQLite transaction
39  *
40  * @param[in] aDatabase the SQLite Database Connection
41  *
42  * Exception is thrown in case of error, then the Transaction is NOT initiated.
43  */
44  explicit Transaction(Database& aDatabase); // throw(SQLite::Exception);
45 
46  /**
47  * @brief Safely rollback the transaction if it has not been committed.
48  */
49  virtual ~Transaction(void) throw(); // nothrow
50 
51  /**
52  * @brief Commit the transaction.
53  */
54  void commit(void); // throw(SQLite::Exception);
55 
56 private:
57  // Transaction must be non-copyable
58  Transaction(const Transaction&);
59  Transaction& operator=(const Transaction&);
60  /// @}
61 
62 private:
63  Database& mDatabase; //!< Reference to the SQLite Database Connection
64  bool mbCommited; //!< True when commit has been called
65 };
66 
67 
68 } // namespace SQLite