//
// PostgreSQLStatementImpl.cpp
//
// $Id: //poco/1.4/Data/PostgreSQL/src/PostgreSQLStatementImpl.cpp#1 $
//
// Library: Data
// Package: PostgreSQL
// Module:  PostgreSQLStatementImpl
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Poco/Data/PostgreSQL/PostgreSQLStatementImpl.h"

namespace Poco {
namespace Data {
namespace PostgreSQL {


PostgreSQLStatementImpl::PostgreSQLStatementImpl(SessionImpl& aSessionImpl)
:	Poco::Data::StatementImpl	(aSessionImpl),
	_statementExecutor			(aSessionImpl.handle()),
	_pBinder					(new Binder),
	_pExtractor					(new Extractor (_statementExecutor)),
	_hasNext					(NEXT_DONTKNOW)
{
}


PostgreSQLStatementImpl::~PostgreSQLStatementImpl()
{
}


std::size_t
PostgreSQLStatementImpl::columnsReturned() const
{
	return _statementExecutor.columnsReturned();
}


int
PostgreSQLStatementImpl::affectedRowCount() const
{
	return _statementExecutor.getAffectedRowCount();
}


const MetaColumn&
PostgreSQLStatementImpl::metaColumn(std::size_t aPosition, std::size_t aDataset) const
{
	// PostgreSql doesn't support multiple result sets
	poco_assert_dbg(aDataset == 0);
	
	return _statementExecutor.metaColumn(aPosition);
}


bool
PostgreSQLStatementImpl::hasNext()
{
	if (NEXT_DONTKNOW == _hasNext)
	{
		if (columnsReturned() == 0)
		{
			return false;
		}

		if (_statementExecutor.fetch())
		{
			_hasNext = NEXT_TRUE;
			return true;
		}

		_hasNext = NEXT_FALSE;
		return false;
	}
	else
		if (NEXT_TRUE == _hasNext)
		{
			return true;
		}

	return false;
}


std::size_t
PostgreSQLStatementImpl::next()
{
	if (! hasNext())
	{
		throw StatementException("No data received");
	}

	Poco::Data::AbstractExtractionVec::iterator it= extractions().begin();
	Poco::Data::AbstractExtractionVec::iterator itEnd = extractions().end();
	
	std::size_t position = 0;

	for (; it != itEnd; ++it)
	{
		(*it)->extract(position);
		position += (*it)->numOfColumnsHandled();
	}

	_hasNext = NEXT_DONTKNOW;

	return 1;
}


bool
PostgreSQLStatementImpl::canBind() const
{
	bool ret = false;

	if ((_statementExecutor.state() >= StatementExecutor::STMT_COMPILED)
		 && ! bindings().empty())
	{
		ret = (*bindings().begin())->canBind();
	}

	return ret;
}


bool
PostgreSQLStatementImpl::canCompile() const
{
	return (_statementExecutor.state() < StatementExecutor::STMT_COMPILED);
}


void
PostgreSQLStatementImpl::compileImpl()
{
	_statementExecutor.prepare(toString());
}


void
PostgreSQLStatementImpl::bindImpl()
{
	Poco::Data::AbstractBindingVec& binds = bindings();

	std::size_t position = 0;
	Poco::Data::AbstractBindingVec::iterator it= binds.begin();
	Poco::Data::AbstractBindingVec::iterator itEnd = binds.end();

	for (; it != itEnd && (*it)->canBind(); ++it)
	{
		(*it)->bind(position);
		position += (*it)->numOfColumnsHandled();
	}

	_pBinder->updateBindVectorToCurrentValues();

	_statementExecutor.bindParams(_pBinder->bindVector());

	_statementExecutor.execute();

	_hasNext = NEXT_DONTKNOW;
}


Poco::Data::AbstractExtractor::Ptr PostgreSQLStatementImpl::extractor()
{
	return _pExtractor;
}


Poco::Data::AbstractBinder::Ptr PostgreSQLStatementImpl::binder()
{
	return _pBinder;
}


} } } // namespace Poco::Data::PostgreSQL
