Add is_transaction_active() to all connectors (#550)

* Document the connector API method is_transaction_active()
* Move mysql::connection_base::is_transaction_active() to the other transaction-handling methods.
* Add more tests for mysql::connection::is_transaction_active()
* Add postgresql::connection_base::is_transaction_active()
* Add tests for postgresql::connection_base::is_transaction_active()
* Change the type of the SQLite3 transaction status from transaction_status_type to a boolean flag.
* Add sqlite3::connection_base::is_transaction_active()
* Add tests for sqlite3::connection_base::is_transaction_active()
* When closing a transaction do it in the following order: report (if any), execute SQL command, set transaction active flag to false.
This commit is contained in:
MeanSquaredError
2024-01-05 09:59:42 +02:00
committed by GitHub
parent 3474a4fa5d
commit 9b49afa306
7 changed files with 42 additions and 30 deletions

View File

@@ -291,11 +291,6 @@ namespace sqlpp
return _handle->config;
}
bool is_transaction_active()
{
return _transaction_active;
}
template <typename Select>
char_result_t select(const Select& s)
{
@@ -462,8 +457,8 @@ namespace sqlpp
{
throw sqlpp::exception{"MySQL: Cannot commit a finished or failed transaction"};
}
_transaction_active = false;
execute_statement(_handle, "COMMIT");
_transaction_active = false;
}
//! rollback transaction (or throw if the transaction has been finished already)
@@ -477,8 +472,8 @@ namespace sqlpp
{
std::cerr << "MySQL warning: Rolling back unfinished transaction" << std::endl;
}
_transaction_active = false;
execute_statement(_handle, "ROLLBACK");
_transaction_active = false;
}
//! report a rollback failure (will be called by transactions in case of a rollback failure in the destructor)
@@ -487,6 +482,12 @@ namespace sqlpp
std::cerr << "MySQL message:" << message << std::endl;
}
//! check if transaction is active
bool is_transaction_active()
{
return _transaction_active;
}
MYSQL* native_handle()
{
return _handle->native_handle();

View File

@@ -542,9 +542,8 @@ namespace sqlpp
{
throw sqlpp::exception{"PostgreSQL error: transaction failed or finished."};
}
_transaction_active = false;
execute("COMMIT");
_transaction_active = false;
}
//! rollback transaction
@@ -554,12 +553,11 @@ namespace sqlpp
{
throw sqlpp::exception{"PostgreSQL error: transaction failed or finished."};
}
execute("ROLLBACK");
if (report)
{
std::cerr << "PostgreSQL warning: rolling back unfinished transaction" << std::endl;
}
execute("ROLLBACK");
_transaction_active = false;
}
@@ -569,6 +567,12 @@ namespace sqlpp
std::cerr << "PostgreSQL error: " << message << std::endl;
}
//! check if transaction is active
bool is_transaction_active()
{
return _transaction_active;
}
//! get the last inserted id for a certain table
uint64_t last_insert_id(const std::string& table, const std::string& fieldname)
{

View File

@@ -146,14 +146,7 @@ namespace sqlpp
class SQLPP11_SQLITE3_EXPORT connection_base : public sqlpp::connection
{
private:
enum class transaction_status_type
{
none,
maybe,
active
};
transaction_status_type _transaction_status{transaction_status_type::none};
bool _transaction_active{false};
// direct execution
bind_result_t select_impl(const std::string& statement)
@@ -470,35 +463,33 @@ namespace sqlpp
//! start transaction
void start_transaction()
{
if (_transaction_status == transaction_status_type::active)
if (_transaction_active)
{
throw sqlpp::exception{"Sqlite3 error: Cannot have more than one open transaction per connection"};
}
_transaction_status = transaction_status_type::maybe;
auto prepared = prepare_statement(_handle, "BEGIN");
execute_statement(_handle, prepared);
_transaction_status = transaction_status_type::active;
_transaction_active = true;
}
//! commit transaction (or throw if the transaction has been finished already)
void commit_transaction()
{
if (_transaction_status == transaction_status_type::none)
if (!_transaction_active)
{
throw sqlpp::exception{"Sqlite3 error: Cannot commit a finished or failed transaction"};
}
_transaction_status = transaction_status_type::maybe;
auto prepared = prepare_statement(_handle, "COMMIT");
execute_statement(_handle, prepared);
_transaction_status = transaction_status_type::none;
_transaction_active = false;
}
//! rollback transaction with or without reporting the rollback (or throw if the transaction has been finished
// already)
void rollback_transaction(bool report)
{
if (_transaction_status == transaction_status_type::none)
if (!_transaction_active)
{
throw sqlpp::exception{"Sqlite3 error: Cannot rollback a finished or failed transaction"};
}
@@ -506,10 +497,9 @@ namespace sqlpp
{
std::cerr << "Sqlite3 warning: Rolling back unfinished transaction" << std::endl;
}
_transaction_status = transaction_status_type::maybe;
auto prepared = prepare_statement(_handle, "ROLLBACK");
execute_statement(_handle, prepared);
_transaction_status = transaction_status_type::none;
_transaction_active = false;
}
//! report a rollback failure (will be called by transactions in case of a rollback failure in the destructor)
@@ -518,6 +508,12 @@ namespace sqlpp
std::cerr << "Sqlite3 message:" << message << std::endl;
}
//! check if transaction is active
bool is_transaction_active()
{
return _transaction_active;
}
//! get the last inserted id
uint64_t last_insert_id() noexcept
{