Functions for querying environment and data source information

Db2 ODBC provides functions that let applications retrieve information about the characteristics and capabilities of the current ODBC driver or the data source to which it is connected.

One of the most common situations in which functions are needed that provide driver or data source information involves displaying information for the user. Information such as the data source name and version, or the version of the Db2 ODBC driver might be displayed at connect time, or as part of the error reporting process.

Example: The following code shows an application that queries an ODBC environment for a data source, all supported functions, and a supported data type.
Figure 1. An application that queries environment information
/***********************************************************/
/*  Querying environment and data source information       */
/***********************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
void main()
{
    SQLHENV         hEnv;            /* Environment handle               */
    SQLHDBC         hDbc;            /* Connection handle                */
    SQLRETURN       rc;              /* Return code for API calls        */
    SQLHSTMT        hStmt;           /* Statement handle                 */
    SQLCHAR         dsname[30];      /* Data source name                 */
    SQLCHAR         dsdescr[255];    /* Data source description          */
    SQLSMALLINT     dslen;           /* Length of data source            */
    SQLSMALLINT     desclen;         /* Length of dsdescr                */
    BOOL            found = FALSE;
    SQLSMALLINT     funcs[100];
    SQLINTEGER      rgbValue;
    /*
     *  Initialize environment - allocate environment handle.
     */
    rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );
    rc = SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hDbc );
    /*
     *  Use SQLDataSources to verify ZOSDB2 does exist.
     */
    while( ( rc = SQLDataSources( hEnv,
                            SQL_FETCH_NEXT,
                            dsname,
                            SQL_MAX_DSN_LENGTH+1,
                            &dslen,
                            dsdescr,
                            &desclen ) ) != SQL_NO_DATA_FOUND )
    {
       if( !strcmp( dsname, "ZOSDB2" ) )   /* data source exist          */
       {
           found = TRUE;
           break;
       }
    }
    if( !found )
    {
        fprintf(stdout, "Data source 
        fprintf(stdout, "program aborted.\n");
        exit(1);
    }     
    if( ( rc = SQLConnect( hDbc, dsname, SQL_NTS, "myid", SQL_NTS,
                            "mypd", SQL_NTS ) ) 
         == SQL_SUCCESS )
    {
       fprintf( stdout, "Connect to 
    }
    SQLAllocHandle( SQL_HANDLE_STMT, hDbc, &hStmt );
    /*
     *   Use SQLGetFunctions to store all APIs status.
     */
    rc = SQLGetFunctions( hDbc, SQL_API_ALL_FUNCTIONS, funcs );
    /*
     *   Check whether SQLGetInfo is supported in this driver. If so,
     *   verify whether DATE is supported for this data source.
     */
    if( funcs[SQL_API_SQLGETINFO] == 1 )
    {
        SQLGetInfo( hDbc, SQL_CONVERT_DATE, (SQLPOINTER)&rgbValue, 255, &desclen );
        if( rgbValue & SQL_CVT_DATE )
        {
           SQLGetTypeInfo( hStmt, SQL_DATE );
           /*  use SQLBindCol and SQLFetch to retrieve data ....*/
        }
    }
}