/*******************************************************************************
**                                                                        
** Source File Name = ordin.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 :                                                           
** Use array input, adds order to the ord_line & ord_cust tables.
** This example uses the tables created by create.c, see the README file
** for more information.
**                                                                        
**    FUNCTIONS USED :                                                    
**        SQLAllocEnv
**        SQLAllocStmt
**        SQLBindParameter
**        SQLDisconnect
**        SQLExecute
**        SQLFreeConnect
**        SQLFreeEnv
**        SQLFreeStmt
**        SQLParamOptions
**        SQLPrepare
**                                                                        
** 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

/*
 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, hstmt ;
    SQLRETURN rc ;
    SQLINTEGER ords_array_size = 16 ;
    SQLINTEGER ord_lines__array_size = 25 ;

    SQLCHAR * stmt1 = ( SQLCHAR * )
       "INSERT INTO ORD_CUST VALUES (?, ?, CURRENT DATE)" ;

    SQLCHAR * stmt2 = ( SQLCHAR * )
       "INSERT INTO ORD_LINE VALUES (?, ?, ?)" ;

    SQLINTEGER Ord_Num[] = {
        100101, 100102, 100103, 100104, 100105, 100106,
        100107, 100108, 100109, 100110, 100111, 100112,
        100113, 100114, 100115, 100116
    } ;
    SQLINTEGER Cust_Num[] = {
         10,  10,  30,  40,  50,  60,  60,  60,  90,
        100, 110, 110, 130, 160, 160, 160
    } ;
    SQLINTEGER Prod_Num_Lines[] = {
        100110, 100510, 200510, 200610, 100220, 200120,
        200510, 500210, 200510, 990110, 990110, 500210,
        200220, 200610, 500210, 990120, 990110, 200510,
        200610, 100110, 100210, 990120, 500110, 500210,
        200510
    } ;
    SQLINTEGER Ord_Num_Lines[] = {
        100101, 100101, 100102, 100103, 100104, 100104,
        100104, 100105, 100105, 100106, 100107, 100107,
        100108, 100108, 100109, 100110, 100111, 100112,
        100113, 100114, 100115, 100116, 100116, 100116,
        100116
    } ;
    SQLDOUBLE Quant[] = {
        1, 1, 1, 1, 1, 1,
        1, 0.250, 1, 1, 1, 0.5,
        1, 1, 1.5, 1, 1, 1,
        1, 1, 1, 1, 0.250, 1.0,
        1
    } ;

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

    rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;

    /* Prepare the first statement */
    rc = SQLPrepare( hstmt, stmt1, SQL_NTS ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLSetStmtAttr( hstmt,
                         SQL_ATTR_PARAMSET_SIZE,
                         ( SQLPOINTER ) ords_array_size,
                         0
                       ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                          0, 0, Ord_Num, 0, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                          0, 0, Cust_Num, 0, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLExecute( hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
    printf( "Inserted %ld Rows\n", ords_array_size ) ;

    /* Reset the statement handle for use with second statement */
    rc = SQLFreeStmt( hstmt, SQL_UNBIND ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLFreeStmt( hstmt, SQL_RESET_PARAMS ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLFreeStmt( hstmt, SQL_CLOSE ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /* Prepare the second statement */
    rc = SQLPrepare( hstmt, stmt2, SQL_NTS ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLSetStmtAttr( hstmt,
                         SQL_ATTR_PARAMSET_SIZE,
                         ( SQLPOINTER ) ord_lines__array_size,
                         0
                       ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                          0, 0, Ord_Num_Lines, 0, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
                          0, 0, Prod_Num_Lines, 0, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL,
                          14, 7, Quant, 0, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLExecute( hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
    printf( "Inserted %ld Rows\n", ord_lines__array_size ) ;

    rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /* COMMIT, free resources and exit */

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