/******************************************************************************
**
** Source File Name = clicall.c
**
** 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 :
**      - Defines a function that is called from the embedded SQL sample
**        program mrspcli3.sqc. This function calls mrspsrv2.sqc on the server
**        which returns a result set.
**
** 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 common CLI sample code */

#define MAX_STMT_LEN 255

int
callcli( char         *procname,
         SQLDOUBLE    *sal,
         SQLSMALLINT  *salind )
{
    SQLHENV         henv;
    SQLHDBC         hdbc;
    SQLRETURN       rc;

    SQLHSTMT        hstmt;
    SQLCHAR         stmt[MAX_STMT_LEN] = "";
    SQLINTEGER      pcbValue;

    rc = SQLAllocEnv(&henv);
    if (rc == SQL_ERROR)
      return (terminate(henv, rc));

    rc = SQLAllocConnect(henv, &hdbc);
    if (rc == SQL_ERROR)
      return (terminate(henv, rc));

    rc = SQLConnect(hdbc, NULL, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS);
    if (rc == SQL_ERROR)
       return (terminate(henv, rc));

    rc = SQLAllocStmt(hdbc, &hstmt);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
    pcbValue = SQL_NULL_DATA;           /* Sal has no input, so set to null */

   /* Build the Call Statement */
    strcpy((char *)stmt, "CALL ");
    strcat((char *)stmt, procname);
    strcat((char *)stmt, "( ? )");
   /* 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, &pcbValue);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
    *salind = (SQLSMALLINT) pcbValue;

    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 (pcbValue == 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 = SQLFreeStmt(hstmt, SQL_DROP);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLTransact(henv, hdbc, SQL_COMMIT);
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

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

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

    rc = SQLFreeEnv(henv);
    if (rc != SQL_SUCCESS)
        terminate(henv, rc);

    return (SQL_SUCCESS);
};                              /* end main */