home *** CD-ROM | disk | FTP | other *** search
- #ifdef RCSID
- static char *RCSid =
- "$Header: samplec.pc,v 6.0 88/06/16 19:25:58 rchoplin Exp $ samplec.pc Copyr (c) 1986 Oracle";
- #endif /* RCSID */
-
- /*
- * $Date: 88/06/16 19:25:58 $ $Revision: 6.0 $ samplec.pc Copyr (c) 1986 Oracle
- */
-
- /* VOID sample
-
- sample is a simple example program which adds new employee
- records to the personnel data base. Checking
- is done to insure the integrity of the data base.
- The employee numbers are automatically selected using
- the current maximum employee number + 10.
-
- The program queries the user for data as follows:
-
- Enter employee name:
- Enter employee job:
- Enter employee salary:
- Enter employee dept:
-
- The program terminates if EOF (end of file) or a null
- string (<return> key) is entered when the employee name
- is requested.
-
- If the record is successfully inserted, the following
- is printed:
-
- ename added to department dname as employee # nnnnnn
-
- To build and run SAMPLE (assumes that SQLCA file is in "SQLCA.H"):
-
- $ pcc iname=samplec.pc prog=xxx$sample userid=scott/tiger
- $ cc -O -c samplec.c
- $ cc -o samplec samplec.o -lsql -lhlic -lhli
- $ samplec
-
- where "xxx" is your logon id. E.g.:
-
- $ pcc iname=samplec prog=oracle$sample userid=scott/tiger
-
- The idea here is to pick a unique name for the program, so that
- anyone precompiling pgms into "scott/tiger" will have unique names
- for their pgms (e.g., if I have a prog=sample userid=scott/tiger;
- and you have a prog=sample userid=scott/tiger, then we have a
- conflict...).
- */
- #include <stdio.h>
- #include <ctype.h>
-
- /* SQL stmt #1
- EXEC SQL BEGIN DECLARE SECTION;
- VARCHAR uid[20];
- */
- struct {
- unsigned short len;
- unsigned char arr[20];
- } uid;
- /*
- /o username o/
- VARCHAR pwd[20];
- */
- struct {
- unsigned short len;
- unsigned char arr[20];
- } pwd;
- /*
- /o password o/
-
- */
- int empno; /* employee number */
- /*
- VARCHAR ename[15];
- */
- struct {
- unsigned short len;
- unsigned char arr[15];
- } ename;
- /*
- /o employee name o/
- */
- int deptno; /* department number */
- /*
- VARCHAR dname[15];
- */
- struct {
- unsigned short len;
- unsigned char arr[15];
- } dname;
- /*
- /o department name o/
-
- VARCHAR job[15];
- */
- struct {
- unsigned short len;
- unsigned char arr[15];
- } job;
- /*
- /o employee job o/
- */
- int sal; /* employee salary */
- /* SQL stmt #2
- EXEC SQL END DECLARE SECTION;
- */
- static struct {
- unsigned long sq001N;
- unsigned char *sq001V[4];
- unsigned long sq001L[4];
- unsigned short sq001T[4];
- unsigned short *sq001I[4];
- } sq001 = {4};
- static struct {
- unsigned long sq002N;
- unsigned char *sq002V[1];
- unsigned long sq002L[1];
- unsigned short sq002T[1];
- unsigned short *sq002I[1];
- } sq002 = {1};
- static char sq003[45] =
- "SELECT NVL(MAX(EMPNO),0)+10 INTO:b1 FROM EMP"
- ;
- static struct {
- unsigned long sq004N;
- unsigned char *sq004V[1];
- unsigned long sq004L[1];
- unsigned short sq004T[1];
- unsigned short *sq004I[1];
- } sq004 = {1};
- static struct {
- unsigned long sq005N;
- unsigned char *sq005V[1];
- unsigned long sq005L[1];
- unsigned short sq005T[1];
- unsigned short *sq005I[1];
- } sq005 = {1};
- static char sq006[48] =
- "SELECT DNAME INTO:b1 FROM DEPT WHERE DEPTNO=:b2"
- ;
- static struct {
- unsigned long sq007N;
- unsigned char *sq007V[1];
- unsigned long sq007L[1];
- unsigned short sq007T[1];
- unsigned short *sq007I[1];
- } sq007 = {1};
- static struct {
- unsigned long sq008N;
- unsigned char *sq008V[1];
- unsigned long sq008L[1];
- unsigned short sq008T[1];
- unsigned short *sq008I[1];
- } sq008 = {1};
- static struct {
- unsigned long sq009N;
- unsigned char *sq009V[1];
- unsigned long sq009L[1];
- unsigned short sq009T[1];
- unsigned short *sq009I[1];
- } sq009 = {1};
- static char sq010[71] =
- "INSERT INTO EMP(EMPNO,ENAME,JOB,SAL,DEPTNO)VALUES(:b1,:b2,:b3,:b4,:b5)"
- ;
- static struct {
- unsigned long sq011N;
- unsigned char *sq011V[5];
- unsigned long sq011L[5];
- unsigned short sq011T[5];
- unsigned short *sq011I[5];
- } sq011 = {5};
- static struct {
- unsigned long sq012N;
- unsigned char *sq012V[1];
- unsigned long sq012L[1];
- unsigned short sq012T[1];
- unsigned short *sq012I[1];
- } sq012 = {1};
- static struct {
- unsigned long sq013N;
- unsigned char *sq013V[1];
- unsigned long sq013L[1];
- unsigned short sq013T[1];
- unsigned short *sq013I[1];
- } sq013 = {1};
- static int SQLTM[8];
- static int sqlcun[3] = {
- 0,0,0};
- static int sqlusi[1] = {
- 0};
- static unsigned long sqlami = 0;
- static int SQLBT0 = 1;
- static int SQLBT1 = 2;
- static int SQLBT2 = 4;
- static int SQLBT3 = 9;
- static unsigned long sqlvsn = 10315;
- static int IAPSUCC = 0;
- static int IAPFAIL = 1403;
- static int IAPFTL = 535;
- extern void sqlab2();
- extern void sqlad2();
- extern void sqlbs2();
- extern void sqlcom();
- extern void sqlexe();
- extern void sqlfch();
- extern void sqliem();
- extern void sqllo2();
- extern void sqlopn();
- extern void sqlosq();
- extern void sqlrol();
- extern void sqlsca();
- extern void sqlscc();
- extern void sqlsch();
- extern void sqltfl();
- /* SQL stmt #3
- EXEC SQL INCLUDE sqlca.h;
- */
- /*
- * $Header: sqlca.h,v 10301.3 89/02/22 11:07:56 nsalah Exp $ sqlca.h Copyr (c) 1987 Oracle
- */
-
- /* v1pcc,10301,v1pcc$src:[defs.10301] */
- /* Copyright (c) 1985,1986 by Oracle Corporation. */
-
- /*
- NAME
- SQLCA : SQL Communications Area.
- FUNCTION
- Contains no code. Oracle fills in the SQLCA with status info
- during the execution of a SQL stmt.
- NOTES
- If the symbol SQLCA_STORAGE_CLASS is defined, then the SQLCA
- will be defined to have this storage class. For example:
-
- #define SQLCA_STORAGE_CLASS extern
-
- will define the SQLCA as an extern.
-
- If the symbol SQLCA_INIT is defined, then the SQLCA will be
- statically initialized. Although this is not necessary in order
- to use the SQLCA, it is a good pgming practice not to have
- unitialized variables. However, some C compilers/OS's don't
- allow automatic variables to be init'd in this manner. Therefore,
- if you are INCLUDE'ing the SQLCA in a place where it would be
- an automatic AND your C compiler/OS doesn't allow this style
- of initialization, then SQLCA_INIT should be left undefined --
- all others can define SQLCA_INIT if they wish.
-
- MODIFIED
- Clare 12/06/84 - Ch SQLCA to not be an extern.
- Clare 10/21/85 - Add initialization.
- Bradbury 01/05/86 - Only initialize when SQLCA_INIT set
- Clare 06/12/86 - Add SQLCA_STORAGE_CLASS option.
- */
-
- #ifndef SQLCA
- #define SQLCA 1
-
- struct sqlca
- {
- /* ub1 */ char sqlcaid[8];
- /* b4 */ long sqlabc;
- /* b4 */ long sqlcode;
- struct
- {
- /* ub2 */ unsigned short sqlerrml;
- /* ub1 */ char sqlerrmc[70];
- } sqlerrm;
- /* ub1 */ char sqlerrp[8];
- /* b4 */ long sqlerrd[6];
- /* ub1 */ char sqlwarn[8];
- /* ub1 */ char sqlext[8];
- };
-
- #ifdef SQLCA_STORAGE_CLASS
- SQLCA_STORAGE_CLASS struct sqlca sqlca
- #else
- struct sqlca sqlca
- #endif
-
- #ifdef SQLCA_INIT
- = {
- {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
- sizeof(struct sqlca),
- 0,
- { 0, {0}},
- {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0}
- }
- #endif
- ;
-
- #endif
-
- /* end SQLCA */
-
- main()
- {
-
- /* -----------------------------------------------------------------------------
- logon to ORACLE, and open the cursors. The program exits if any errors occur.
- ----------------------------------------------------------------------------- */
-
- strcpy(uid.arr,"SCOTT");
- uid.len = strlen(uid.arr);
- strcpy(pwd.arr,"TIGER");
- pwd.len = strlen(pwd.arr);
-
- /* SQL stmt #4
- EXEC SQL WHENEVER SQLERROR GOTO errexit;
- EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- sq001.sq001V[0] = (unsigned char *)&uid.len;
- sq001.sq001L[0] = (unsigned long)22;
- sq001.sq001T[0] = (unsigned short)9;
- sq001.sq001I[0] = (unsigned short *)0;
- sq001.sq001V[1] = (unsigned char *)&pwd.len;
- sq001.sq001L[1] = (unsigned long)22;
- sq001.sq001T[1] = (unsigned short)9;
- sq001.sq001I[1] = (unsigned short *)0;
- sq001.sq001T[2] = (unsigned short)10;
- sq001.sq001T[3] = (unsigned short)10;
- SQLTM[0] = (int)0;
- SQLTM[1] = (int)10;
- sqllo2(
- &sq001.sq001N,sq001.sq001V,sq001.sq001L,sq001.sq001T,
- &sqlami, &SQLTM[0], &SQLTM[1], &sqlvsn);
- if (sqlca.sqlcode < 0) goto errexit;
- } /* ending of SQL code gen stmt */
-
- /* -----------------------------------------------------------------------------
- Retrieve the current maximum employee number
- ----------------------------------------------------------------------------- */
-
- /* SQL stmt #6
- EXEC SQL SELECT NVL(MAX(EMPNO),0) + 10
- INTO :empno
- FROM EMP;
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- if ( !sqlusi[0] )
- { /* OPEN SCOPE */
- sq002.sq002T[0] = (unsigned short)10;
- SQLTM[0] = (int)4;
- sqlbs2(&sq002.sq002N, sq002.sq002V,
- sq002.sq002L, sq002.sq002T, sq002.sq002I,
- &SQLTM[0], &sqlusi[0]);
- } /* CLOSE SCOPE */
- sqlsch(&sqlusi[0]);
- sqlscc(&sqlcun[0]);
- sqltfl(&SQLTM[0], &SQLBT0);
- if ( !SQLTM[0] )
- { /* OPEN SCOPE */
- SQLTM[0] = (int)16384;
- sqlopn(&SQLTM[0], &SQLBT3, &sqlvsn);
- SQLTM[0] = (int)44;
- sqlosq(sq003, &SQLTM[0]);
- sq004.sq004V[0] = (unsigned char *)&empno;
- sq004.sq004L[0] = (unsigned long)4;
- sq004.sq004T[0] = (unsigned short)3;
- sq004.sq004I[0] = (unsigned short *)0;
- sqlad2(&sq004.sq004N, sq004.sq004V,
- sq004.sq004L, sq004.sq004T, sq004.sq004I);
- } /* CLOSE SCOPE */
- SQLTM[0] = (int)1;
- sqlexe(&SQLTM[0]);
- SQLTM[0] = (int)1;
- SQLTM[1] = (int)0;
- sqlfch(&SQLTM[0], &SQLTM[1]);
- if (sqlca.sqlcode < 0) goto errexit;
- } /* ending of SQL code gen stmt */
-
- /* -----------------------------------------------------------------------------
- read the user's input from STDIN. If the employee name is
- not entered, exit.
- Verify that the entered department number is valid and echo the
- department's name
- ----------------------------------------------------------------------------- */
-
- for( ; ; empno+=10 )
- {
- int l;
-
- /* Get employee name to be inserted.
-
- IMPORTANT NOTE: beware of coding as follows (I got burnt, 1st time):
-
- ename.len = asks("Enter employee name : ", ename.arr);
- if ( ename.len <= 0 )
- ..etc..
-
- In the above, asks() returns an int, but ename.len is an unsigned
- short (see SQLCA). Therefore, the "if" fails for <EOF> (which returns
- -1) because, by definition, the unsigned short can't be negative.
- */
- l = asks("Enter employee name : ",ename.arr);
-
- if ( l <= 0 )
- break;
-
- ename.len = l;
-
- job.len = asks("Enter employee job : ",job.arr);
- askn("Enter employee salary: ",&sal);
- for ( ; ; )
- {
- if ( askn("Enter employee dept : ",&deptno) < 0 )
- break;
-
- /* SQL stmt #7
- EXEC SQL WHENEVER NOT FOUND GOTO nodept;
- EXEC SQL SELECT DNAME
- INTO :dname
- FROM DEPT
- WHERE DEPTNO = :deptno;
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- if ( !sqlusi[0] )
- { /* OPEN SCOPE */
- sq005.sq005T[0] = (unsigned short)10;
- SQLTM[0] = (int)4;
- sqlbs2(&sq005.sq005N, sq005.sq005V,
- sq005.sq005L, sq005.sq005T, sq005.sq005I,
- &SQLTM[0], &sqlusi[0]);
- } /* CLOSE SCOPE */
- sqlsch(&sqlusi[0]);
- sqlscc(&sqlcun[1]);
- sqltfl(&SQLTM[0], &SQLBT0);
- if ( !SQLTM[0] )
- { /* OPEN SCOPE */
- SQLTM[0] = (int)16384;
- sqlopn(&SQLTM[0], &SQLBT3, &sqlvsn);
- SQLTM[0] = (int)47;
- sqlosq(sq006, &SQLTM[0]);
- sq007.sq007V[0] = (unsigned char *)&deptno;
- sq007.sq007L[0] = (unsigned long)4;
- sq007.sq007T[0] = (unsigned short)3;
- sq007.sq007I[0] = (unsigned short *)0;
- sqlab2(&sq007.sq007N, sq007.sq007V,
- sq007.sq007L, sq007.sq007T, sq007.sq007I);
- sq008.sq008V[0] = (unsigned char *)&dname.len;
- sq008.sq008L[0] = (unsigned long)17;
- sq008.sq008T[0] = (unsigned short)9;
- sq008.sq008I[0] = (unsigned short *)0;
- sqlad2(&sq008.sq008N, sq008.sq008V,
- sq008.sq008L, sq008.sq008T, sq008.sq008I);
- } /* CLOSE SCOPE */
- SQLTM[0] = (int)1;
- sqlexe(&SQLTM[0]);
- SQLTM[0] = (int)1;
- SQLTM[1] = (int)0;
- sqlfch(&SQLTM[0], &SQLTM[1]);
- if (sqlca.sqlcode == 1403) goto nodept;
- if (sqlca.sqlcode < 0) goto errexit;
- } /* ending of SQL code gen stmt */
-
- dname.arr[dname.len] = '\0';
-
- /* SQL stmt #9
- EXEC SQL WHENEVER NOT FOUND STOP;
- */
-
- /* Here if deptno was found in dbs. Insert new employee into dbs. */
-
- /* SQL stmt #10
- EXEC SQL INSERT INTO EMP(EMPNO,ENAME,JOB,SAL,DEPTNO)
- VALUES (:empno,:ename,:job,:sal,:deptno);
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- if ( !sqlusi[0] )
- { /* OPEN SCOPE */
- sq009.sq009T[0] = (unsigned short)10;
- SQLTM[0] = (int)4;
- sqlbs2(&sq009.sq009N, sq009.sq009V,
- sq009.sq009L, sq009.sq009T, sq009.sq009I,
- &SQLTM[0], &sqlusi[0]);
- } /* CLOSE SCOPE */
- sqlsch(&sqlusi[0]);
- sqlscc(&sqlcun[2]);
- sqltfl(&SQLTM[0], &SQLBT0);
- if ( !SQLTM[0] )
- { /* OPEN SCOPE */
- SQLTM[0] = (int)16384;
- sqlopn(&SQLTM[0], &SQLBT3, &sqlvsn);
- SQLTM[0] = (int)70;
- sqlosq(sq010, &SQLTM[0]);
- sq011.sq011V[0] = (unsigned char *)&empno;
- sq011.sq011L[0] = (unsigned long)4;
- sq011.sq011T[0] = (unsigned short)3;
- sq011.sq011I[0] = (unsigned short *)0;
- sq011.sq011V[1] = (unsigned char *)&ename.len;
- sq011.sq011L[1] = (unsigned long)17;
- sq011.sq011T[1] = (unsigned short)9;
- sq011.sq011I[1] = (unsigned short *)0;
- sq011.sq011V[2] = (unsigned char *)&job.len;
- sq011.sq011L[2] = (unsigned long)17;
- sq011.sq011T[2] = (unsigned short)9;
- sq011.sq011I[2] = (unsigned short *)0;
- sq011.sq011V[3] = (unsigned char *)&sal;
- sq011.sq011L[3] = (unsigned long)4;
- sq011.sq011T[3] = (unsigned short)3;
- sq011.sq011I[3] = (unsigned short *)0;
- sq011.sq011V[4] = (unsigned char *)&deptno;
- sq011.sq011L[4] = (unsigned long)4;
- sq011.sq011T[4] = (unsigned short)3;
- sq011.sq011I[4] = (unsigned short *)0;
- sqlab2(&sq011.sq011N, sq011.sq011V,
- sq011.sq011L, sq011.sq011T, sq011.sq011I);
- } /* CLOSE SCOPE */
- SQLTM[0] = (int)1;
- sqlexe(&SQLTM[0]);
- if (sqlca.sqlcode < 0) goto errexit;
- } /* ending of SQL code gen stmt */
-
- printf("\n%s added to the %s department as employee number %d\n",
- ename.arr,dname.arr,empno);
- break;
-
- /* Here if deptno NOT found in dbs */
- nodept:
- printf("\nNo such department\n");
- continue;
- }
- }
-
- /* -----------------------------------------------------------------------------
- close the cursors and log off from ORACLE
- ----------------------------------------------------------------------------- */
-
- /* SQL stmt #11
- EXEC SQL COMMIT WORK RELEASE;
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- if ( !sqlusi[0] )
- { /* OPEN SCOPE */
- sq012.sq012T[0] = (unsigned short)10;
- SQLTM[0] = (int)4;
- sqlbs2(&sq012.sq012N, sq012.sq012V,
- sq012.sq012L, sq012.sq012T, sq012.sq012I,
- &SQLTM[0], &sqlusi[0]);
- } /* CLOSE SCOPE */
- sqlsch(&sqlusi[0]);
- SQLTM[0] = (int)1;
- sqlcom(&SQLTM[0]);
- if (sqlca.sqlcode < 0) goto errexit;
- } /* ending of SQL code gen stmt */
- printf ("\nEnd of the C/ORACLE example program.\n");
- return(1);
-
- errexit:
- errrpt();
- /* SQL stmt #12
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- EXEC SQL ROLLBACK WORK RELEASE;
- */
- { /* beginning of SQL code gen stmt */
- sqlsca(&sqlca);
- if ( !sqlusi[0] )
- { /* OPEN SCOPE */
- sq013.sq013T[0] = (unsigned short)10;
- SQLTM[0] = (int)4;
- sqlbs2(&sq013.sq013N, sq013.sq013V,
- sq013.sq013L, sq013.sq013T, sq013.sq013I,
- &SQLTM[0], &sqlusi[0]);
- } /* CLOSE SCOPE */
- sqlsch(&sqlusi[0]);
- SQLTM[0] = (int)1;
- sqlrol(&SQLTM[0]);
- } /* ending of SQL code gen stmt */
- return(0);
- }
-
- /*------------------------------------------------------------------------------
- COUNT askn(text,variable)
-
- print the 'text' on STDOUT and read an integer variable from
- SDTIN.
-
- text points to the null terminated string to be printed
- variable points to an integer variable
-
- askn returns a 1 if the variable was read successfully or a
- -1 if -eof- was encountered
- ----------------------------------------------------------------------------- */
-
- int askn(text,variable)
- char text[];
- int *variable;
- {
- char s[20];
- printf(text);
- if ( gets(s) == (char *)0 )
- return(EOF);
-
- *variable = atoi(s);
- return(1);
- }
-
- /* -----------------------------------------------------------------------------
- COUNT asks(text,variable)
-
- print the 'text' on STDOUT and read up to 'len' characters into
- the buffer pointed to by variable from STDIN.
-
- text points to the null terminated string to be printed
- variable points to a buffer of at least 'len'+1 characters
-
- asks returns the number of character read into the string, or a
- -1 if -eof- was encountered
- ----------------------------------------------------------------------------- */
-
- int asks(text,variable)
- char text[],variable[];
- {
- printf(text);
- return( gets(variable) == (char *)0 ? EOF : strlen(variable) );
- }
-
- /* -----------------------------------------------------------------------------
- VOID errrpt()
-
- errrpt prints the ORACLE error msg and number.
- ----------------------------------------------------------------------------- */
-
- errrpt()
- {
- printf("\n%.70s (%d)\n", sqlca.sqlerrm.sqlerrmc, -sqlca.sqlcode);
- return(0);
- }
-