/*** @file Transaction.cpp* @ingroup SQLiteCpp* @brief A Transaction is way to group multiple SQL statements into an atomic secured operation.** Copyright (c) 2012-2025 Sebastien Rombauts (sebastien.rombauts@gmail.com)** Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt* or copy at http://opensource.org/licenses/MIT)*/#include <SQLiteCpp/Transaction.h>#include <SQLiteCpp/Database.h>#include <SQLiteCpp/Assertion.h>#include <sqlite3.h>namespace SQLite{// Begins the SQLite transactionTransaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :mDatabase(aDatabase){const char *stmt;switch (behavior) {case TransactionBehavior::DEFERRED:stmt = "BEGIN DEFERRED";break;case TransactionBehavior::IMMEDIATE:stmt = "BEGIN IMMEDIATE";break;case TransactionBehavior::EXCLUSIVE:stmt = "BEGIN EXCLUSIVE";break;default:throw SQLite::Exception("invalid/unknown transaction behavior", SQLITE_ERROR);}mDatabase.exec(stmt);}// Begins the SQLite transactionTransaction::Transaction(Database &aDatabase) :mDatabase(aDatabase){mDatabase.exec("BEGIN TRANSACTION");}// Safely rollback the transaction if it has not been committed.Transaction::~Transaction(){if (false == mbCommited){try{mDatabase.exec("ROLLBACK TRANSACTION");}catch (SQLite::Exception&){// Never throw an exception in a destructor: error if already rollbacked, but no harm is caused by this.}}}// Commit the transaction.void Transaction::commit(){if (false == mbCommited){mDatabase.exec("COMMIT TRANSACTION");mbCommited = true;}else{throw SQLite::Exception("Transaction already committed.");}}// Rollback the transactionvoid Transaction::rollback(){if (false == mbCommited){mDatabase.exec("ROLLBACK TRANSACTION");}else{throw SQLite::Exception("Transaction already committed.");}}} // namespace SQLite
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。