/*******************************************************************************
**                                                                        
** Source File Name = dbnative.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 :                                                           
**    Translates a statement that contains an ODBC escape clause
**    to a data source specific format.
**                                                                        
** 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 */


/*******************************************************************
** main
*******************************************************************/
int main( int argc, char * argv[] )
{   SQLRETURN   sqlrc = SQL_SUCCESS;
    int         rc = 0; 
    SQLHANDLE   henv;  /* environment handle */
    SQLHANDLE   hdbc;  /* connection handle */

    SQLCHAR    inODBCStmt[] = 
                 "SELECT * FROM employee WHERE hiredate={d '1994-03-29'}" ;
    SQLCHAR    odbcEscapeClause[] = "{d '1994-03-29'}" ;
    SQLCHAR    outDbStmt[1024]  ; 
    SQLINTEGER dbStmtLen ;    

    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: TRANSLATING A STATEMENT THAT\n");
    printf("           CONTAINS AN ODBC ESCAPE CLAUSE\n");  
    printf("           TO A DATA SOURCE SPECIFIC FORMAT.\n");

    /* initialize the CLI application */
    rc = CLIAppInit( dbAlias, user, pswd, &henv, &hdbc, 
                     (SQLPOINTER)SQL_AUTOCOMMIT_ON);
    if ( rc != 0 ) return( rc ) ;


    printf("\nUSE THE CLI FUNCTION\n");
    printf("-SQLNativeSql\n");
    printf("TO :\n");    

    printf("\n   translate the statement\n");       
    printf("   %s\n", inODBCStmt);
    printf("   that contains the ODBC escape clause %s\n", odbcEscapeClause);
    printf("   to a data source specific format.\n"); 

    /*--> 00000630.snippet */
    SQLNativeSql(hdbc, inODBCStmt, SQL_NTS, outDbStmt, 1024, &dbStmtLen);
    HANDLE_CHECK( SQL_HANDLE_DBC, hdbc, sqlrc, &henv, &hdbc ) ;     

    if ( dbStmtLen == SQL_NULL_DATA ) printf( "Invalid ODBC statement\n" ) ;
    else {
       printf( "\n   the data source specific format is:\n   %s\n", outDbStmt);
    }    
    /* 00000630.snippet <--*/
    
    /* terminate the CLI application */
    rc = CLIAppTerm( &henv, &hdbc, dbAlias);
    return( rc ) ;
}                                  /* end main */