Skip to content

Commit ff04c27

Browse files
authored
Add precompiler (Pro*C and Pro*COBOL demos) (#457)
* Create simpleConnDemo.c Basic Connection and Query execution to Oracle Database * Update and rename drcp.c to drcpDemo.c Update the username and password to a generic string * Update makefile Make the makefile generic * Update simpleConnDemo.c Add the required setup and related blog details * Update simpleConnDemo.c Remove blog details. To be updated in README * Create sessionPoolingDemo Demo for OCI Session Pooling and Multithreading * Update README.md Add more details in the README.md file * Update README.md Minor change * Rename sessionPoolingDemo to sessionPoolingDemo.c Add .c extension in the file name * Update makefile Make the makefile more correct * Update makefile Add some comments * Add precompiler demos * Add precompilers section to main README
1 parent 61a06c0 commit ff04c27

File tree

6 files changed

+485
-0
lines changed

6 files changed

+485
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This repository stores a variety of examples demonstrating how to use the Oracle
1313
| [json-relational-duality](./json-relational-duality) | JSON Relational Duality examples
1414
| [machine-learning](./machine-learning) | Oracle Machine Learning examples |
1515
| [optimizer](./optimizer) | Oracle Optimizer and Optimizer Stats examples |
16+
| [precompilers](./precompilers) | Oracle Pro\*C and Pro\*COBOL precompiler examples |
1617
| [plsql](./plsql) | PL/SQL examples |
1718
| [python](./python) | Python examples |
1819
| [ruby](./ruby) | Ruby examples |

precompilers/Readme.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Precompiler Examples
2+
3+
This directory contains sample programs illustrating the use of Oracle Precompilers (Pro*C and Pro*COBOL).
4+
It also includes makefiles for building and compiling these samples into executables.
5+
6+
## Prerequisites
7+
8+
### Oracle Packages / Components
9+
10+
If you are using the Oracle Instant Client, install the following packages for your platform:
11+
12+
- **Basic**
13+
- **SDK**
14+
- **Precompiler**
15+
16+
### Compilers
17+
18+
- A **C/C++ compiler** is required for the Pro*C samples.
19+
- A **COBOL compiler** is required for the Pro*COBOL samples.
20+
21+
### Minimum Required Version
22+
23+
The minimum required **Oracle Database** and **Oracle Client** versions are **19c** in both cases.
24+
25+
## Included Files
26+
27+
| File Name | Description |
28+
|-----------------------|-----------------------------------------------------------------------------------------------|
29+
| [`procdemo.pc`](./procdemo.pc) | Sample Pro*C program demonstrating basic database operations using Oracle Precompiler. |
30+
| [`procobdemo.pco`](./procobdemo.pco) | Sample Pro*COBOL program illustrating database interaction using Oracle Precompiler. |
31+
| [`makefile_proc.mk`](./makefile_proc.mk) | Makefile for compiling the Pro*C example, managing dependencies, and generating the executable. |
32+
| [`makefile_procob.mk`](./makefile_procob.mk) | Makefile for compiling the Pro*COBOL example, handling build processes, and producing the executable. |
33+
34+
## How to Compile and Run
35+
36+
To build the sample programs:
37+
38+
- Use `make -f makefile_proc.mk` for **Pro*C** samples.
39+
- Use `make -f makefile_procob.mk` for **Pro*COBOL** samples.
40+
41+
Refer to the respective makefiles for more detailed instructions on compilation and execution steps.

precompilers/makefile_proc.mk

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
###############################################################################
2+
# Make file for PROC demos
3+
###############################################################################
4+
# Usage :
5+
# For compiling proc demos
6+
# make -f makefile_proc.mk
7+
#
8+
# For precompiling, compiling & linking the procdemo.pc file
9+
# make -f makefile_proc.mk build EXE=procdemo OBJS=procdemo.o
10+
#
11+
# In general, for any proc program
12+
# make -f makefile_proc.mk build EXE=<exename> OBJS="<list of dependent objs>"
13+
#
14+
# To make use of any PROC options during precompilation,
15+
# make -f makefile_proc.mk build PROCFLAGS="<list of proc options>"
16+
# EXE=<exename> OBJS="<list of dependent objs>"
17+
#
18+
# NOTES:
19+
# 1. Please change "cc/CC" and the "InstantClient directories" to point to
20+
# appropiate locations on your machine before using this makefile.
21+
# 2. In case of RPM installation, please change the following variables
22+
# as mentioned below:
23+
# PROC=/usr/lib/oracle/VV.v/client/bin/proc
24+
# CCINCLUDES=$(I_SYM)/usr/include/oracle/VV.v/client
25+
# PRECOMPPUBH=/usr/include/oracle/VV.v/client
26+
# ICLIBHOME=/usr/lib/oracle/VV.v/client/lib/
27+
# Legend:
28+
# VV - Major Oracle version number
29+
# v - Minor Oracle version number
30+
# (Ex: For the release 11.2, VV = 11 and v = 2)
31+
#
32+
###############################################################################
33+
34+
35+
CC=/usr/bin/gcc
36+
cc=/usr/bin/gcc
37+
38+
# InstantClient Directories.
39+
ICSDKHOME=../
40+
ICLIBHOME=../../
41+
42+
MKLINK=ln
43+
REMOVE=rm -rf
44+
CLNCACHE=cleancache
45+
CACHEDIR=SunWS_cachea
46+
MAKE=make
47+
MAKEFILE=makefile_proc.mk
48+
PROCDEMO=procdemo
49+
50+
PROC=$(ICSDKHOME)/proc
51+
SO_EXT=.so
52+
I_SYM=-I
53+
54+
CCINCLUDES= $(I_SYM)$(ICSDKHOME)/include
55+
56+
# Pre-compiler Flags.
57+
PRECOMPPUBH=$(ICSDKHOME)include
58+
59+
# Compiler Flags.
60+
OPTIMIZE=-O2
61+
LDPATHFLAG=-L
62+
SPFLAGS=-DLINUX -D_GNU_SOURCE -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -DSLTS_ENABLE -DSLMXMX_ENABLE -D_REENTRANT -DNS_THREADS
63+
CCFLAGS= -fPIC -DPRECOMP
64+
LDFLAGS=-g
65+
LPFLAGS=
66+
GFLAG=
67+
CDEBUG=
68+
USRFLAGS=
69+
ICLIBPATH=$(LDPATHFLAG)$(ICLIBHOME)
70+
PFLAGS=$(CCINCLUDES) $(SPFLAGS) $(LPFLAGS)
71+
CFLAGS=$(GFLAG) $(OPTIMIZE) $(CDEBUG) $(CCFLAGS) $(PFLAGS) $(USRFLAGS)
72+
73+
# Libraries.
74+
PROLDLIBS=$(LDCLIENTLIBS) $(THREADLIBS)
75+
LDCLIENTLIBS=$(ICLIBPATH) $(LLIBCLNTSH) $(LDLIBS)
76+
LLIBCLNTSH=$(LDLIBFLAG)$(LIBCLNTSHNAME)
77+
LDLIBFLAG=-l
78+
LIBCLNTCORENAME=clntshcore
79+
LIBCLNTSHNAME=clntsh
80+
LDLIBS=$(EXSYSLIBS) $(MATHLIB) $(USRLIBS)
81+
EXSYSLIBS=-ldl
82+
MATHLIB=-lm
83+
THREADLIBS=-lpthread
84+
85+
C2O=$(CC) $(CFLAGS) -c $*.c
86+
PCC2C=$(PROC) $(PROCFLAGS) iname=$(PCCSRC)
87+
DEMO_PROC_BUILD=$(CC) -o $(EXE) $(OBJS) $(LDFLAGS) $(PROLDLIBS)
88+
89+
#-----------------------------------------------------------------------------
90+
# Targets for building the proc sample programs.
91+
all: clean $(PROCDEMO)
92+
93+
$(PROCDEMO):
94+
$(MAKE) -f $(MAKEFILE) build OBJS=$@.o EXE=$@
95+
96+
build: $(CLNCACHE) $(OBJS)
97+
$(DEMO_PROC_BUILD)
98+
99+
#-----------------------------------------------------------------------------
100+
# Here are some rules for converting .pc -> .c -> .o
101+
.SUFFIXES: .pc .c .o
102+
103+
pc1:
104+
$(PCC2C)
105+
106+
.pc.c:
107+
$(MAKE) -f $(MAKEFILE) PROCFLAGS="$(PROCFLAGS)" PCCSRC=$* I_SYM=include= pc1
108+
109+
.pc.o:
110+
$(MAKE) -f $(MAKEFILE) PROCFLAGS="$(PROCFLAGS)" PCCSRC=$* I_SYM=include= pc1
111+
$(C2O)
112+
113+
.c.o:
114+
$(C2O)
115+
116+
#-----------------------------------------------------------------------------
117+
# Clean up all executables, *.o and generated *.c files
118+
clean: $(CLNCACHE)
119+
$(REMOVE) $(PROCDEMO) $(PROCDEMO).o $(PROCDEMO).c $(PROCDEMO).lis
120+
121+
cleancache:
122+
$(REMOVE) $(CACHEDIR)
123+

precompilers/makefile_procob.mk

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
###############################################################################
2+
# Make file for PROCOB demos
3+
###############################################################################
4+
# Usage :
5+
# For compiling procob demos
6+
# make -f makefile_procob.mk
7+
#
8+
# For precompiling, compiling & linking the procobdemo.pco file
9+
# make -f makefile_procob.mk build EXE=procobdemo COBS=procobdemo.cob
10+
#
11+
# In general, for any procob program
12+
# make -f makefile_procob.mk build EXE=<exename> COBS="<list of dependent cobs>"
13+
# To make use of any PROCOB options during precompilation,
14+
# make -f makefile_procob.mk build PROCOBFLAGS="<list of procob options>"
15+
# EXE=<exename> COBS="<list of dependent cobs>"
16+
#
17+
# NOTES:
18+
# 1. Please change "COB" and the "InstantClient directories" to point to
19+
# appropiate locations on your machine before using this makefile.
20+
# 2. In case of RPM installation, please change the following variables
21+
# as mentioned below:
22+
# PROCOB=/usr/lib/oracle/VV.v/client/bin/procob
23+
# ICLIBHOME=/usr/lib/oracle/VV.v/client/lib/
24+
# Legend:
25+
# VV - Major Oracle version number
26+
# v - Minor Oracle version number
27+
# (Ex: For the release 18.1, VV = 12 and v = 1)
28+
#
29+
###############################################################################
30+
31+
COB=cob
32+
33+
# InstantClient Directories.
34+
ICSDKHOME=../
35+
ICLIBHOME=../../
36+
37+
MKLINK=ln
38+
REMOVE=rm -rf
39+
CLNCACHE=cleancache
40+
CACHEDIR=SunWS_cachea
41+
MAKE=make
42+
MAKEFILE=makefile_procob.mk
43+
PROCOBDEMO=procobdemo
44+
45+
PROCOB=$(ICSDKHOME)/procob
46+
ICLIBPATH=$(LDPATHFLAG)$(ICLIBHOME)
47+
SO_EXT=.so
48+
COBFLAGS=-C IBMCOMP -C NESTCALL -t -x
49+
LDPATHFLAG=-L
50+
COBSQLINTF=$(ICLIBHOME)cobsqlintf.o
51+
LDLIBS=$(EXSYSLIBS) $(MATHLIB) $(USRLIBS)
52+
EXSYSLIBS=-ldl
53+
MATHLIB=-lm
54+
COBOL_PROLDLIBS=$(SHARED_CLIENTLIBS) $(LDLIBS)
55+
SHARED_CLIENTLIBS=$(LLIBCLNTSH) $(LDFLAGS)
56+
LLIBCLNTSH=$(LDLIBFLAG)$(LIBCLNTSHNAME)
57+
LDLIBFLAG=-l
58+
LIBCLNTCORENAME=clntshcore
59+
LIBCLNTSHNAME=clntsh
60+
LDFLAGS=-g
61+
62+
DEMO_PROCOB_BUILD=$(COB) $(COBFLAGS) -o $(EXE) $(COBS) $(ICLIBPATH) $(COBSQLINTF) $(COBOL_PROLDLIBS)
63+
64+
#-----------------------------------------------------------------------------
65+
# Targets for building the procob sample programs.
66+
#
67+
# The target 'build' puts together an executable $(EXE) from the cobol
68+
# sources in $(COBS) and the libraries in $(COBOL_PROLDLIBS).
69+
# The rules to make .cob files from .pco files are later in this file.
70+
#
71+
all: clean $(PROCOBDEMO)
72+
73+
$(PROCOBDEMO):
74+
$(MAKE) -f $(MAKEFILE) build COBS=$@.cob EXE=$@
75+
76+
build: $(CLNCACHE) $(COBS)
77+
$(DEMO_PROCOB_BUILD)
78+
79+
#-----------------------------------------------------------------------------
80+
# Here are some rules for converting .pco -> .cob -> .o and for .cob -> .gnt.
81+
#
82+
.SUFFIXES: .cob .cbl .o .pco $(GNT)
83+
84+
.pco.cob:
85+
$(PROCOB) $(PROCOBFLAGS) iname=$*.pco
86+
87+
.cob$(GNT):
88+
$(COB2GNT)
89+
90+
#-----------------------------------------------------------------------------
91+
# Clean up all executables, *.o and generated *.cob files
92+
clean: $(CLNCACHE)
93+
$(REMOVE) $(PROCOBDEMO) $(PROCOBDEMO).o $(PROCOBDEMO).cob $(PROCDEMO).lis $(PROCOBDEMO).int $(PROCOBDEMO).idy
94+
95+
cleancache:
96+
$(REMOVE) $(CACHEDIR)

precompilers/procdemo.pc

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Copyright (c) 2025, Oracle and/or its affiliates.*/
2+
/* All rights reserved.*/
3+
4+
/* NAME
5+
* procdemo.pc - Pro*C demo program
6+
*
7+
* DESCRIPTION
8+
* This program connects to ORACLE, declares and opens a cursor,
9+
* fetches the names, salaries, and commissions of all
10+
* salespeople, displays the results, then closes the cursor.
11+
*
12+
*/
13+
14+
#include <stdio.h>
15+
#include <string.h>
16+
#include <sqlca.h>
17+
#include <stdlib.h>
18+
#include <sqlda.h>
19+
#include <sqlcpr.h>
20+
21+
#define UNAME_PWD_LEN 256
22+
23+
/*
24+
* Use the precompiler typedef'ing capability to create
25+
* null-terminated strings for the authentication host
26+
* variables. (This isn't really necessary--plain char *'s
27+
* would work as well. This is just for illustration.)
28+
*/
29+
typedef char asciiz[UNAME_PWD_LEN];
30+
31+
EXEC SQL TYPE asciiz IS CHARZ(UNAME_PWD_LEN) REFERENCE;
32+
asciiz username;
33+
asciiz password;
34+
35+
struct emp_info
36+
{
37+
asciiz emp_name;
38+
float salary;
39+
float commission;
40+
};
41+
42+
void sql_error(msg)
43+
char *msg;
44+
{
45+
char err_msg[512];
46+
size_t buf_len, msg_len;
47+
48+
EXEC SQL WHENEVER SQLERROR CONTINUE;
49+
50+
printf("\n%s\n", msg);
51+
52+
/* Call sqlglm() to get the complete text of the
53+
* error message.
54+
*/
55+
buf_len = sizeof (err_msg);
56+
sqlglm(err_msg, &buf_len, &msg_len);
57+
printf("%.*s\n", msg_len, err_msg);
58+
59+
EXEC SQL ROLLBACK RELEASE;
60+
exit(EXIT_FAILURE);
61+
}
62+
63+
void main()
64+
{
65+
struct emp_info *emp_rec_ptr;
66+
67+
/* Allocate memory for emp_info struct. */
68+
if ((emp_rec_ptr =
69+
(struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
70+
{
71+
fprintf(stderr, "Memory allocation error.\n");
72+
exit(EXIT_FAILURE);
73+
}
74+
75+
/* application user to set the database credentials here */
76+
/* CAUTION: Username and password buffers are 256 characters. */
77+
/* Do not paste credentials longer than 256 characters. */
78+
79+
strcpy(username, "<username>");
80+
strcpy(password, "<password>");
81+
82+
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");
83+
84+
EXEC SQL CONNECT :username IDENTIFIED BY :password;
85+
printf("\nConnected to ORACLE as user: %s\n", username);
86+
87+
/* Declare the cursor. All static SQL explicit cursors
88+
* contain SELECT commands. 'salespeople' is a SQL identifier,
89+
* not a (C) host variable.
90+
*/
91+
EXEC SQL DECLARE salespeople CURSOR FOR
92+
SELECT ENAME, SAL, COMM
93+
FROM EMP
94+
WHERE JOB LIKE 'SALES%';
95+
96+
/* Open the cursor. */
97+
EXEC SQL OPEN salespeople;
98+
99+
/* Get ready to print results. */
100+
printf("\n\nThe company's salespeople are--\n\n");
101+
printf("Salesperson Salary Commission\n");
102+
printf("----------- ------ ----------\n");
103+
104+
/* Loop, fetching all salesperson's statistics.
105+
* Cause the program to break the loop when no more
106+
* data can be retrieved on the cursor.
107+
*/
108+
EXEC SQL WHENEVER NOT FOUND DO break;
109+
110+
for (;;)
111+
{
112+
EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
113+
printf("%s %9.2f %12.2f\n", emp_rec_ptr->emp_name,
114+
emp_rec_ptr->salary, emp_rec_ptr->commission);
115+
}
116+
117+
/* Close the cursor. */
118+
EXEC SQL CLOSE salespeople;
119+
120+
printf("\nGOOD-BYE!!\n\n");
121+
122+
EXEC SQL COMMIT WORK RELEASE;
123+
exit(EXIT_SUCCESS);
124+
}
125+

0 commit comments

Comments
 (0)