/******************************************************************************
**
** Source File Name = sampudf.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 program is an example of how UDF and UDTs are implemented in
**       order to modify table entries.
**       This program needs the embedded SQL calls to connect to
**       an existing database, create a new table to store the rows
**       identified as having a UDT of type 'dollar', and insert data
**       into this table, after the rows are modified through a 
**       sourced UDF.
**
**    STRUCTURES USED :
**       sqlca
**
**    APIs USED :
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  fgets, strncpy
**
**       DBMS LIBRARY :
**         sqlenv.h
**
**    Other Function(s) used:
**       check_error :   Checks for SQLCODE error, and prints out any 
**       [in UTIL.C]     related information available.
**
**    EXTERNAL DEPENDANCIES :
**       - Ensure existence of database (for example 'SAMPLE') 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 <sqlenv.h>
#include "utilemb.h"


EXEC SQL INCLUDE SQLCA;


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

   EXEC SQL BEGIN DECLARE SECTION;
      char st[255];
      char userid[9];
      char passwd[19];
   EXEC SQL END DECLARE SECTION;


   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: sampudf [userid passwd]\n\n");
      return 1;
   } /* endif */
     
   EXEC SQL CREATE DISTINCT TYPE dollar AS DECIMAL (9,2) WITH COMPARISONS;
   EMB_SQL_CHECK("CREATE DISTINCT TYPE dollar");

   EXEC SQL CREATE DISTINCT TYPE factor AS DECIMAL (9,2) WITH COMPARISONS;
   EMB_SQL_CHECK("CREATE DISTINCT TYPE factor");

   EXEC SQL CREATE FUNCTION samp_add (dollar, dollar)
      RETURNS dollar SOURCE "+" (DECIMAL, DECIMAL);
   EMB_SQL_CHECK("CREATE FUNCTION samp_add");

   EXEC SQL CREATE FUNCTION samp_mul (dollar, factor) SPECIFIC mult1
      RETURNS dollar SOURCE "*" (DECIMAL, DECIMAL);
   EMB_SQL_CHECK("CREATE FUNCTION samp_mul - mult1");

   EXEC SQL CREATE FUNCTION samp_mul (factor, factor) SPECIFIC mult2
      RETURNS factor SOURCE "*" (DECIMAL, DECIMAL);
   EMB_SQL_CHECK("CREATE FUNCTION samp_mul - mult1");

   EXEC SQL CREATE TABLE sampudf (name VARCHAR(15), salary dollar,
      newsal dollar);
   EMB_SQL_CHECK("CREATE TABLE");

   strcpy (st, "INSERT INTO sampudf SELECT name, DOLLAR(salary), ");
   strcat (st, "SAMP_MUL(DOLLAR(salary), FACTOR(1.2)) FROM staff");
   EXEC SQL PREPARE s1 FROM :st;
   EMB_SQL_CHECK("PREPARE");

   EXEC SQL EXECUTE s1;
   EMB_SQL_CHECK("EXECUTE");

   EXEC SQL COMMIT;
   EMB_SQL_CHECK("COMMIT");

   /* drop objects created by the program */
   EXEC SQL DROP FUNCTION samp_add;
   EMB_SQL_CHECK("DROP FUNCTION");
   EXEC SQL DROP SPECIFIC FUNCTION mult1;
   EMB_SQL_CHECK("DROP FUNCTION");
   EXEC SQL DROP SPECIFIC FUNCTION mult2;
   EMB_SQL_CHECK("DROP FUNCTION");
   EXEC SQL DROP TABLE sampudf;
   EMB_SQL_CHECK("DROP TABLE");
   EXEC SQL DROP DISTINCT TYPE dollar;
   EMB_SQL_CHECK("DROP TYPE");
   EXEC SQL DROP DISTINCT TYPE factor;
   EMB_SQL_CHECK("DROP TYPE");

   printf("Program sampudf successful.\n");
   return 0;

ext:
   EXEC SQL ROLLBACK;
   EMB_SQL_CHECK("ROLLBACK");
   return 1;
}
/* end of program : sampudf.sqc */