/******************************************************************************
**
** Source File Name = varinp.sqc  1.5
**
** 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: This sample program demonstrates the use of a CURSOR.
**          The CURSOR is processed using dynamic SQL.  This program
**          obtains all managers in the STAFF table of the
**          SAMPLE database and change their job from "Mgr" to
**          "Clerk".  A ROLLBACK is done so that the SAMPLE
**          database remains unchanged.
**
** 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 "util.h"

#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif

EXEC SQL INCLUDE SQLCA;

#define  CHECKERR(CE_STR)   if (check_error (CE_STR, &sqlca) != 0) return 1;

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

   EXEC SQL BEGIN DECLARE SECTION;
      char   pname[10];
      short  dept;
      char userid[9];
      char passwd[19];
      char st[255];
      char parm_var[6];
   EXEC SQL END DECLARE SECTION;

#ifdef DB268K
   /* Before making any API calls for 68K environment,
      need to initial the Library Manager */
	InitLibraryManager(0,kCurrentZone,kNormalMemory);
	atexit(CleanupLibraryManager);
#endif

   printf( "Sample C program: VARINP \n" );

   if (argc == 1) {
      EXEC SQL CONNECT TO sample;
	  CHECKERR ("CONNECT TO SAMPLE");
   }
   else if (argc == 3) { 
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd;
      CHECKERR ("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: varinp [userid passwd]\n\n");
      return 1;
   } /* endif */
   
   strcpy (st, "SELECT name, dept FROM staff WHERE job = ? FOR UPDATE OF job");
   EXEC SQL PREPARE s1 FROM :st; /* :rk.1:erk. */
   CHECKERR ("PREPARE");

   EXEC SQL DECLARE c1 CURSOR FOR s1; /* :rk.2:erk. */

   strcpy (parm_var, "Mgr");
   EXEC SQL OPEN c1 USING :parm_var; /* :rk.3:erk. */
   CHECKERR ("OPEN");

   strcpy (parm_var, "Clerk");
   strcpy (st, "UPDATE staff SET job = ? WHERE CURRENT OF c1");
   EXEC SQL PREPARE s2 from :st; /* :rk.4:erk. */

   do {
      EXEC SQL FETCH c1 INTO :pname, :dept; /* :rk.5:erk. */
      if (SQLCODE != 0) break;

      printf( "%-10.10s in dept. %2d will be demoted to Clerk\n",
              pname, dept );
      EXEC SQL EXECUTE s2 USING :parm_var; /* :rk.6:erk. */
      CHECKERR ("EXECUTE");
   } while ( 1 );

   EXEC SQL CLOSE c1;  /* :rk.7:erk. */
   CHECKERR ("CLOSE CURSOR");

   EXEC SQL ROLLBACK;
   CHECKERR ("ROLLBACK");
   printf( "\nOn second thought -- changes rolled back.\n" );

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