Skip to content

Commit ded6951

Browse files
authored
Merge pull request #339 Allow specifying transaction behaviors DEFERRED, IMMEDIATE, and EXCLUSIVE from jjenkins278/transaction_behavior
2 parents c7909ee + abd139c commit ded6951

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

include/SQLiteCpp/Transaction.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ namespace SQLite
2020
// Forward declaration
2121
class Database;
2222

23+
/**
24+
* @brief Transaction behaviors when opening an SQLite transaction.
25+
* Names correspond directly to the behavior.
26+
*/
27+
enum class TransactionBehavior {
28+
DEFERRED,
29+
IMMEDIATE,
30+
EXCLUSIVE,
31+
};
32+
2333
/**
2434
* @brief RAII encapsulation of a SQLite Transaction.
2535
*
@@ -44,14 +54,24 @@ class Transaction
4454
{
4555
public:
4656
/**
47-
* @brief Begins the SQLite transaction
57+
* @brief Begins the SQLite transaction using the default transaction behavior.
4858
*
4959
* @param[in] aDatabase the SQLite Database Connection
5060
*
5161
* Exception is thrown in case of error, then the Transaction is NOT initiated.
5262
*/
5363
explicit Transaction(Database& aDatabase);
5464

65+
/**
66+
* @brief Begins the SQLite transaction with the specified behavior.
67+
*
68+
* @param[in] aDatabase the SQLite Database Connection
69+
* @param[in] behavior the requested transaction behavior
70+
*
71+
* Exception is thrown in case of error, then the Transaction is NOT initiated.
72+
*/
73+
explicit Transaction(Database& aDatabase, TransactionBehavior behavior);
74+
5575
// Transaction is non-copyable
5676
Transaction(const Transaction&) = delete;
5777
Transaction& operator=(const Transaction&) = delete;

src/Transaction.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,36 @@
1313
#include <SQLiteCpp/Database.h>
1414
#include <SQLiteCpp/Assertion.h>
1515

16+
#include <sqlite3.h>
1617

1718
namespace SQLite
1819
{
1920

2021

2122
// Begins the SQLite transaction
22-
Transaction::Transaction(Database& aDatabase) :
23+
Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
24+
mDatabase(aDatabase),
25+
mbCommited(false)
26+
{
27+
const char *stmt;
28+
switch (behavior) {
29+
case TransactionBehavior::DEFERRED:
30+
stmt = "BEGIN DEFERRED";
31+
break;
32+
case TransactionBehavior::IMMEDIATE:
33+
stmt = "BEGIN IMMEDIATE";
34+
break;
35+
case TransactionBehavior::EXCLUSIVE:
36+
stmt = "BEGIN EXCLUSIVE";
37+
break;
38+
default:
39+
throw SQLite::Exception("invalid/unknown transaction behavior", SQLITE_ERROR);
40+
}
41+
mDatabase.exec(stmt);
42+
}
43+
44+
// Begins the SQLite transaction
45+
Transaction::Transaction(Database &aDatabase) :
2346
mDatabase(aDatabase),
2447
mbCommited(false)
2548
{

tests/Transaction_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ TEST(Transaction, commitRollback)
4242
EXPECT_THROW(transaction.commit(), SQLite::Exception);
4343
}
4444

45+
// ensure transactions with different types are well-formed
46+
{
47+
for (auto behavior : {
48+
SQLite::TransactionBehavior::DEFERRED,
49+
SQLite::TransactionBehavior::IMMEDIATE,
50+
SQLite::TransactionBehavior::EXCLUSIVE })
51+
{
52+
SQLite::Transaction transaction(db, behavior);
53+
transaction.commit();
54+
}
55+
56+
EXPECT_THROW(SQLite::Transaction(db, static_cast<SQLite::TransactionBehavior>(-1)), SQLite::Exception);
57+
}
58+
4559
// Auto rollback if no commit() before the end of scope
4660
{
4761
// Begin transaction

0 commit comments

Comments
 (0)