Skip to content

Instantly share code, notes, and snippets.

@klemens-morgenstern
Created March 27, 2024 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klemens-morgenstern/6d38fa888610b5d90c50f4a2f032db79 to your computer and use it in GitHub Desktop.
Save klemens-morgenstern/6d38fa888610b5d90c50f4a2f032db79 to your computer and use it in GitHub Desktop.
//
// 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