/*******************************************************************************
**                                                                        
** Source File Name = dbusemx.sqc                                      
**                                                                        
** 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 use a database in conjunction with embedded SQL.
**                                                                        
** 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 <sql.h>
#include <sqlenv.h>
#include "utilcli.h"          /* Header file for CLI sample code */


int CLIMixEmbedded( SQLHANDLE) ;


EXEC SQL INCLUDE SQLCA ;


/*******************************************************************
** main
*******************************************************************/
int main( int argc, char * argv[] )
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 
    SQLHANDLE   henv;  /* environment handle */
    SQLHANDLE   hdbc;  /* connection 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\nDATABASES: HOW TO USE A DATABASE IN CONJUNCTION WITH\n");
    printf("           EMBEDDED SQL STATEMENTS\n");

    /* initialize the CLI application */
    rc = CLIAppInit( dbAlias, user, pswd, &henv, &hdbc, 
                     (SQLPOINTER)SQL_AUTOCOMMIT_OFF);
    if ( rc != 0 ) return( rc ) ;
    
    /* mix CLI calls with embedded SQL */
    rc = CLIMixEmbedded( hdbc) ;

    /* terminate the CLI application */
    rc = CLIAppTerm( &henv, &hdbc, dbAlias);
    return( rc ) ;
}                                  /* end main */
    
/******************************************************************************
**    CLIMixEmbedded - how to mix CLI calls with embedded SQL
******************************************************************************/
int CLIMixEmbedded( SQLHANDLE hdbc)
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 

    printf("\nUSE THE EMBEDDED SQL STATEMENT\n");
    printf("    CREATE TABLE table1 (col1 INTEGER)\n");
    printf("TO SHOW HOW TO MIX CLI WITH EMBEDDED SQL:\n");   

    /* set AUTOCOMMIT OFF */
    sqlrc = SQLSetConnectAttr( hdbc,
                               SQL_ATTR_AUTOCOMMIT,
                               (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_NTS) ;
    DBC_HANDLE_CHECK( hdbc, sqlrc);
    printf("\n    Transactions enabled\n");        

    /* execute the embedded SQL statement */
    printf("    Execute the embedded SQL statement:\n");    
    printf("        CREATE TABLE table1 (col1 INTEGER)\n");    

    EXEC SQL CREATE TABLE table1 ( col1 INTEGER) ; 
    if ( SQLCODE != 0)
    {   printf( "\nError while executing the embedded SQL statement.\n");    
    }    
   
    printf("    Rolling back the transaction...\n"); 
    sqlrc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK );     
    DBC_HANDLE_CHECK( hdbc, sqlrc);     
    printf("    Transaction rolled back.\n");
    
    return(rc);    
}