/*******************************************************************************
**                                                                        
** Source File Name = fetch.c  1.5                                          
**                                                                        
** 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 :
**
** A DB2 Version 2 sample that has been unchanged. See the CLI sample 
** fetch.c for the same program updated to use the latest set of functions.
**
** Example of executing an SQL statement.
** SQLBindCol & SQLFetch is used to retrive data from the result set
** directly into application storage.
**                                                                        
** 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 "v2sutil.h"

/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
   /* Need to include ASLM for 68K applications */
   #include <LibraryManager.h>
#endif

#define MAX_STMT_LEN 255
/* Global Variables for user id and password.
   To keep samples simple, not a recommended practice.
*/
    SQLCHAR   server[SQL_MAX_DSN_LENGTH + 1];
    SQLCHAR   uid[MAX_UID_LENGTH + 1];
    SQLCHAR   pwd[MAX_PWD_LENGTH + 1];

/*******************************************************************
** main
*******************************************************************/
int
main( int argc, char * argv[] )
{
    SQLHENV         henv;
    SQLHDBC         hdbc;
    SQLHSTMT        hstmt;
    SQLRETURN       rc;
    SQLCHAR         sqlstmt[] = "SELECT deptname, location from org where division = 'Eastern'";
    struct { SQLINTEGER ind;
             SQLCHAR  s[15];
           } deptname, location;

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

    rc = SQLAllocEnv(&henv);    /* allocate an environment handle    */
    if (rc != SQL_SUCCESS)
        return (terminate(henv, rc));

    rc = DBconnect(henv, &hdbc);/* allocate a connect handle, and connect */
    if (rc != SQL_SUCCESS)
        return (terminate(henv, rc));

    rc = SQLAllocStmt(hdbc, &hstmt);
    CHECK_DBC(hdbc, rc);

    rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS);
    CHECK_STMT(hstmt, rc);

    rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) deptname.s, 15,
                    &deptname.ind);
    CHECK_STMT(hstmt, rc);

    rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, (SQLPOINTER) location.s, 15,
                    &location.ind);
    CHECK_STMT(hstmt, rc);

    printf("Departments in Eastern division:\n");
    printf("DEPTNAME       Location\n");
    printf("-------------- -------------\n");

    while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) {
        printf("%-14.14s %-14.14s \n", deptname.s, location.s);
    }
    if (rc != SQL_NO_DATA_FOUND)
        check_error(henv, hdbc, hstmt, rc, __LINE__, __FILE__);

    rc = SQLFreeStmt(hstmt, SQL_DROP);
    CHECK_STMT(hstmt, rc);

    rc = SQLTransact(henv, hdbc, SQL_COMMIT);
    CHECK_DBC(hdbc, rc);

    printf("Disconnecting .....\n");
    rc = SQLDisconnect(hdbc);
    CHECK_DBC(hdbc, rc);

    rc = SQLFreeConnect(hdbc);
    CHECK_DBC(hdbc, rc);

    rc = SQLFreeEnv(henv);
    if (rc != SQL_SUCCESS)
        return (terminate(henv, rc));

    return (0);
}                               /* end main */