/*******************************************************************************
**                                                                        
** Source File Name = apinfo.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 application 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 */

int EnvAttrSetGet( SQLHANDLE) ;
int SuppFunctionsList( SQLHANDLE, char *, char *, char *) ;

/*******************************************************************
** main
*******************************************************************/
int main( int argc, char * argv[] )
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 
    SQLHANDLE   henv;  /* environment handle */
    
    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\nCLI APP.: HOW TO GET/SET INFO AT CLI APP. LEVEL.\n");


    /* allocate an environment handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
    if ( sqlrc != SQL_SUCCESS ) 
    {   printf( "\n--ERROR while allocating the environment handle.\n" ) ;
        printf( "  sqlrc             = %d\n", sqlrc);
        printf( "  line              = %d\n", __LINE__);
        printf( "  file              = %s\n", __FILE__);	    
        return( 1 ) ;
    }


    rc = EnvAttrSetGet( henv) ;    
    rc = SuppFunctionsList( henv, dbAlias, user, pswd) ;    

    /* free the environment handle */ 
    sqlrc = SQLFreeHandle( SQL_HANDLE_ENV,  henv ) ;
    ENV_HANDLE_CHECK( henv, sqlrc ) ;

    return( 0 ) ;
}                                  /* end main */
 
/******************************************************************************
**    EnvAttrSetGet
******************************************************************************/
int EnvAttrSetGet( SQLHANDLE henv )
{   SQLRETURN  sqlrc = SQL_SUCCESS;
    int        rc = 0;

    SQLINTEGER output_nts;    

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLSetEnvAttr\n-SQLGetEnvAttr\n");
    printf("TO SET/GET ENVIRONMENT ATTRIBUTES:\n");    

 
    printf( "\n    Set the env. attr. SQL_ATTR_OUTPUT_NTS to TRUE.\n" ) ;
    sqlrc = SQLSetEnvAttr( henv,
                           SQL_ATTR_OUTPUT_NTS,
                           ( SQLPOINTER ) SQL_TRUE,
                           0 ) ;
    ENV_HANDLE_CHECK( henv, sqlrc) ;    

    printf( "    Get the env. attr. SQL_ATTR_OUTPUT_NTS.\n" ) ;
    /*-->00000611.snippet */
    sqlrc = SQLGetEnvAttr( henv, SQL_ATTR_OUTPUT_NTS, &output_nts, 0, NULL ) ;
    ENV_HANDLE_CHECK( henv, sqlrc) ;    
    /* 00000611.snippet <--*/
    printf( "        Null Termination of Output strings is: " ) ;
    if ( output_nts == SQL_TRUE ) printf( "True\n" ) ;
    else printf( "False\n" ) ;

    

    return( 0 ) ;
}

typedef struct {
  SQLUSMALLINT id ;
  char * name ;
} functionInfo ;

functionInfo functions[] = {
  { SQL_API_SQLAllocConnect,     "SQLAllocConnect"     },
  { SQL_API_SQLAllocEnv,         "SQLAllocEnv"         },
  { SQL_API_SQLAllocHandle,      "SQLAllocHandle"      },
  { SQL_API_SQLAllocStmt,        "SQLAllocStmt"        },
  { SQL_API_SQLBindCol,          "SQLBindCol"          },
  { SQL_API_SQLBindFileToCol,    "SQLBindFileToCol"    },
  { SQL_API_SQLBindFileToParam,  "SQLBindFileToParam"  },
  { SQL_API_SQLBINDPARAM,        "SQLBINDPARAM"        },
  { SQL_API_SQLBindParameter,    "SQLBindParameter"    },
  { SQL_API_SQLBrowseConnect,    "SQLBrowseConnect"    },
  { SQL_API_SQLCancel,           "SQLCancel"           },
  { SQL_API_SQLCloseCursor,      "SQLCloseCursor"      },
  { SQL_API_SQLColAttribute,     "SQLColAttribute"     },
  { SQL_API_SQLColAttributeS,    "SQLColAttributeS"    },
  { SQL_API_SQLColumnPrivileges, "SQLColumnPrivileges" },
  { SQL_API_SQLColumns,          "SQLColumns"          },
  { SQL_API_SQLConnect,          "SQLConnect"          },
  { SQL_API_SQLCopyDesc,         "SQLCopyDesc"         },
  { SQL_API_SQLDataSources,      "SQLDataSources"      },
  { SQL_API_SQLDescribeCol,      "SQLDescribeCol"      },
  { SQL_API_SQLDescribeParam,    "SQLDescribeParam"    },
  { SQL_API_SQLDisconnect,       "SQLDisconnect"       },
  { SQL_API_SQLDriverConnect,    "SQLDriverConnect"    },
  { SQL_API_SQLEndTran,          "SQLEndTran"          },
  { SQL_API_SQLError,            "SQLError"            },
  { SQL_API_SQLExecDirect,       "SQLExecDirect"       },
  { SQL_API_SQLExecute,          "SQLExecute"          },
  { SQL_API_SQLExtendedFetch,    "SQLExtendedFetch"    },
  { SQL_API_SQLFetch,            "SQLFetch"            },
  { SQL_API_SQLFetchSCROLL,      "SQLFetchSCROLL"      },
  { SQL_API_SQLForeignKeys,      "SQLForeignKeys"      },
  { SQL_API_SQLFreeConnect,      "SQLFreeConnect"      },
  { SQL_API_SQLFreeEnv,          "SQLFreeEnv"          },
  { SQL_API_SQLFreeHandle,       "SQLFreeHandle"       },
  { SQL_API_SQLFreeStmt,         "SQLFreeStmt"         },
  { SQL_API_SQLGetConnectAttr,   "SQLGetConnectAttr"   },
  { SQL_API_SQLGetConnectOption, "SQLGetConnectOption" },
  { SQL_API_SQLGetCursorName,    "SQLGetCursorName"    },
  { SQL_API_SQLGetData,          "SQLGetData"          },
  { SQL_API_SQLGetDescField,     "SQLGetDescField"     },
  { SQL_API_SQLGetDescRec,       "SQLGetDescRec"       },
  { SQL_API_SQLGetDiagField,     "SQLGetDiagField"     },
  { SQL_API_SQLGetDiagRec,       "SQLGetDiagRec"       },
  { SQL_API_SQLGetEnvAttr,       "SQLGetEnvAttr"       },
  { SQL_API_SQLGetFunctions,     "SQLGetFunctions"     },
  { SQL_API_SQLGetInfo,          "SQLGetInfo"          },
  { SQL_API_SQLGetLength,        "SQLGetLength"        },
  { SQL_API_SQLGetPosition,      "SQLGetPosition"      },
  { SQL_API_SQLGetSQLCA,         "SQLGetSQLCA"         },
  { SQL_API_SQLGetStmtAttr,      "SQLGetStmtAttr"      },
  { SQL_API_SQLGetStmtOption,    "SQLGetStmtOption"    },
  { SQL_API_SQLGetSubString,     "SQLGetSubString"     },
  { SQL_API_SQLGetTypeInfo,      "SQLGetTypeInfo"      },
  { SQL_API_SQLMoreResults,      "SQLMoreResults"      },
  { SQL_API_SQLNativeSql,        "SQLNativeSql"        },
  { SQL_API_SQLNumParams,        "SQLNumParams"        },
  { SQL_API_SQLNumResultCols,    "SQLNumResultCols"    },
  { SQL_API_SQLParamData,        "SQLParamData"        },
  { SQL_API_SQLParamOptions,     "SQLParamOptions"     },
  { SQL_API_SQLPrepare,          "SQLPrepare"          },
  { SQL_API_SQLPrimaryKeys,      "SQLPrimaryKeys"      },
  { SQL_API_SQLProcedureColumns, "SQLProcedureColumns" },
  { SQL_API_SQLProcedures,       "SQLProcedures"       },
  { SQL_API_SQLPutData,          "SQLPutData"          },
  { SQL_API_SQLRowCount,         "SQLRowCount"         },
  { SQL_API_SQLSetColAttributes, "SQLSetColAttributes" },
  { SQL_API_SQLSetConnectAttr,   "SQLSetConnectAttr"   },
  { SQL_API_SQLSetConnection,    "SQLSetConnection"    },
  { SQL_API_SQLSetConnectOption, "SQLSetConnectOption" },
  { SQL_API_SQLSetCursorName,    "SQLSetCursorName"    },
  { SQL_API_SQLSetDescField,     "SQLSetDescField"     },
  { SQL_API_SQLSetDescRec,       "SQLSetDescRec"       },
  { SQL_API_SQLSetEnvAttr,       "SQLSetEnvAttr"       },
  { SQL_API_SQLSetParam,         "SQLSetParam"         },
  { SQL_API_SQLSetPos,           "SQLSetPos"           },
  { SQL_API_SQLSETSCROLLOPTIONS, "SQLSETSCROLLOPTIONS" },
  { SQL_API_SQLSETSTMTATTR,      "SQLSETSTMTATTR"      },
  { SQL_API_SQLSetStmtOption,    "SQLSetStmtOption"    },
  { SQL_API_SQLSpecialColumns,   "SQLSpecialColumns"   },
  { SQL_API_SQLStatistics,       "SQLStatistics"       },
  { SQL_API_SQLTablePrivileges,  "SQLTablePrivileges"  },
  { SQL_API_SQLTables,           "SQLTables"           },
  { SQL_API_SQLTransact,         "SQLTransact"         },
  { 0,                           ( char * ) 0          }
} ;


/******************************************************************************
**    SuppFunctionsList 
******************************************************************************/
int SuppFunctionsList( SQLHANDLE henv,
                       char dbAlias[],
                       char user[], 
                       char pswd[] )
{   SQLRETURN  sqlrc = SQL_SUCCESS;
    int        rc = 0;

    SQLHANDLE   hdbc;  /* connection handle */	 

    SQLUSMALLINT supported ;
    int func_pos ;    

    printf("\nUSE THE CLI FUNCTIONS\n");
    printf("-SQLAllocHandle\n-SQLConnect\n");
    printf("-SQLGetFunctions\n");    
    printf("-SQLDisconnect\n-SQLFreeHandle\n");    
    printf("TO LIST THE SUPPORTED CLI FNCTIONS:\n");    

    /* allocate a database connection handle */
    sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_ENV, henv, sqlrc, &henv, &hdbc ) ;

    /* connect to the database */
    printf( "\n    Connecting to the database %s ...\n", dbAlias ) ;    
    sqlrc = SQLConnect( hdbc,
                     (SQLCHAR *)dbAlias, SQL_NTS,
                     (SQLCHAR *)user, SQL_NTS,
                     (SQLCHAR *)pswd, SQL_NTS
                   ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;     
    printf( "    Connected to the database %s.\n", dbAlias ) ;


    printf("    List the supported CLI functions:\n");    
    
    func_pos = 0;
    while ( functions[func_pos].id != 0 )
    {   sqlrc = SQLGetFunctions( hdbc,
                                 functions[func_pos].id,
                                 &supported ) ;
        HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;    
	
        if ( supported )
            printf( "        %-20s is supported\n",
                    functions[func_pos].name ) ;
        else
            printf( "        %-20s is not supported\n",
                  functions[func_pos].name ) ;

        func_pos++ ;
    }

    
    /*********   Stop using the connection  **************************/
    
    /* disconnect from the database */   
    printf( "\n    Disconnecting from the database %s ...\n", dbAlias ) ;
    sqlrc = SQLDisconnect( hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;
    printf( "    Disconnected from the database %s.\n", dbAlias ) ;    

    /* free the connection handle */    
    sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;    
 
    return( 0 ) ;
}