/*******************************************************************************
**                                                                        
** Source File Name = getinfo.c  1.3                                      
**                                                                        
** 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 :                                                           
**    - Connect to a database and display database and driver information.
**    - error handling has been ignored for simplicity.
**                                                                        
** 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

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

    SQLCHAR         buffer[255];
    SQLSMALLINT     output;
    SQLUSMALLINT    supported;
    SQLSMALLINT     outlen;

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

/*--> SQLL1X07.SCRIPT 
*/
    /* Check to see if SQLGetInfo() is supported */
    rc = SQLGetFunctions(hdbc, SQL_API_SQLGetInfo, &supported);

    if (supported == SQL_TRUE) { /* get information about current connection */

        rc = SQLGetInfo(hdbc, SQL_DATA_SOURCE_NAME, buffer, 255, &outlen);
        printf("      Server Name: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_DATABASE_NAME,    buffer, 255, &outlen);
        printf("    Database Name: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_SERVER_NAME,      buffer, 255, &outlen);
        printf("    Instance Name: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_DBMS_NAME,        buffer, 255, &outlen);
        printf("        DBMS Name: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_DBMS_VER,         buffer, 255, &outlen);
        printf("     DBMS Version: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_DRIVER_NAME,      buffer, 255, &outlen);
        printf("   CLI Driver Name: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_DRIVER_VER,       buffer, 255, &outlen);
        printf("CLI Driver Version: %s\n", buffer);

        rc = SQLGetInfo(hdbc, SQL_ODBC_SQL_CONFORMANCE, &output,
                        sizeof(output), &outlen);
        switch (output) {
        case 0:
            strcpy((char *)buffer, "Minimum Grammar");
            break;
        case 1:
            strcpy((char *)buffer, "Core Grammar");
            break;
        case 2:
            strcpy((char *)buffer, "Extended Grammar");
            break;
        default:
            printf("Error calling getinfo!");
            return (SQL_ERROR);
        }
        printf("ODBC SQL Conformance Level: %s\n", buffer);
    }
    else printf( "SQLGetInfo is not supported!\n" ) ;

/*<-- */

    /*  Disconnect and free up CLI resources.  */

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

}