/******************************************************************************
**
** Source File Name = lobval.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 LOB values.
**          The program creates a CURSOR, and fetches the contents of
**          the "emp_resume" table (the SAMPLE database must be
**          installed with the "db2sampl" executable), and then
**          outputs the first 15 lines of the resume.
**
** An external function "check_error" is conatined 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 "utilemb.h"


EXEC SQL INCLUDE SQLCA;


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

   EXEC SQL BEGIN DECLARE SECTION; /* :rk.1:erk. */
      char number[7];
      SQL TYPE IS CLOB(5K) resume;
      short lobind;
      char userid[9];
      char passwd[19];
   EXEC SQL END DECLARE SECTION;


   printf( "Sample C program: LOBVAL\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: lobval [userid passwd]\n\n");
      return 1;
   } /* endif */
   
   EXEC SQL DECLARE c1 CURSOR FOR
            SELECT empno, resume FROM emp_resume WHERE resume_format='ascii';

   EXEC SQL OPEN c1;
   EMB_SQL_CHECK("OPEN CURSOR");

   do {
      char        *c;
      sqluint32    idx, lines;
      EXEC SQL FETCH c1 INTO :number, :resume :lobind;  /* :rk.2:erk. */
      if (SQLCODE != 0) break;
      if (lobind < 0) {
         printf ("NULL LOB indicated\n");
      } else {
	  
         /* PRINT OUT THE CONTENTS OF THE LOB */
         printf ("Printing the first 15 lines for the resume of empno %s\n",
            number);
         for (idx=0, lines=0, c=resume.data; /* :rk.3:erk. */
            idx < resume.length, lines < 16;
            idx++, c++) {
#ifdef DB2MAC     
			/* If data is returned from DB2/6000, the newline character is 0x0A */
			/* Need to convert before printing it out */
			
            if (*c == 0x0A) {
			   lines++;
			   *c = 0x0D;
			}
            printf ("%c", *c);
#else
            printf ("%c", *c);
            if (*c == '\n') lines++;
#endif
         } /* endfor */
         printf ("Resume Length (number of characters):%d\n", resume.length); /* :rk.4:erk. */
         printf ("\n\n\n");
      } /* endif */
   } while ( 1 );

   EXEC SQL CLOSE c1;
   EMB_SQL_CHECK("CLOSE CURSOR");

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