1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
//
// Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_SQLITE_TRANSACTION_HPP
#define BOOST_SQLITE_TRANSACTION_HPP
#include <boost/sqlite/connection.hpp>
BOOST_SQLITE_BEGIN_NAMESPACE
/**
* @brief A simple transaction guard implementing RAAI for transactions
* @ingroup reference
*
* @par Example
* @code{.cpp}
* sqlite::connection conn;
* conn.connect("./my-database.db");
*
* sqlite::transaction t{conn};
* conn.prepare("insert into log (text) values ($1)").execute(std::make_tuple("booting up"));
* t.commit();
* @endcode
*/
struct transaction
{
/// The mode of the transaction
enum behaviour {deferred, immediate, exclusive};
/// A tag to use, to adopt an already initiated transaction.
constexpr static struct adopt_transaction_t {} adopt_transaction{};
/// Create transaction guard on an existing transaction
transaction(connection & conn, adopt_transaction_t) : conn_(conn), completed_(false)
{
}
/// Create transaction guard and initiate a transaction
transaction(connection & conn) : conn_(conn)
{
conn.execute("BEGIN");
completed_ = false;
}
/// Create transaction guard and initiate a transaction with the defined behaviour
transaction(connection & conn, behaviour b) : conn_(conn)
{
switch (b)
{
case deferred: conn.execute("BEGIN DEFERRED"); break;
case immediate: conn.execute("BEGIN IMMEDIATE"); break;
case exclusive: conn.execute("BEGIN EXCLUSIVE"); break;
}
completed_ = false;
}
// see https://www.sqlite.org/lang_transaction.html re noexcept
/// rollback the transaction if not committed.
~transaction() noexcept(SQLITE_VERSION_NUMBER >= 3007011)
{
if (!completed_)
conn_.execute("ROLLBACK");
}
///@{
/// Commit the transaction.
void commit()
{
conn_.execute("COMMIT");
completed_ = true;
}
void commit(system::error_code & ec, error_info & ei)
{
conn_.execute("COMMIT", ec, ei);
completed_ = true;
}
///@}
///@{
/// Rollback the transaction explicitly.
void rollback()
{
conn_.execute("ROLLBACK");
completed_ = true;
}
void rollback(system::error_code & ec, error_info & ei)
{
conn_.execute("ROLLBACK", ec, ei);
completed_ = true;
}
///@}
private:
connection & conn_;
bool completed_ = true;
};
/**
* @brief A simple transaction guard implementing RAAI for savepoints. Savepoints can be used recursively.
* @ingroup reference
*
* @par Example
* @code{.cpp}
* sqlite::connection conn;
* conn.connect("./my-database.db");
*
* sqlite::savepoint t{conn, "my-savepoint};
* conn.prepare("insert into log (text) values ($1)").execute(std::make_tuple("booting up"));
* t.commit();
* @endcode
*/
struct savepoint
{
/// A tag to use, to adopt an already initiated transaction.
constexpr static transaction::adopt_transaction_t adopt_transaction{};
/// Create savepoint guard on an existing savepoint
savepoint(connection & conn, std::string name, transaction::adopt_transaction_t)
: conn_(conn), name_(std::move(name))
{
}
/// Create transaction guard and initiate it
savepoint(connection & conn, std::string name) : conn_(conn), name_(std::move(name))
{
conn.execute("SAVEPOINT " + name_);
completed_ = false;
}
/// rollback to the savepoint if not committed.
~savepoint() noexcept(SQLITE_VERSION_NUMBER >= 3007011)
{
if (!completed_)
conn_.execute("ROLLBACK TO " + name_);
}
///@{
/// Commit/Release the transaction.
void commit()
{
conn_.execute("RELEASE " + name_);
completed_ = true;
}
void commit(system::error_code & ec, error_info & ei)
{
conn_.execute("RELEASE " + name_, ec, ei);
completed_ = true;
}
void release()
{
conn_.execute("RELEASE " + name_);
completed_ = true;
}
void release(system::error_code & ec, error_info & ei)
{
conn_.execute("RELEASE " + name_, ec, ei);
completed_ = true;
}
///@}
///@{
/// Rollback the transaction explicitly.
void rollback()
{
conn_.execute("ROLLBACK TO" + name_);
completed_ = true;
}
void rollback(system::error_code & ec, error_info & ei)
{
conn_.execute("ROLLBACK TO " + name_, ec, ei);
completed_ = true;
}
///@}
/// The name of the savepoint.
const std::string & name() const {return name_;}
private:
connection & conn_;
std::string name_;
bool completed_ = true;
};
BOOST_SQLITE_END_NAMESPACE
#endif //BOOST_SQLITE_TRANSACTION_HPP
|