/*******************************************************************************
**                                                                        
** Source File Name = getfuncs.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
**    - Display connect information
**    - List all functions, and if they are supported
**                                                                        
** 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

/*--> SQLL1X06.SCRIPT */

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

/*<-- */

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

    SQLUSMALLINT supported ;

    int func_pos, side ;
    char b_line[4], e_line[3] ;

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

    /* Print connection information */
    print_connect_info( hdbc ) ;

/*--> */

    func_pos = 0 ;
    side = 0 ;
    *b_line = '\0' ;
    *e_line = '\0' ;

    while ( functions[func_pos].id != 0 ) {
       SQLGetFunctions( hdbc,
                        functions[func_pos].id,
                        &supported
                      ) ;
       if ( supported )
          printf( "%s%-20s is supported%s",
                  b_line,
                  functions[func_pos].name,
                  e_line
                ) ;
       else
          printf( "%s%-20s is not supported%s",
                  b_line,
                  functions[func_pos].name,
                  e_line
                ) ;
       if ( side ) {
          *b_line = '\0' ;
          *e_line = '\0' ;
          side = 0 ;
       }
       else {
          strcpy( b_line, "   " ) ;
          strcpy( e_line, "\n" ) ;
          side = 1 ;
       }
       func_pos++ ;
    }

/*<-- */

    /*  Disconnect and free up CLI resources.  */

    /*  Commit the changes.  */
    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

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

}                                  /* end main */