/*******************************************************************************
**                                                                        
** Source File Name = apsqlca.c                                      
**                                                                        
** Licensed Materials - Property of IBM                                   
**                                                                        
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000
** All Rights Reserved.                                                   
**                                                                        
** US Government Users Restricted Rights - Use, duplication or            
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      
**                                                                        
**                                                                        
**    PURPOSE :                                                           
**    Shows how to use the CLI function SQLGetSQLCA.
**                                                                        
** 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 "utilcli.h"          /* Header file for CLI sample code */

/*******************************************************************
** main
*******************************************************************/
int main( int argc, char * argv[] )
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 
    SQLHANDLE   henv;  /* environment handle */
    SQLHANDLE   hdbc;  /* connection handle */
    SQLHANDLE   hstmt; /* statement handle */  

    SQLCHAR *   stmt1 = ( SQLCHAR * ) "SELECT * FROM org";    
    SQLCHAR *   stmt2 = ( SQLCHAR * ) "SELECT * FROM staff";       

    char       dbAlias[SQL_MAX_DSN_LENGTH + 1] ;
    char       user[MAX_UID_LENGTH + 1] ;
    char       pswd[MAX_PWD_LENGTH + 1] ;

    struct sqlca    sqlca;    

    /* checks the command line arguments */
    rc = CmdLineArgsCheck1( argc, argv, dbAlias, user, pswd );
    if ( rc != 0 ) return( rc ) ;

    printf("\n\nCLI APP.: HOW TO USE SQLGetSQLCA.\n");

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLAllocHandle\n-SQLSetConnectAttr\n");
    printf("-SQLConnect\n-SQLSetStmtAttr\n");  
    printf("-SQLPrepare\n-SQLGetSQLCA\n");    
    printf("-SQLDisconnect\n-SQLFreeHandle\n");    
    printf("TO SHOW HOW TO USE SQLGetSQLCA:\n");    
    

    /* allocate an environment handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
    if ( sqlrc != SQL_SUCCESS ) 
    {   printf( "\n--ERROR while allocating the environment handle.\n" ) ;
        printf( "  sqlrc             = %d\n", sqlrc);
        printf( "  line              = %d\n", __LINE__);
        printf( "  file              = %s\n", __FILE__);	    
        return( 1 ) ;
    }

    /* allocate a database connection handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;

    /* set AUTOCOMMIT on */
    sqlrc = SQLSetConnectAttr( hdbc,
                               SQL_ATTR_AUTOCOMMIT,
                               (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;     
    
    
    /* connect to the database */
    printf( "\n    Connecting to the database %s ...\n", dbAlias ) ;    
    sqlrc = SQLConnect( hdbc,
                     (SQLCHAR *)dbAlias, SQL_NTS,
                     (SQLCHAR *)user, SQL_NTS,
                     (SQLCHAR *)pswd, SQL_NTS
                   ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;     
    printf( "    Connected to the database %s.\n", dbAlias ) ;

    /* allocate one or more statement handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;  

    /* disable deferred prepare*/
    printf("\n    Disable deffered prepare.\n");    
    sqlrc = SQLSetStmtAttr( hstmt,
                         SQL_ATTR_DEFERRED_PREPARE,
                         (SQLPOINTER) SQL_DEFERRED_PREPARE_OFF ,
                         0); 
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;  

    /*********   Start using the statement handle *******************/

    /* prepare the statement */    
    printf("    Prepare the statement\n");    
    printf("        %s\n", stmt1);    
    sqlrc = SQLPrepare( hstmt, stmt1, SQL_NTS ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;  

    /*--> 00000622.snippet */
    /* call SQLGetSQLCA */    
    printf("    Call SQLGetSQLCA and print some SQLCA fields.\n");    
    sqlrc = SQLGetSQLCA( henv, hdbc, hstmt, &sqlca ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;  
    /* 00000622.snippet <--*/
    printf("        Relative cost estimate of the resources\n");
    printf("          required to process the statement = %lu\n",
           sqlca.sqlerrd[3]);    
    printf("        Estimate of the number of rows that will be returned\n");
    printf("          to the user when the statement is executed = %ld\n", 
           sqlca.sqlerrd[2]);     

    /* prepare the statement */    
    printf("    Prepare the statement\n");    
    printf("        %s\n", stmt2);    
    sqlrc = SQLPrepare( hstmt, stmt2, SQL_NTS ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;  

    /* call SQLGetSQLCA */    
    printf("    Call SQLGetSQLCA and print some SQLCA fields.\n");    
    sqlrc = SQLGetSQLCA( henv, hdbc, hstmt, &sqlca ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;  

    printf("        Relative cost estimate of the resources\n");
    printf("          required to process the statement = %lu\n",
           sqlca.sqlerrd[3]);    
    printf("        Estimate of the number of rows that will be returned\n");
    printf("          to the user when the statement is executed = %ld\n", 
           sqlca.sqlerrd[2]);    

    /*********   Stop using the statement handles ********************/

    /* free the statement handle */
    sqlrc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;    
    
    /* disconnect from the database */   
    printf( "\n    Disconnecting from the database %s ...\n", dbAlias ) ;
    sqlrc = SQLDisconnect( hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
    printf( "    Disconnected from the database %s.\n", dbAlias ) ;    

    /* free the connection handle */    
    sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;   
    

    /* free the environment handle */ 
    sqlrc = SQLFreeHandle( SQL_HANDLE_ENV,  henv ) ;
    ENV_HANDLE_CHECK( henv, sqlrc ) ;

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