• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRombauts / SQLiteCpp / #613698365

22 Nov 2023 10:24AM UTC coverage: 98.238%. First build
#613698365

travis-ci

669 of 681 relevant lines covered (98.24%)

29.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/Transaction.cpp
1
/**
2
 * @file    Transaction.cpp
3
 * @ingroup SQLiteCpp
4
 * @brief   A Transaction is way to group multiple SQL statements into an atomic secured operation.
5
 *
6
 * Copyright (c) 2012-2025 Sebastien Rombauts (sebastien.rombauts@gmail.com)
7
 *
8
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9
 * or copy at http://opensource.org/licenses/MIT)
10
 */
11
#include <SQLiteCpp/Transaction.h>
12

13
#include <SQLiteCpp/Database.h>
14
#include <SQLiteCpp/Assertion.h>
15

16
#include <sqlite3.h>
17

18
namespace SQLite
19
{
20

21
// Begins the SQLite transaction
22
Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
4✔
23
    mDatabase(aDatabase)
4✔
24
{
25
    const char *stmt;
26
    switch (behavior) {
4✔
27
        case TransactionBehavior::DEFERRED:
1✔
28
            stmt = "BEGIN DEFERRED";
1✔
29
            break;
1✔
30
        case TransactionBehavior::IMMEDIATE:
1✔
31
            stmt = "BEGIN IMMEDIATE";
1✔
32
            break;
1✔
33
        case TransactionBehavior::EXCLUSIVE:
1✔
34
            stmt = "BEGIN EXCLUSIVE";
1✔
35
            break;
1✔
36
        default:
1✔
37
            throw SQLite::Exception("invalid/unknown transaction behavior", SQLITE_ERROR);
1✔
38
    }
39
    mDatabase.exec(stmt);
3✔
40
}
3✔
41

42
// Begins the SQLite transaction
43
Transaction::Transaction(Database &aDatabase) :
6✔
44
    mDatabase(aDatabase)
6✔
45
{
46
    mDatabase.exec("BEGIN TRANSACTION");
6✔
47
}
6✔
48

49
// Safely rollback the transaction if it has not been committed.
50
Transaction::~Transaction()
18✔
51
{
52
    if (false == mbCommited)
9✔
53
    {
54
        try
55
        {
56
            mDatabase.exec("ROLLBACK TRANSACTION");
4✔
57
        }
58
        catch (SQLite::Exception&)
1✔
59
        {
60
            // Never throw an exception in a destructor: error if already rollbacked, but no harm is caused by this.
61
        }
62
    }
63
}
9✔
64

65
// Commit the transaction.
66
void Transaction::commit()
6✔
67
{
68
    if (false == mbCommited)
6✔
69
    {
70
        mDatabase.exec("COMMIT TRANSACTION");
5✔
71
        mbCommited = true;
5✔
72
    }
73
    else
74
    {
75
        throw SQLite::Exception("Transaction already committed.");
1✔
76
    }
77
}
5✔
78

79
// Rollback the transaction
80
void Transaction::rollback()
1✔
81
{
82
    if (false == mbCommited)
1✔
83
    {
84
        mDatabase.exec("ROLLBACK TRANSACTION");
1✔
85
    }
86
    else
87
    {
88
        throw SQLite::Exception("Transaction already committed.");
×
89
    }
90
}
1✔
91

92
}  // namespace SQLite
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc