/*******************************************************************************
**                                                                        
** Source File Name = dbinfo.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 get/set info at database level.
**                                                                        
** 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 */

    SQLUSMALLINT  supported;     /* to check if SQLGetInfo() is supported */  
    SQLCHAR       dbInfoBuf[255]; /* buffer for database info */
    SQLSMALLINT   outlen;    

    SQLHANDLE   hstmt ;  /* statement handle */
    SQLINTEGER  cursor_hold, autocommit ;    

    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\nDATABASES: GETTING/SETTING INFO ABOUT DATABASES.\n");

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

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLGetFunctions\n-SQLGetInfo\n");
    printf("TO GET:\n");    
    
    /* check to see if SQLGetInfo() is supported */
    sqlrc = SQLGetFunctions(hdbc, SQL_API_SQLGetInfo, &supported);
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;     

    if (supported == SQL_TRUE)
    {   sqlrc = SQLGetInfo(hdbc, SQL_DATA_SOURCE_NAME, dbInfoBuf, 255, &outlen);
        HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; 	
        printf("\n    Database Alias: %s\n", dbInfoBuf);

        sqlrc = SQLGetInfo(hdbc, SQL_DATABASE_NAME, dbInfoBuf, 255, &outlen);
        HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; 	
        printf("     Database Name: %s\n", dbInfoBuf);
    }
    else printf( "\nSQLGetInfo is not supported!\n" ) ;

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLGetConnectAttr\n-SQLAllocHandle\n");
    printf("-SQLGetStmtAttr\n-SQLFreeHandle\n");    
    printf("TO GET:\n");    

    /*--> 00003362.snippet */
    sqlrc = SQLGetConnectAttr( hdbc, SQL_AUTOCOMMIT, &autocommit, 0, NULL ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
    printf("\n    A connection attribute...\n");
    printf( "        Autocommit is: " ) ;
    if ( autocommit == SQL_AUTOCOMMIT_ON ) printf( "ON\n" ) ;
    else printf( "OFF\n" ) ;
    /* 00003362.snippet <--*/

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

    /*--> 00003365.snippet */
    sqlrc = SQLGetStmtAttr( hstmt, SQL_CURSOR_HOLD, &cursor_hold, 0, NULL ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;
    printf("    A statement attribute...\n");
    printf( "        Cursor With Hold is: " ) ;
    if ( cursor_hold == SQL_CURSOR_HOLD_ON ) printf( "ON\n" ) ;
    else printf( "OFF\n" ) ;
    /* 00003365.snippet <--*/

    /* free the statement handle */
    sqlrc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
    HANDLE_CHECK( SQL_HANDLE_STMT, hstmt, sqlrc, &henv, &hdbc ) ;
    
    
    /* terminate the CLI application */
    rc = CLIAppTerm( &henv, &hdbc, dbAlias);
    return( rc ) ;
}                                  /* end main */