/*******************************************************************************
**
** Source File Name = picin2.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 :
**      Duplicate of picin.c, but reads the file itself, and inserts
**      the blob in pieces.
**
** 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 ;

/*--> SQLL1X31.SCRIPT */
    SQLCHAR * stmt = ( SQLCHAR * )
    "INSERT INTO emp_photo (empno, photo_format, picture) VALUES (?, ?, ?)" ;
/*<-- */

    SQLCHAR         Photo_Format[11] = {""};
    SQLCHAR         Empno[7];
    SQLCHAR         FName[256] = "";
    SQLCHAR         fbuffer[BUFSIZ];  /* Use BUFSIZ from stdio.h */
    SQLCHAR         input_param[] = "Photo Data";

    SQLINTEGER      PicLength=0;
    SQLINTEGER      FInd = 0;
    SQLSMALLINT     FLength = 13;
    SQLINTEGER      prgbValue=0;
    SQLINTEGER      i = 0;
    FILE           *PicFile;
    size_t          n = 0;
    size_t          FileSize = 0;

/* 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 statement */
    rc = SQLPrepare(hstmt, stmt, SQL_NTS);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
                          6, 0, Empno, 7, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
                          10, 0,  Photo_Format, 11, NULL);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

    /*
     * This paramter will use SQLPutData, rgbValue is set to Param Number,
     * pcbValue is set to SQL_DATA_AT_EXEC
     */
    PicLength = SQL_DATA_AT_EXEC;
    rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_BLOB,
                          BUFSIZ, 0, (SQLPOINTER)input_param, BUFSIZ, &PicLength);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
/*<-- */

    while (1) {
        printf("Which Picture Format [xwd (XWindows), bitmap]?\n");
        gets((char *)Photo_Format);
        if (strcmp((char *)Photo_Format, "xwd") == 0 ||
            strcmp((char *)Photo_Format, "bitmap") == 0) break;
        printf("Invalid Format!\n");
    }


    printf("Enter the Employee Number:\n");
    gets((char *)Empno);
    printf("Enter the filename of the Photo of type %s\n", Photo_Format);
    gets((char *)FName);

/*--> */
    if ( ( rc = SQLExecute( hstmt ) ) == SQL_NEED_DATA ) {
       PicFile = fopen((char *)FName, "rb");
       if (PicFile == NULL) {
          printf(">---- ERROR Opening File -------");
          /* Cancel the DATA AT EXEC state for hstmt */
          rc = SQLCancel(hstmt);
          CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
          /* Commit, free resources, disconnect and exit */
       }
       else {
          while ( ( rc = SQLParamData( hstmt, ( SQLPOINTER ) &prgbValue )
                  ) == SQL_NEED_DATA
                ) {
             printf("Getting data for %s\n", prgbValue);
             /*
              if more than 1 parms used DATA_AT_EXEC then prgbValue would
              have to be checked to determine which param needed data
             */
             while ( feof( PicFile ) == 0 ) {
                n = fread(fbuffer, sizeof(char), BUFSIZ, PicFile);
                rc = SQLPutData(hstmt, fbuffer, n);
                CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
                FileSize = FileSize + n;
                if (FileSize > 102400u) {
                   /* BLOB column defined as 100K MAX */
                   printf(">---- ERROR: File > 100K  -------");
                   exit(terminate(hdbc, SQL_ERROR));
                }
             }
             printf("Read a total of %u bytes from %s\n", FileSize, FName);
          }
          CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
       }
    }
    else CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;

/*<-- */

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