/*******************************************************************************
**                                                                        
** Source File Name = sendda.c  1.4                                      
**                                                                        
** 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 :                                                           
**      An input/output stored procedure sample.  This is the calling
**      application.  The showda procedure is called, which accepts any
**      number of arguments and modifies the argument values.
**                                                                        
** 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 */

/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
   /* Need to include ASLM for 68K applications */
   #include <LibraryManager.h>
#endif

#define NUM_SP_PARAMS 9

/*
 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 ;

/*--> SQLL1X42.SCRIPT */

    SQLCHAR * stmt = ( SQLCHAR * ) "CALL showda(?, ?, ?, ?, ?, ?, ?, ?, ?)" ;

/*<-- */

    SQLINTEGER ind[NUM_SP_PARAMS] ;
    SQLCHAR returnbuf[1024] = "Empty" ;
   
    SQLCHAR * blob = ( SQLCHAR * ) "\xff\x01\xff\x02\xff\x03\xff\x04\xff\x05" ;

    SQLCHAR * clob = ( SQLCHAR * ) "I am a CLOB" ;
    SQLCHAR * character = ( SQLCHAR * ) "I am a CHAR   ";
    SQLCHAR * varchar = ( SQLCHAR * ) "I am a VARCHAR";
    SQLINTEGER integer = 12345678 ;
    SQLCHAR * decimal = ( SQLCHAR * ) "-0001234.56780000" ;
    SQLDOUBLE double_var = 1234.5678 ;
    struct sqlca sqlca ;
    int i ;

/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
   /* Before making any API calls for 68K environment, need to initialize the
      Library Manager */
   InitLibraryManager(0,kCurrentZone,kNormalMemory);
   atexit(CleanupLibraryManager);
#endif

    /* 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 ) ;

/*--> */

    rc = SQLPrepare( hstmt, stmt, SQL_NTS ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[0] = 10;
    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY, 
                          SQL_BLOB, 10, 0, blob, 10, &ind[0]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[1] = SQL_NTS;
    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR, 
                          SQL_CLOB, 14, 0, clob, 15, &ind[1]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[2] = SQL_NTS;
    rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR, 
           SQL_CHAR, 14, 0, character, 15, &ind[2]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[3] = SQL_NTS;
    rc = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR, 
             SQL_VARCHAR, 14, 0, varchar, 15, &ind[3]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[4] = 0;
    rc = SQLBindParameter(hstmt, 5, SQL_PARAM_INPUT_OUTPUT, SQL_C_LONG, 
                          SQL_INTEGER, 0, 0, &integer, 4, &ind[4]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[5] =SQL_NTS ;
    rc = SQLBindParameter(hstmt, 6, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR, 
                          SQL_DECIMAL, 15, 8, decimal, 18, &ind[5]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[6] = 0;
    rc = SQLBindParameter(hstmt, 7, SQL_PARAM_INPUT_OUTPUT, SQL_C_DOUBLE, 
                          SQL_DOUBLE, 0, 0, &double_var, 4, &ind[6]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    ind[7] = 0;
    rc = SQLBindParameter(hstmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL, 
	              15, 11, &double_var, 8, &ind[7]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /*
     The last parameter is an output CLOB, used by showda to send back
     the SQLDA information that the stored procedure received
     */

    ind[NUM_SP_PARAMS -1] = SQL_NTS;   
    rc = SQLBindParameter(hstmt, 9, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR, 
                          SQL_CLOB, 1023, 0, returnbuf, 1024, 
                          &ind[NUM_SP_PARAMS-1]);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    printf("Calling Stored Procedure with valid arguments\n");
    rc = SQLExecute( hstmt ) ;
    /* Ignore Warnings */
    if ( rc != SQL_SUCCESS_WITH_INFO )
       CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
	
    printf("\nReturn Buffer:\n %s \n", returnbuf);
    printf("  Blob =", clob);
        for (i = 0; i < 10; i++)
            printf("%02X", (int)blob[i]);
    printf("\n");
    printf("  Clob = %s\n", clob);
    printf("  Char = %s\n", character);
    printf("  Varchar = %s\n", varchar);
    printf("  Integer = %ld\n", integer );

    printf("  Decimal = %s\n", decimal);
    printf("  Double  = %lf\n", double_var );



    for (i=0; i<NUM_SP_PARAMS-1; i++)
       ind[i]=SQL_NULL_DATA;

    printf("\nCalling Stored Procedure with NULL arguments\n");

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

    printf("\nReturn Buffer:\n %s \n", returnbuf);
/*<-- */

    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 */