boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
transaction.hpp
1//
2// Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BOOST_SQLITE_TRANSACTION_HPP
9#define BOOST_SQLITE_TRANSACTION_HPP
10
11#include <boost/sqlite/connection.hpp>
12
13BOOST_SQLITE_BEGIN_NAMESPACE
14
30{
32 enum behaviour {deferred, immediate, exclusive};
34 constexpr static struct adopt_transaction_t {} adopt_transaction{};
35
36
38 transaction(connection & conn, adopt_transaction_t) : conn_(conn), completed_(false)
39 {
40 }
41
42
44 transaction(connection & conn) : conn_(conn)
45 {
46 conn.execute("BEGIN");
47 completed_ = false;
48 }
49
51 transaction(connection & conn, behaviour b) : conn_(conn)
52 {
53 switch (b)
54 {
55 case deferred: conn.execute("BEGIN DEFERRED"); break;
56 case immediate: conn.execute("BEGIN IMMEDIATE"); break;
57 case exclusive: conn.execute("BEGIN EXCLUSIVE"); break;
58 }
59 completed_ = false;
60 }
61
63 ~transaction() noexcept(false)
64 {
65 if (!completed_)
66 conn_.execute("ROLLBACK");
67 }
68
71 void commit()
72 {
73 conn_.execute("COMMIT");
74 completed_ = true;
75 }
76
77 void commit(system::error_code & ec, error_info & ei)
78 {
79 conn_.execute("COMMIT", ec, ei);
80 completed_ = true;
81 }
83
86 void rollback()
87 {
88 conn_.execute("ROLLBACK");
89 completed_ = true;
90 }
91
92 void rollback(system::error_code & ec, error_info & ei)
93 {
94 conn_.execute("ROLLBACK", ec, ei);
95 completed_ = true;
96 }
98
99 private:
100 connection & conn_;
101 bool completed_ = true;
102};
103
118struct savepoint
119{
121 constexpr static transaction::adopt_transaction_t adopt_transaction{};
122
124 savepoint(connection & conn, std::string name, transaction::adopt_transaction_t)
125 : conn_(conn), name_(std::move(name))
126 {
127 }
128
130 savepoint(connection & conn, std::string name) : conn_(conn), name_(std::move(name))
131 {
132 conn.execute("SAVEPOINT " + name_);
133 completed_ = false;
134 }
135
136
138 ~savepoint() noexcept(false)
139 {
140 if (!completed_)
141 conn_.execute("ROLLBACK TO " + name_);
142 }
143
146 void commit()
147 {
148 conn_.execute("RELEASE " + name_);
149 completed_ = true;
150 }
152 void commit(system::error_code & ec, error_info & ei)
153 {
154 conn_.execute("RELEASE " + name_, ec, ei);
155 completed_ = true;
156 }
158 void release()
159 {
160 conn_.execute("RELEASE " + name_);
161 completed_ = true;
162 }
164 void release(system::error_code & ec, error_info & ei)
165 {
166 conn_.execute("RELEASE " + name_, ec, ei);
167 completed_ = true;
168 }
170
173 void rollback()
174 {
175 conn_.execute("ROLLBACK TO" + name_);
176 completed_ = true;
177 }
179 void rollback(system::error_code & ec, error_info & ei)
180 {
181 conn_.execute("ROLLBACK TO " + name_, ec, ei);
182 completed_ = true;
183 }
185
187 const std::string & name() const {return name_;}
188 private:
189 connection & conn_;
190 std::string name_;
191 bool completed_ = true;
192};
193
194
195BOOST_SQLITE_END_NAMESPACE
196
197#endif //BOOST_SQLITE_TRANSACTION_HPP
main object for a connection to a database.
Additional information about error conditions stored in an sqlite-allocate string.
Definition error.hpp:33
A simple transaction guard implementing RAAI for transactions.
transaction(connection &conn)
Create transaction guard and initiate a transaction.
transaction(connection &conn, behaviour b)
Create transaction guard and initiate a transaction with the defined behaviour.
void commit(system::error_code &ec, error_info &ei)
void rollback(system::error_code &ec, error_info &ei)
behaviour
The mode of the transaction.
transaction(connection &conn, adopt_transaction_t)
Create transaction guard on an existing transaction.
~transaction() noexcept(false)
rollback the transaction if not committed.