/*******************************************************************************
**                                                                        
** Source File Name = dtinfo.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 get info about data types.
**                                                                        
** 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 */


int DtInfoGet( SQLHANDLE) ;

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

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

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

    printf("\n\nDATA TYPES: HOW TO GET INFO ABOUT DATA TYPES.\n");

    /* initialize the CLI application */
    rc = CLIAppInit( dbAlias, user, pswd, &henv, &hdbc, 
                     (SQLPOINTER)SQL_AUTOCOMMIT_ON);
    if ( rc != 0 ) return( rc ) ;


    rc = DtInfoGet( hdbc) ;
    /* terminate the CLI application */
    rc = CLIAppTerm( &henv, &hdbc, dbAlias);
    return( rc ) ;
}                                  /* end main */
    

/******************************************************************************
**    DtInfoGet
******************************************************************************/
int DtInfoGet( SQLHANDLE hdbc)
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 
    SQLHANDLE   hstmt ;  /* statement handle */

    struct
    {   SQLINTEGER ind ;
        SQLCHAR    val[129] ;
    } dtName;   

    struct 
    {   SQLINTEGER ind ;
        SQLSMALLINT val ;
    } dtCode, dtNullable, dtCaseSens; 

    struct
    {   SQLINTEGER ind;
        SQLINTEGER val;
    } dtPrecision;

    SQLINTEGER      count = 0;
    char            truefalse[2][6] = {{"FALSE"}, {"TRUE"}};    

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLSetConnectAttr\n-SQLAllocHandle\n");
    printf("-SQLGetTypeInfo\n-SQLBindCol\n");    
    printf("-SQLFetch\n-SQLFreeHandle\n");    
    printf("TO GET INFO ABOUT DATA TYPES:\n");   

    /* set AUTOCOMMIT on */
    sqlrc = SQLSetConnectAttr( hdbc,
                               SQL_ATTR_AUTOCOMMIT,
                               (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS) ;
    DBC_HANDLE_CHECK( hdbc, sqlrc);
    
    /* allocate a statement handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ;
    DBC_HANDLE_CHECK( hdbc, sqlrc);

    /*--> 00000627.snippet */
    /* call SQLTables */ 
    printf("\n    Call SQLGetTypeInfo.\n");
    sqlrc = SQLGetTypeInfo( hstmt, SQL_ALL_TYPES); 
    STMT_HANDLE_CHECK( hstmt, sqlrc);
    /* 00000627.snippet <--*/

    /* bind columns to variables */
    sqlrc = SQLBindCol( hstmt, 1, SQL_C_CHAR, ( SQLPOINTER)dtName.val, 129,
                        &dtName.ind ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc);
    sqlrc = SQLBindCol( hstmt, 2, SQL_C_DEFAULT, ( SQLPOINTER)&dtCode.val,
                        sizeof(dtCode.val), &dtCode.ind ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc);    
    sqlrc = SQLBindCol( hstmt, 3, SQL_C_DEFAULT, ( SQLPOINTER)&dtPrecision.val,
                        sizeof(dtPrecision.val), &dtPrecision.ind ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc); 
    sqlrc = SQLBindCol( hstmt, 7, SQL_C_DEFAULT, ( SQLPOINTER)&dtNullable.val,
                        sizeof(dtNullable.val), &dtNullable.ind ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc);     
    sqlrc = SQLBindCol( hstmt, 8, SQL_C_DEFAULT, ( SQLPOINTER)&dtCaseSens.val,
                        sizeof(dtCaseSens.val), &dtCaseSens.ind ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc);       

    /* fetch each row, and display */ 
    printf("    Fetch each row and dispaly.\n");
    printf("Datatype                  Datatype Precision  Nullab. Case\n");
    printf("Typename                   (int)                      Sensit.\n");
    printf("------------------------- -------- ---------- ------- -------\n");
    sqlrc = SQLFetch( hstmt );
    STMT_HANDLE_CHECK( hstmt, sqlrc);
    if (sqlrc == SQL_NO_DATA_FOUND)
    {   printf("\n    Data not found.\n");
    }
    while (sqlrc != SQL_NO_DATA_FOUND)     
    {   printf("%-25s ", dtName.val);
        printf("%8d ", dtCode.val);
        printf("%10ld ", dtPrecision.val);
        printf("%-7s ", truefalse[dtNullable.val]);
        printf("%-7s\n", truefalse[dtCaseSens.val]);
	
        sqlrc = SQLFetch( hstmt );
        STMT_HANDLE_CHECK( hstmt, sqlrc);
    }	    

    /* free the statement handle */
    sqlrc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
    STMT_HANDLE_CHECK( hstmt, sqlrc);

    return(rc);    
}