• 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

96.0
/src/Savepoint.cpp
1
/**
2
 * @file    Savepoint.cpp
3
 * @ingroup SQLiteCpp
4
 * @brief   A Savepoint is a way to group multiple SQL statements into an atomic
5
 * secured operation. Similar to a transaction while allowing child savepoints.
6
 *
7
 * Copyright (c) 2020 Kelvin Hammond (hammond.kelvin@gmail.com)
8
 * Copyright (c) 2020-2025 Sebastien Rombauts (sebastien.rombauts@gmail.com)
9
 *
10
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt or
11
 * copy at http://opensource.org/licenses/MIT)
12
 */
13

14
#include <SQLiteCpp/Assertion.h>
15
#include <SQLiteCpp/Database.h>
16
#include <SQLiteCpp/Savepoint.h>
17
#include <SQLiteCpp/Statement.h>
18

19
namespace SQLite
20
{
21

22
// Begins the SQLite savepoint
23
Savepoint::Savepoint(Database& aDatabase, const std::string& aName)
4✔
24
    : mDatabase(aDatabase), msName(aName)
4✔
25
{
26
    // workaround because you cannot bind to SAVEPOINT
27
    // escape name for use in query
28
    Statement stmt(mDatabase, "SELECT quote(?)");
8✔
29
    stmt.bind(1, msName);
4✔
30
    stmt.executeStep();
4✔
31
    msName = stmt.getColumn(0).getText();
4✔
32

33
    mDatabase.exec(std::string("SAVEPOINT ") + msName);
4✔
34
}
4✔
35

36
// Safely rollback the savepoint if it has not been committed.
37
Savepoint::~Savepoint()
8✔
38
{
39
    if (!mbReleased)
4✔
40
    {
41
        try
42
        {
43
            rollback();
3✔
44
            release();
3✔
45
        }
46
        catch (SQLite::Exception&)
×
47
        {
48
            // Never throw an exception in a destructor: error if already released,
49
            // but no harm is caused by this.
50
        }
51
    }
52
}
4✔
53

54
// Release the savepoint and commit
55
void Savepoint::release()
5✔
56
{
57
    if (!mbReleased)
5✔
58
    {
59
        mDatabase.exec(std::string("RELEASE SAVEPOINT ") + msName);
4✔
60
        mbReleased = true;
4✔
61
    }
62
    else
63
    {
64
        throw SQLite::Exception("Savepoint already released.");
1✔
65
    }
66
}
4✔
67

68
// Rollback to the savepoint, but don't release it
69
void Savepoint::rollbackTo()
5✔
70
{
71
    if (!mbReleased)
5✔
72
    {
73
        mDatabase.exec(std::string("ROLLBACK TO SAVEPOINT ") + msName);
4✔
74
    }
75
    else
76
    {
77
        throw SQLite::Exception("Savepoint already released.");
1✔
78
    }
79
}
4✔
80

81
}  // 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