SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
Transaction.cpp
Go to the documentation of this file.
1 /**
2  * @file Transaction.cpp
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 #include "Transaction.h"
11 
12 #include "Database.h"
13 
14 namespace SQLite
15 {
16 
17 // Begins the SQLite transaction
18 Transaction::Transaction(Database& aDatabase) : // throw(SQLite::Exception)
19  mDatabase(aDatabase),
20  mbCommited(false)
21 {
22  mDatabase.exec("BEGIN");
23 }
24 
25 // Safely rollback the transaction if it has not been committed.
26 Transaction::~Transaction(void) throw() // nothrow
27 {
28  if (false == mbCommited)
29  {
30  try
31  {
32  mDatabase.exec("ROLLBACK");
33  }
34  catch (SQLite::Exception& /*e*/)
35  {
36  // Never throw an exception in a destructor
37  //std::cout << e.what() << std::endl;
38  SQLITE_CPP_ASSERT(false);
39  }
40  }
41 }
42 
43 // Commit the transaction.
44 void Transaction::commit(void) // throw(SQLite::Exception)
45 {
46  if (false == mbCommited)
47  {
48  mDatabase.exec("COMMIT");
49  mbCommited = true;
50  }
51  else
52  {
53  throw SQLite::Exception("Transaction already commited");
54  }
55 }
56 
57 
58 } // namespace SQLite