#include <stdio.h>
#include "sqlext.h"
#include "sql.h"
#include "sqltypes.h"

#define STR_LEN 30

int main()
{
	HENV 	henv;
    HDBC 	hdbc;
    HSTMT	hstmt_sel = 0;
    HSTMT   hstmt_upd = 0;
    SDWORD	retcode;
    UCHAR	info[STR_LEN];
    int nExitCounter = 20000;

    retcode = SQLAllocEnv(&henv);
    retcode = SQLAllocConnect(henv,&hdbc);
    retcode = SQLConnect(hdbc,(UCHAR*)"postgres", SQL_NTS,(UCHAR*) "postgres", SQL_NTS, (UCHAR*)"postgres", SQL_NTS);
    if(retcode != SQL_SUCCESS)
    {
        printf("\nConnect failed\n");
        goto EXIT;
    }
#ifdef AUTO_COMMIT_OFF
    retcode = SQLSetConnectOption(hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF);
#endif
    
    SQLSMALLINT cbInfoValue;
    retcode = SQLGetInfo(hdbc, SQL_DBMS_VER, info, STR_LEN, &cbInfoValue);
    if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
    {
        printf("\nGet info failed\n");
        goto EXIT;
    }
    printf("\n Current DBMS Version is %s\n", info);

    retcode = SQLAllocStmt(hdbc,&hstmt_sel);
    if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
    {
        printf("\nSQLAllocStmt for SELECT failed\n");
        goto EXIT;
    }
    retcode = SQLAllocStmt(hdbc,&hstmt_upd);
    if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
    {
        printf("\nSQLAllocStmt for Update failed\n");
        goto EXIT;
    }

    UDWORD Counter_Id;
    SDWORD cbs_id;
    retcode = SQLBindCol(hstmt_sel, 1, SQL_C_LONG, &Counter_Id, 0, &cbs_id);
    if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
    {
        printf("\nSQLBindCol failed\n");
        goto EXIT;
    }

    UDWORD nCounterPlaceHolderValue;
    SDWORD cbnCounterPlaceHolderValue;

    retcode = SQLBindParameter( hstmt_upd, 1, SQL_PARAM_INPUT, SQL_C_LONG, 
                                SQL_INTEGER, 0, 0, &nCounterPlaceHolderValue, 0, &cbnCounterPlaceHolderValue);

    while(nExitCounter--)
    { 
        retcode = SQLExecDirect(hstmt_sel,(UCHAR*) "SELECT * FROM COUNTER_TABLE", SQL_NTS);
        if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
        {
            printf("\nSQLExecuteDirect failed\n");
            break;
        }
        retcode = SQLFetch(hstmt_sel);
        if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
        {
            printf("\nSQLFetch failed\n");
            break;
        }
        printf("\nFetch CounterId = %d", Counter_Id);

        nCounterPlaceHolderValue = Counter_Id;
        nCounterPlaceHolderValue++;
        retcode = SQLExecDirect(hstmt_upd,(UCHAR*) "UPDATE COUNTER_TABLE SET COUNTER = ?", SQL_NTS);
        if( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) )
        {
            printf("\nSQLExecDirect for update failed\n");
            break;
        }
#ifdef AUTO_COMMIT_OFF
        retcode = SQLTransact(henv,hdbc,SQL_COMMIT);
#endif
        printf("\t\t\tUpdated with %d",nCounterPlaceHolderValue);
        SQLFreeStmt(hstmt_sel,SQL_CLOSE);
        SQLFreeStmt(hstmt_upd,SQL_CLOSE);
    }

    EXIT:
#ifdef AUTO_COMMIT_OFF
        retcode = SQLTransact(henv,hdbc,SQL_ROLLBACK);
#endif
    SQLFreeStmt(hstmt_sel,SQL_CLOSE);
    SQLFreeStmt(hstmt_upd,SQL_CLOSE);
    SQLDisconnect(hdbc);
    SQLFreeConnect(hdbc);
    SQLFreeEnv(henv);

    return 0;
}

