/*******************************************************************************
**
** Source File Name = mrspcli.c  1.3
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1999
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**  Client sample for the outmrsp 'output' stored procedure sample
**  that demonstrates returning a result set from a stored procedure.
**  Only the line:
**          SQLCHAR stmt[] = "CALL mrspsrv( ? )";
**  is different between this program and mrspcli2.c.
**  See mrspsrv.c for details on what happens on the server side.
**
**
** For more information about these samples see the README file.
**
** For more information on programming in CLI see the:
**     - "Building CLI Applications" section of the Application Building Guide, and the
**     - CLI Guide and Reference.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
#include "samputil.h"          /* Header file for CLI sample code */

/*
 Global Variables for user id and password.
 To keep samples simple, not a recommended practice.
*/
extern SQLCHAR server[SQL_MAX_DSN_LENGTH + 1] ;
extern SQLCHAR uid[MAX_UID_LENGTH + 1] ;
extern SQLCHAR pwd[MAX_PWD_LENGTH + 1] ;

/* main */

int main( int argc, char * argv[] ) {

    SQLHANDLE henv, hdbc, hstmt ;
    SQLRETURN rc ;

/*--> SQLL1X45.SCRIPT */

    SQLCHAR * stmt = ( SQLCHAR * ) "CALL mrspsrv( ? )" ;

/* Declare Local Variables for Holding Returned Data */
     SQLDOUBLE   sal     = 0.0;  /* Bound to parameter marker in stmt */
     SQLINTEGER  salind  = 0;    /* Indicator variable for sal */
/*<-- */

    /* macro to initalize server, uid and pwd */
    INIT_UID_PWD ;

    /* allocate an environment handle */
    rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
    if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;

    /* allocate a connect handle, and connect */
    rc = DBconnect( henv, &hdbc ) ;
    if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;

    rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

/*--> */
   /**********************************************************\
   * Call the Remote Procedure via CALL with bound parameters *
   \**********************************************************/
   printf("Use CALL with Host Variable to invoke the Server Procedure "
      "named mrspsrv\n");
   salind = SQL_NULL_DATA;                  /* Sal has no input, so set to null */

   /* Prepare the call statement */
    rc = SQLPrepare(hstmt, stmt, SQL_NTS);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /* Bind the parameter to application variables (sal and salind) */
    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE,
                      0, 0, &sal, 0, &salind);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    SQLExecute(hstmt);
    /* Ignore Warnings */
    if (rc != SQL_SUCCESS & rc != SQL_SUCCESS_WITH_INFO)
       CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

   printf("Server Procedure Complete.\n");

   /* Print Salary Returned in the bound application variables */
   if (salind == SQL_NULL_DATA) /* Check for null value */
      printf("Median Salary = NULL\n");
   else
      printf("Median Salary = %.2f\n\n", sal );
/*<-- */

    /* Assume only 1 result set */
    rc = print_results(hstmt);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /* COMMIT, free resources and exit */

    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

    printf( "\n>Disconnecting .....\n" ) ;
    rc = SQLDisconnect( hdbc ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

    rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

    rc = SQLFreeHandle( SQL_HANDLE_ENV,  henv ) ;
    if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;

    return( SQL_SUCCESS ) ;

}                            /* end main */