Cobol and PostgreSQL

From PostgreSQL_wiki
Revision as of 21:48, 7 November 2025 by Martin (talk | contribs) (gixsql precompilation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This chapter contains the description of a complete working example of a Cobol PostgreSQL program, the setup, source code and a simple compilation workflow are below.

  • Program name: pgcobol
  • Source code: pgcobol.cbl
  • Intermediate preprocessed source code file: pgcobol.cbsql
  • Cobol compiler output: pgcobol.out
  • Database: Employee, with table emptable
  • pgcobol print output file: pgcobol.prt

GixSQL is a way in which Cobol programs are able to use modern database management systems in a Linux environment.

Cobol

Cobol has been around for more than 60 years and will stay around for the foreseeable future, estimates are that there are about 800 billion lines of Business Cobol code around. The programs are heavily used, servicing about 80% of all financial and business transactions world wide.

The reason behind this endurance lies in the fact that Cobol is a standardised language, a program written in 1974 can be compiled today with a modern compiler. The resulting executable program still runs today, without the need of adaptation. This very fact is of high value to enterprises, their investments are safe and well protected on the long term.

Traditionally Cobol programs have their application in big business mainframe environments. These large enterprises, however, are shifting more and more of their applications to the Linux platform. The broad adoption of Linux in the enterprise environment started around 2010. So, it is no wonder that Cobol gradually is being made available for the Linux platform, enter GNUCobol.

GNUCobol

Is an Open Source Cobol compiler, the project originally started in 2002 GNUCobol as OpenCobol. GNUCobol now is a full blown Cobol compiler.

GixSQL links PostgreSQL and Cobol

PostgreSQL is getting more and more traction around the world, also in the big enterprises. Big enterprises have ample reason to stick to their Cobol program stack, in which they invested huge amounts of money. So if PostgreSQL is to make it to the business applications of the big enterprises, it has to find a way to interact in a reliable manner with Cobol. Enter the GixSQL SQL preprocessor for Cobol.

An SQL statement in Cobol

This is the way the SQL code is embedded in the Cobol program:

           EXEC SQL
              CONNECT TO :DATASRC AS cobol_sql USER :DBUSR USING :DBPWD
           END-EXEC.

The example shows the program making a connection to the database with the variables DATASRC, DBUSR and DBPWD.

Practical introduction to GixSQL

This chapter provides guidance to set up a working GixSQL for GNUCobol with PostgreSQL development environment.

Prerequisites

  • Debian 12
  • GnuCobol4
  • libmariadb-dev libmariadb-dev-compat libpq5 unixodbc libfmt9
  • GixSQL, find the link below
  • optionally the PostgreSQL database server on the same machine.

GixSQL is available here: GixSQL. Just install the software in the sequence as described above and the system is ready to go.
For reasons of convenience add the follow lines to /etx/profile:

LD_DIRECTORY_PATH="/usr/local/src/gix/gixisql/runtime/libgixsql:/usr/local/src/gix/gixsql/common:/usr/local/src/gix/gixsql/common"
export LD_DIRECTORY_PATH

Workflow

Once the editing of the program is finished, the program has to be compiled. This is done in various steps, but this can also be done by using the Python script below.
The steps are:

  1. The precompile process
  2. Inspect (but do not edit the output)
  3. The compilation process
  4. Inspect the compilation listing
  5. Run the program

Simple Workflow script

The python script gixc.py implements the GixSQL Cobol compilation workflow. It is invoked with a single parameter, being the program_name suffixed by either 'cbl or 'cob'. People that like to get their hands dirty follow the two steps below.

gixsql precompilation

The command invoking the precompile process is as follows:

/usr/bin/gixsql pgcobol.cbl pgcobol.cbsql -S -I . -e ".,*.cpy,*.CPY"

The input file is pgcobol.cbl, the preprocessing output file is pgcobol.cbsql. It is very important to leave the cbsql file as is, because meddling with the file easily leads to errors in the next step of the workflow.

The result of the precompile step of the SQL stement above looks like this:

GIXSQL*    EXEC SQL
GIXSQL*       CONNECT TO :DATASRC AS cobol_sql USER :DBUSR USING :DBPWD
GIXSQL*    END-EXEC.
GIXSQL     CALL STATIC "GIXSQLConnect" USING
GIXSQL         BY REFERENCE SQLCA
GIXSQL         BY REFERENCE DATASRC
GIXSQL         BY VALUE 64
GIXSQL         BY REFERENCE "cobol_sql" & x"00"
GIXSQL         BY VALUE 0
GIXSQL         BY REFERENCE x"00"
GIXSQL         BY VALUE 0
GIXSQL         BY REFERENCE DBUSR
GIXSQL         BY VALUE 64
GIXSQL         BY REFERENCE DBPWD
GIXSQL         BY VALUE 64
GIXSQL     END-CALL.

The precompile program translated the SQL statement in to a standardised Cobol call.

The cobc GNUCobol compilation

The command invoking the precompile process is as follows:

/usr/bin/cobc -x pgcobol.cbsql -l:libgixsql.so -T pgcobol.out

This results in the executable program file pgcobol and the compilation listing pgcobol.out.

Sample Program

The sample program pgcobol.cbl makes use of the sample database: employee. When executed pgcobol puts out the file pgcobol.prt.
The pgcobol program file is available for download here: pgcobol.cbl .

Sample database

The employee database is a very simple database, with one table containing three lines. The pg_dump file is in the link.


Back to: voorpagina