/******************************************************************************
**
** Source File Name = blobfile.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: This sample program demonstrates the use of BLOB values.
**          The program creates a CURSOR, and fetches the contents of
**          the "emp_photo" table (the SAMPLE database must be
**          installed with the "db2sampl" executable), and then
**          display the image using the platform display program.
**
** An external function "check_error" is contained in the file "util.c"
** which must be compiled along with this file.
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - Precompile with the SQL precompiler (PREP in DB2)
**       - Bind to a database (BIND in DB2)
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows)
**         or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sql.h>
#include "utilemb.h"


EXEC SQL INCLUDE SQLCA;


#if (defined(DB2OS2) || defined(DB2NT))
#define photo_file "PHOTO.BMP"
#define photo_format "bitmap"
#else
/* UNIX */
#define photo_file "photo.xwd"
#define photo_format "xwd"
#endif

#if defined(DB2OS2)
   /* On OS/2 display using the iconedit program */
   #define view_cmd "iconedit "
#elif defined(DB2NT)
   /* On Windows NT display using the pbrush program */
   #define view_cmd "pbrush "
#else /* UNIX */
   /* For AIX display the image using the xwud program */
   #define view_cmd "xwud -in "
#endif

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

   EXEC SQL BEGIN DECLARE SECTION; /* :rk.1:erk. */
      SQL TYPE IS BLOB_FILE photo;
      short lobind;
      char userid[9];
      char passwd[19];
      char format[10];
      char file_name[10];
   EXEC SQL END DECLARE SECTION;


   printf( "Sample C program: BLOBFILE\n\n" );
   printf( "Note: Some UNIX systems may not support this program's graphical format.\n" );
   printf( "If the photo does not appear on your screen, or the program hangs,\n" );
   printf( "comment out the command: 'system(\"xwud -in photo.xwd\");', convert photo.xwd\n" );
   printf( "which should be in your working directory to your system's graphical format,\n" );
   printf( "and use your system's graphical tool or browser to view the file.\n\n" );

   if (argc == 1) {
      EXEC SQL CONNECT TO sample;
	  EMB_SQL_CHECK("CONNECT TO SAMPLE");
   }
   else if (argc == 3) { 
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd;
      EMB_SQL_CHECK("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: blobfile [userid passwd]\n\n");
      return 1;
   } /* endif */

   strcpy(format, photo_format);
   strcpy(file_name, photo_file);

   strcpy (photo.name, file_name);  /* :rk.2:erk. */
   photo.name_length = strlen(file_name);
   photo.file_options = SQL_FILE_OVERWRITE;

   EXEC SQL SELECT picture INTO :photo :lobind FROM emp_photo  /* :rk.3:erk. */
      WHERE photo_format=:format AND empno='000130';
   EMB_SQL_CHECK("SELECT INTO");

   if (lobind < 0) {
      printf ("NULL LOB indicated \n");
   } else {
      printf ("Photo for EMPNO 000130 is in file : " photo_file "\n");
   } /* endif */

   /* Set file options to SQL_FILE_READ before reading the PHOTO.BMP file
      and updating the emp_photo table */
   photo.file_options = SQL_FILE_READ;
   EXEC SQL INSERT INTO emp_photo(EMPNO, PHOTO_FORMAT, PICTURE)
                           VALUES('000131', :format, :photo);
   EMB_SQL_CHECK("INSERT INTO");

   /* Delete the previously updated record from the emp_photo table */
   EXEC SQL DELETE FROM emp_photo WHERE empno = '000131';
   EMB_SQL_CHECK("DELETE FROM");

   system(view_cmd photo_file);  

   EXEC SQL CONNECT RESET;
   EMB_SQL_CHECK("CONNECT RESET");
   return 0;
}
/* end of program : blobfile.sqc */