// gcc -o upcnt upcnt.c -lodbc
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>

#define PARAM_ARRAY_SIZE 2 // number of params in param array

void print_stmt_err(HENV henv, HDBC hdbc, HSTMT hstmt);

int main () {

  SQLHENV  henv  = SQL_NULL_HENV;   // Environment
  SQLHDBC  hdbc  = SQL_NULL_HDBC;   // Connection handle
  SQLHSTMT hstmt = SQL_NULL_HSTMT;  // Statement handle
  SQLRETURN retcode;// Return status

  SQLCHAR * strSQL   = "DELETE FROM TSTBL1 WHERE INTCLMN2 = ?";
    SQLUINTEGER   i;
    SQLLEN   RowCount;

   SQLINTEGER   pPersonIDs2 [PARAM_ARRAY_SIZE] =
     {30,10};

   SQLULEN      ParamsProcessed=0;
   SQLUINTEGER  param_count=0;

  SQLSMALLINT nSize;
  TCHAR       szState[6];
  TCHAR       szErrorMsg[1024];
  SQLINTEGER  nErrorCode = -1;


    // Allocate an environment handle
    retcode=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

    // Set ODBC Version
    retcode=SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);

    // Allocate a connection handle
    retcode=SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

    // DSN
    retcode = SQLConnect(hdbc, "MyDataSource",SQL_NTS, "kommih", SQL_NTS, "kommih123", SQL_NTS);
  if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
    /*  SQLError */
    SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, (SQLTCHAR *)szState, &nErrorCode, (SQLTCHAR *)szErrorMsg, sizeof(szErrorMsg), &nSize);
    printf("SQLConnect NG!,ERRCD=%d,MSG=%s\n",nErrorCode,szErrorMsg);
  }else{
    printf("SQLConnect OK!\n");
  }
    SQLSetConnectOption( hdbc, SQL_AUTOCOMMIT,0) ;


    // SQL_PARAM_ARRAY_ROW_COUNTS:2
    // SQL_PARC_BATCH=1
    // SQL_PARC_NO_BATCH=2
    retcode = SQLGetInfo(hdbc, SQL_PARAM_ARRAY_ROW_COUNTS, (PTR)&i, sizeof(i), NULL);

    // Allocate a statement handle
    retcode=SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

    // Setup for parameter array processing
    retcode = SQLParamOptions (hstmt, PARAM_ARRAY_SIZE, NULL);

    // SQLRowCountのかわりに追加したODBC API
    retcode = SQLSetStmtAttr (hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR,  &ParamsProcessed, 0);

  /*  // Bind array values of parameter 1 data in
    retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR,
                               SQL_CHAR, 30, 0, pPersonIDs3, 31, NULL);
    if (retcode != SQL_SUCCESS)  {
       print_stmt_err(henv, hdbc, hstmt);
       return(0);
    }
*/
    // Bind array values of parameter 2 data in
    retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG,
                               SQL_INTEGER, 0, 0, pPersonIDs2, 0, NULL);
    if (retcode != SQL_SUCCESS)  {
       print_stmt_err(henv, hdbc, hstmt);
       return(0);
    }
    retcode = SQLExecDirect (hstmt, strSQL, SQL_NTS);
    if (retcode != SQL_SUCCESS)  {
       print_stmt_err(henv, hdbc, hstmt);
       return(0);
    }

    RowCount = 0;    
    retcode=SQLRowCount(hstmt, &RowCount);
    printf("\nSQLRowCount  is : %i", RowCount);

    retcode = SQLGetInfo(hdbc, SQL_PARAM_ARRAY_ROW_COUNTS, &param_count, 0, NULL);
    //printf("\nparam_count(2) is : %i", (int)param_count);
    if (param_count & SQL_PARC_BATCH){
      printf("\n[1]Row count is : %i\n", (int)ParamsProcessed);     // 3
    }
    else{
      printf("\n[2]Row count is : %i\n", (int)RowCount);
    }


    // Free handles
    //SQLTransact (henv, hdbc, SQL_ROLLBACK);  /* commit the transaction       */
    SQLTransact (henv, hdbc, SQL_COMMIT);  /* commit the transaction       */

    // Statement
    if (hstmt != SQL_NULL_HSTMT)
      SQLFreeHandle(SQL_HANDLE_STMT, hstmt);


    printf("-------------------------------------------\n");
    // Connection
    if (hdbc != SQL_NULL_HDBC) {
      SQLDisconnect(hdbc);
      SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    }

    // Environment
    if (henv != SQL_NULL_HENV)
      SQLFreeHandle(SQL_HANDLE_ENV, henv);

    return 0;
}
void print_stmt_err(HENV henv, HDBC hdbc, HSTMT hstmt) {
    char errstate[ 256 ];
    char errmsg[ 256 ];
    SDWORD errcode;
    SWORD sz;

    SQLGetDiagRec( SQL_HANDLE_STMT, hstmt, 1,
        errstate, &errcode, errmsg, sizeof( errmsg ), &sz );
    printf( "%s(%d)%*s\n", errstate, errcode, (int)sz, errmsg );
}



