/*
*	©Copyright 2002-2004, Microsoft Corporation
*	Microsoft Confidential.
*	Written by Sergey Vasilevskiy, 2002-2004.
*/

/*
*	Data Maintenance database class that uses stored procedures for individual frames.
*/

#include "stdafx.h"

using namespace TPCE;

CDataMaintenanceDB::CDataMaintenanceDB(CDBConnection *pDBConn)
: CTxnHarnessDBBase(pDBConn)
{
	int i;	

	//Allocate one handle per stored procedure (frame)
	if (SQLAllocHandle(SQL_HANDLE_STMT, m_hdbc, &m_hstmtFrame1) != SQL_SUCCESS)
		ThrowError(CODBCERR::eAllocHandle, SQL_HANDLE_STMT, m_hdbc);	

	//Bind input/output parameters for each stored procedure

	//Frame1 input
	i = 0;
	if (   SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_SMALLINT, 0, 0, &Frame1Input.vol_incr, 0, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_IDENT, SQL_IDENT, 0, 0, &Frame1Input.c_id, 0, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_IDENT, SQL_IDENT, 0, 0, &Frame1Input.acct_id, 0, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_IDENT, SQL_IDENT, 0, 0, &Frame1Input.co_id, 0, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_TINYINT, 0, 0, &Frame1Input.day_of_month, 0, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, cSYMBOL_len+1, 0, &Frame1Input.symbol, cSYMBOL_len+1, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, cTX_ID_len+1, 0, &Frame1Input.tx_id, cTX_ID_len+1, NULL)!=SQL_SUCCESS
		|| SQLBindParameter(m_hstmtFrame1, ++i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, max_table_name+1, 0, &Frame1Input.table_name, max_table_name+1, NULL)!=SQL_SUCCESS
		 )
		ThrowError(CODBCERR::eBindParam, SQL_HANDLE_STMT, m_hstmtFrame1);
};

CDataMaintenanceDB::~CDataMaintenanceDB()
{
	SQLFreeHandle(SQL_HANDLE_STMT, m_hstmtFrame1);
}

void CDataMaintenanceDB::DoDataMaintenanceFrame1(const TDataMaintenanceFrame1Input *pFrame1Input)
{
	int rc;

	//copy input parameters into bound locations
	Frame1Input = *pFrame1Input;

	rc = SQLExecDirect(m_hstmtFrame1, 
						(SQLCHAR*)"{call DataMaintenanceFrame1(?, ?, ?, ?, ?, ?, ?, ?)}", SQL_NTS);
	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
		ThrowError(CODBCERR::eExecDirect, SQL_HANDLE_STMT, m_hstmtFrame1, "DoDataMaintenanceFrame1");
};

void CDataMaintenanceDB::Cleanup(void* pException)
{
	SQLCloseCursor(m_hstmtFrame1);
}
