/******************************************************************************
**
** Source File Name = sampudf.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 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 "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)\
goto ext;
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;
#ifdef DB268K
/* Before making any API calls for 68K environment,
need to initial the Library Manager */
InitLibraryManager(0,kCurrentZone,kNormalMemory);
atexit(CleanupLibraryManager);
#endif
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: sampudf [userid passwd]\n\n");
return 1;
} /* endif */
EXEC SQL CREATE DISTINCT TYPE dollar AS DECIMAL (9,2) WITH COMPARISONS;
CHECKERR ("CREATE DISTINCT TYPE dollar");
EXEC SQL CREATE DISTINCT TYPE factor AS DECIMAL (9,2) WITH COMPARISONS;
CHECKERR ("CREATE DISTINCT TYPE factor");
EXEC SQL CREATE FUNCTION samp_add (dollar, dollar)
RETURNS dollar SOURCE "+" (DECIMAL, DECIMAL);
CHECKERR ("CREATE FUNCTION samp_add");
EXEC SQL CREATE FUNCTION samp_mul (dollar, factor) SPECIFIC mult1
RETURNS dollar SOURCE "*" (DECIMAL, DECIMAL);
CHECKERR ("CREATE FUNCTION samp_mul - mult1");
EXEC SQL CREATE FUNCTION samp_mul (factor, factor) SPECIFIC mult2
RETURNS factor SOURCE "*" (DECIMAL, DECIMAL);
CHECKERR ("CREATE FUNCTION samp_mul - mult1");
EXEC SQL CREATE TABLE sampudf (name VARCHAR(15), salary dollar,
newsal dollar);
CHECKERR ("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;
CHECKERR ("PREPARE");
EXEC SQL EXECUTE s1;
CHECKERR ("EXECUTE");
EXEC SQL COMMIT;
CHECKERR("COMMIT");
/* drop objects created by the program */
EXEC SQL DROP FUNCTION samp_add;
CHECKERR ("DROP FUNCTION");
EXEC SQL DROP SPECIFIC FUNCTION mult1;
CHECKERR ("DROP FUNCTION");
EXEC SQL DROP SPECIFIC FUNCTION mult2;
CHECKERR ("DROP FUNCTION");
EXEC SQL DROP TABLE sampudf;
CHECKERR ("DROP TABLE");
EXEC SQL DROP DISTINCT TYPE dollar;
CHECKERR ("DROP TYPE");
EXEC SQL DROP DISTINCT TYPE factor;
CHECKERR ("DROP TYPE");
printf("Program sampudf successful.\n");
return 0;
ext:
EXEC SQL ROLLBACK;
check_error ("ROLLBACK", &sqlca);
return 1;
}
/* end of program : sampudf.sqc */