/*******************************************************************************
**                                                                        
** Source File Name = ininfo.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 instance 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       instInfoBuf[255]; /* buffer for instance info */
    SQLSMALLINT   outlen;   

    SQLCHAR  dbAliasBuf[SQL_MAX_DSN_LENGTH + 1]; /* buf. to read the db. dir. */
    SQLCHAR  dbCommentBuf[255] ;
    SQLSMALLINT aliasLen, commentLen ;    

    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\nINSTANCES: GETTING/SETING INFO AT INSTANCE LEVEL.\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_SERVER_NAME, instInfoBuf, 255, &outlen);
        HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ; 	
        printf("\n      Local Instance Name: %s\n", instInfoBuf);
    }
    else printf( "\nSQLGetInfo is not supported!\n" ) ;


    
    printf("\nUSE THE CLI FUNCTION\n");
    printf("-SQLDataSources\n");
    printf("TO GET:\n");    
    
    /* list the available data sources  */
    printf( "\n      The alias name and the comment\n" ) ;
    printf( "      for every database cataloged \n" ) ;
    printf( "      in the database directory of the local instance :\n\n" ) ;    
    printf( "ALIAS NAME         Comment(Description)\n" ) ;
    printf( "----------------------------------------------------\n" ) ;

    /*-->00000579.snippet */
    sqlrc = SQLDataSources( henv,
                            SQL_FETCH_NEXT,
                            dbAliasBuf,
                            SQL_MAX_DSN_LENGTH + 1,
                            &aliasLen,
                            dbCommentBuf,
                            255,
                            &commentLen );
    /* 00000579.snippet <--*/
    HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;     

    while ( sqlrc != SQL_NO_DATA_FOUND ) 
    {   printf( "%-17s  %s\n", dbAliasBuf, dbCommentBuf ) ;
        sqlrc = SQLDataSources( henv,
                                SQL_FETCH_NEXT,
                                dbAliasBuf,
                                SQL_MAX_DSN_LENGTH + 1,
                                &aliasLen,
                                dbCommentBuf,
                                255,
                                &commentLen );
        HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;       
    }


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