/*
 * transaction.h
 *
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 *
 * This file is part of sqlite3cc (hereafter referred to as "this program").
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SQLITE3CC_TRANSACTION_H_
#define SQLITE3CC_TRANSACTION_H_


#include <boost/utility.hpp>


namespace sqlite
{


class database;


class basic_transaction
	:
	private boost::noncopyable
{
//______________________________________________________________________________
//                                                                 instantiation
public:

	/**
	 * Constructor that provides a database upon which to act
	 * @param database a database
	 */
	explicit basic_transaction(
		database &database );

//______________________________________________________________________________
//                                                              public interface
public:

	/**
	 * Begin the transaction
	 */
	virtual void begin();

	/**
	 * Commit the transaction
	 */
	virtual void commit();

	/**
	 * Rollback the transaction
	 */
	virtual void rollback();

//______________________________________________________________________________
//                                                                implementation
protected:

	/* the database on which to act */
	database &_database;

};


////////////////////////////////////////////////////////////////////////////////


class exclusive_transaction
	:
	public basic_transaction
{
//______________________________________________________________________________
//                                                                 instantiation
public:

	/**
	 * Constructor that provides a database upon which to act
	 * @param database a database
	 */
	explicit exclusive_transaction(
		database &database );

//______________________________________________________________________________
//                                                              public interface
public:

	/**
	 * Begin the transaction
	 */
	virtual void begin();

};


////////////////////////////////////////////////////////////////////////////////


class recursive_transaction
	:
	public basic_transaction
{
//______________________________________________________________________________
//                                                                 instantiation
public:

	/**
	 * Constructor that provides a database upon which to act
	 * @param database a database
	 */
	explicit recursive_transaction(
		database &database );

//______________________________________________________________________________
//                                                              public interface
public:

	/**
	 * Begin the transaction
	 */
	virtual void begin();

	/**
	 * Commit the transaction
	 */
	virtual void commit();

	/**
	 * Rollback the transaction
	 */
	virtual void rollback();

//______________________________________________________________________________
//                                                                implementation
protected:

	/* this transaction's savepoint name */
	std::string _sp_name;

};


////////////////////////////////////////////////////////////////////////////////


template< class T = basic_transaction >
class transaction_guard
	:
	private boost::noncopyable
{
//______________________________________________________________________________
//                                                                 instantiation
public:

	/**
	 * Constructor that provides a database upon which to act
	 * @param database a database
	 */
	explicit transaction_guard(
		database &database )
		:
		_transaction( database ),
		_released( false )
	{
		_transaction.begin();
	}

	~transaction_guard()
	{
		if( !_released )
			_transaction.rollback();
	}

//______________________________________________________________________________
//                                                              public interface
public:

	/**
	 * Commit the transaction
	 */
	void commit()
	{
		if( !_released ) {
			_transaction.commit();
			_released = true;
		}
	}

//______________________________________________________________________________
//                                                                implementation
protected:

	/* the transaction */
	T _transaction;

	/* have we released the transaction yet? */
	bool _released;

};


} // namespace sqlite


#endif /* SQLITE3CC_TRANSACTION_H_ */
