// SPDX-FileCopyrightText: 2023 Yury Gubich // SPDX-License-Identifier: GPL-3.0-or-later #include "transaction.h" DB::MySQL::Transaction::Transaction(MYSQL* connection): con(connection), opened(false) { if (mysql_autocommit(con, 0) != 0) throw std::runtime_error(std::string("Failed to start transaction") + mysql_error(con)); opened = true; } DB::MySQL::Transaction::~Transaction() { if (opened) abort(); } void DB::MySQL::Transaction::commit() { if (mysql_commit(con) != 0) throw std::runtime_error(std::string("Failed to commit transaction") + mysql_error(con)); opened = false; if (mysql_autocommit(con, 1) != 0) throw std::runtime_error(std::string("Failed to return autocommit") + mysql_error(con)); } void DB::MySQL::Transaction::abort() { opened = false; if (mysql_rollback(con) != 0) throw std::runtime_error(std::string("Failed to rollback transaction") + mysql_error(con)); if (mysql_autocommit(con, 1) != 0) throw std::runtime_error(std::string("Failed to return autocommit") + mysql_error(con)); }