/*******************************************************
**
** Licensed Materials - Property of IBM  1.5
**
** (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.
**
**
** file = mixed.sqc
**    - demonstrates mixing embedded SQL and DB2 CLI
**    - error handling ignored for simplicity
**    - this sample can only be built using the makefile;
**      there is currently no build script for this sample
**    - at run time the sample will ask for a database
**      to connect to; this must be the database indicated
**      in the precompile step (cliprep as run by the makefile)
**      otherwise runtime errors will occur
**
** 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
**     - CLI Guide and Reference.
**
** For more information on the SQL language see the SQL Reference.
**
*********************************************************/

#include <stdio.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] ;

#define MAX_CONNECTIONS 2

int Create_Tab(void);
int Drop_Tab(void);

/* main */
int main( int argc, char * argv[] ) {

    SQLHANDLE henv, hdbc[MAX_CONNECTIONS] ;
    SQLRETURN rc ;

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

/*--> SQLL1X35.SCRIPT */

    /* allocate an environment handle */
    SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;

    /* Connect to first data source */
    prompted_connect( henv, &hdbc[0] ) ;

    /* Connect to second data source */
    DBconnect( henv, &hdbc[1] ) ;

    /*********   Start Processing Step  *********************/
    /* NOTE: at this point there are two active connections */
     
    /* set current connection to the first database */
    if ( SQLSetConnection( hdbc[0] ) != SQL_SUCCESS )
       printf( "Error setting connection 1\n" ) ;

    /* call function that contains embedded SQL */
    if ( ( rc = Create_Tab() ) != 0 )
       printf( "Error Creating Table on 1st connection, RC=%d\n", rc ) ;

    /*  Commit transation on connection 1 */
    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc[0], SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;

    /* set current connection to the second database */
    if ( SQLSetConnection( hdbc[1] ) != SQL_SUCCESS )
       printf( "Error setting connection 2\n" ) ;

    /* call function that contains embedded SQL */
    if ( ( rc = Create_Tab() ) != 0 )
       printf( "Error Creating Table on 2nd connection, RC=%d\n", rc ) ;

    /*  Commit transation on connection 2 */
    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc[1], SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;

    /* Pause to allow the existance of the tables to be verified. */
    printf( "Tables created, hit Return to continue\n" ) ;
    getchar() ;

    /* set current connection to the first database */
    if ( SQLSetConnection( hdbc[0] ) != SQL_SUCCESS )
       printf( "Error setting connection 1\n" ) ;

    /* call function that contains embedded SQL */
    if ( ( rc = Drop_Tab() ) != 0 )
       printf( "Error dropping Table on 1st connection, RC=%d\n", rc ) ;

    /*  Commit transation on connection 1 */
    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc[0], SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;

    /* set current connection to the second database */
    if ( SQLSetConnection( hdbc[1] ) != SQL_SUCCESS )
       printf( "Error setting connection 2\n" ) ;

    /* call function that contains embedded SQL */
    if ( ( rc = Drop_Tab() ) != 0 )
       printf( "Error dropping Table on 2nd connection, RC=%d\n", rc ) ;

    /*  Commit transation on connection 2 */
    rc = SQLEndTran( SQL_HANDLE_DBC, hdbc[1], SQL_COMMIT ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;

    printf( "Tables dropped\n" ) ;

    /*********   End Processing Step  ***************************/
/*<-- */

    printf( "\n>Disconnecting .....\n" ) ;

    /* disconnect first connection */
    rc = SQLDisconnect( hdbc[0] ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;

    /* disconnect second connection */
    rc = SQLDisconnect( hdbc[1] ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;

    /* free first connection handle */
    rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc[0] ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[0], rc ) ;

    /* free second connection handle */
    rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc[1] ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc[1], rc ) ;

    /* free environment handle */
    rc = SQLFreeHandle( SQL_HANDLE_ENV,  henv ) ;
    if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;

    return( SQL_SUCCESS ) ;

}

/*--> */
/*************   Embedded SQL Functions  *******************************
** This would normally be a seperate file to avoid having to
** keep precompiling the embedded file in order to compile the DB2 CLI
** section
************************************************************************/

#include <sql.h>
#include <sqlenv.h>

EXEC SQL INCLUDE SQLCA ;

int Create_Tab() {

   EXEC SQL CREATE TABLE mixedup (ID INTEGER, NAME CHAR(10)) ;
   return( SQLCODE ) ;

}

int Drop_Tab() {

   EXEC SQL DROP TABLE mixedup ;
   return( SQLCODE ) ;
}
/*<-- */