Created
March 27, 2024 16:59
-
-
Save klemens-morgenstern/6d38fa888610b5d90c50f4a2f032db79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
struct transaction | |
{ | |
enum behaviour {deferred, immediate, exclusive}; | |
constexpr static struct adopt_transaction_t {} adopt_transaction; | |
transaction(connection & conn, adopt_transaction_t) : conn_(conn), completed_(false) | |
{ | |
} | |
transaction(connection & conn) : conn_(conn) | |
{ | |
conn.execute("BEGIN"); | |
completed_ = false; | |
} | |
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; | |
} | |
~transaction() noexcept(false) | |
{ | |
if (!completed_) | |
conn_.execute("ROLLBACK"); | |
} | |
void commit() | |
{ | |
conn_.execute("COMMIT"); | |
completed_ = true; | |
} | |
void rollback() | |
{ | |
conn_.execute("ROLLBACK"); | |
completed_ = true; | |
} | |
void commit(system::error_code & ec, error_info & ei) | |
{ | |
conn_.execute("COMMIT", ec, ei); | |
completed_ = true; | |
} | |
void rollback(system::error_code & ec, error_info & ei) | |
{ | |
conn_.execute("ROLLBACK", ec, ei); | |
completed_ = true; | |
} | |
private: | |
connection & conn_; | |
bool completed_ = true; | |
}; | |
struct savepoint | |
{ | |
enum behaviour {deferred, immediate, exclusive}; | |
constexpr static transaction::adopt_transaction_t adopt_transaction; | |
savepoint(connection & conn, std::string name, transaction::adopt_transaction_t) | |
: conn_(conn), name_(std::move(name)) | |
{ | |
} | |
savepoint(connection & conn, std::string name) : conn_(conn), name_(std::move(name)) | |
{ | |
conn.execute("SAVEPOINT " + name); | |
completed_ = false; | |
} | |
~savepoint() noexcept(false) | |
{ | |
if (!completed_) | |
conn_.execute("ROLLBACK TO " + name_); | |
} | |
void commit() | |
{ | |
conn_.execute("RELEASE " + name_); | |
completed_ = true; | |
} | |
void release() | |
{ | |
conn_.execute("RELEASE " + name_); | |
completed_ = true; | |
} | |
void rollback() | |
{ | |
conn_.execute("ROLLBACK TO" + name_); | |
completed_ = true; | |
} | |
void commit(system::error_code & ec, error_info & ei) | |
{ | |
conn_.execute("RELEASE " + name_, ec, ei); | |
completed_ = true; | |
} | |
void release(system::error_code & ec, error_info & ei) | |
{ | |
conn_.execute("RELEASE " + name_, ec, ei); | |
completed_ = true; | |
} | |
void rollback(system::error_code & ec, error_info & ei) | |
{ | |
conn_.execute("ROLLBACK TO " + name_, ec, ei); | |
completed_ = true; | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment