/******************************************************************************
**
** Source File Name = updat.sqc  1.4
**
** 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 static SQL.
**          It updates all managers in the STAFF table of the
**          SAMPLE database and changes their job from 'Mgr' to
**          'Clerk', deletes all who are 'Sales', and inserts a new
**          row.  In all three SQL statements (UPDATE, DELETE, INSERT)
**          a host variable is implemented.
**              Finally, a ROLLBACK is done so that the SAMPLE
**          database remains unchanged.
**
**    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 <string.h>
#include <stdlib.h>
#include <sqlenv.h>
#include "util.h"

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

EXEC SQL INCLUDE SQLCA;  /* :rk.1:erk. */

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

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

   EXEC SQL BEGIN DECLARE SECTION;  /* :rk.2:erk. */
      char statement[256];
      char userid[9];
      char passwd[19];
      char jobUpdate[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( "\nSample C program:  UPDAT \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; /* :rk.3:erk. */
	  CHECKERR ("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: updat [userid passwd]\n\n");
      return 1;
   } /* endif */

   strcpy (jobUpdate, "Clerk");
   EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = 'Mgr'; /* :rk.4:erk. */
   CHECKERR ("UPDATE STAFF");
   printf ("All 'Mgr' have been demoted to 'Clerk'!\n" );

   strcpy (jobUpdate, "Sales");
   EXEC SQL DELETE FROM staff WHERE job = :jobUpdate; /* :rk.5:erk. */
   CHECKERR ("DELETE FROM STAFF");
   printf ("All 'Sales' people have been deleted!\n");

   EXEC SQL INSERT INTO staff 
      VALUES (999, 'Testing', 99, :jobUpdate, 0, 0, 0);  /* :rk.6:erk. */
   CHECKERR ("INSERT INTO STAFF");
   printf ("New data has been inserted\n");

   EXEC SQL ROLLBACK; /* :rk.7:erk. */
   CHECKERR ("ROLLBACK");
   printf( "On second thought -- changes rolled back.\n" );

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