Path: blob/main/contrib/kyua/utils/sqlite/transaction.cpp
48199 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/sqlite/transaction.hpp"2930#include "utils/format/macros.hpp"31#include "utils/logging/macros.hpp"32#include "utils/noncopyable.hpp"33#include "utils/sanity.hpp"34#include "utils/sqlite/database.hpp"35#include "utils/sqlite/exceptions.hpp"36#include "utils/sqlite/statement.ipp"3738namespace sqlite = utils::sqlite;394041/// Internal implementation for the transaction.42struct utils::sqlite::transaction::impl : utils::noncopyable {43/// The database this transaction belongs to.44database& db;4546/// Possible statuses of a transaction.47enum statuses {48open_status,49committed_status,50rolled_back_status,51};5253/// The current status of the transaction.54statuses status;5556/// Constructs a new transaction.57///58/// \param db_ The database this transaction belongs to.59/// \param status_ The status of the new transaction.60impl(database& db_, const statuses status_) :61db(db_),62status(status_)63{64}6566/// Destroys the transaction.67///68/// This rolls back the transaction if it is open.69~impl(void)70{71if (status == impl::open_status) {72try {73rollback();74} catch (const sqlite::error& e) {75LW(F("Error while rolling back a transaction: %s") % e.what());76}77}78}7980/// Commits the transaction.81///82/// \throw api_error If there is any problem while committing the83/// transaction.84void85commit(void)86{87PRE(status == impl::open_status);88db.exec("COMMIT");89status = impl::committed_status;90}9192/// Rolls the transaction back.93///94/// \throw api_error If there is any problem while rolling the95/// transaction back.96void97rollback(void)98{99PRE(status == impl::open_status);100db.exec("ROLLBACK");101status = impl::rolled_back_status;102}103};104105106/// Initializes a transaction object.107///108/// This is an internal function. Use database::begin_transaction() to109/// instantiate one of these objects.110///111/// \param db The database this transaction belongs to.112sqlite::transaction::transaction(database& db) :113_pimpl(new impl(db, impl::open_status))114{115}116117118/// Destructor for the transaction.119sqlite::transaction::~transaction(void)120{121}122123124/// Commits the transaction.125///126/// \throw api_error If there is any problem while committing the transaction.127void128sqlite::transaction::commit(void)129{130_pimpl->commit();131}132133134/// Rolls the transaction back.135///136/// \throw api_error If there is any problem while rolling the transaction back.137void138sqlite::transaction::rollback(void)139{140_pimpl->rollback();141}142143144