Skip to content

DL/I from COBOL, C, Rust, Java and Python

The DL/I layer is a library, liboliviadb_dli (in every release archive, with its header oliviadb_dli.h): a host program opens a directory as the database store, loads the DBD and PSB sources, schedules the PSB and makes DL/I calls through CBLTDLI (and AIBTDLI, PLITDLI, ...) or oliviadb_call. What the library implements, and the C ABI in full: embedding the DL/I layer. The bindings for Rust, Java and Python make the same calls (sections at the end). Every example below is built and run in CI (tools/docs-snippets.py, group dli; the bindings' groups dli-rust, dli-java and dli-python with a checkout of the SDK, and against the registries' packages in the release smoke test).

A database and a PSB

lib/PARTDB.dbd and lib/PARTPSB.psb, DBDGEN and PSBGEN source as on IMS:

         DBD   NAME=PARTDB,ACCESS=(HIDAM,VSAM)
         SEGM  NAME=PART,PARENT=0,BYTES=20
         FIELD NAME=(PARTNO,SEQ,U),BYTES=6,START=1
         FIELD NAME=DESC,BYTES=14,START=7
         SEGM  NAME=STOCK,PARENT=PART,BYTES=10
         FIELD NAME=(LOC,SEQ,U),BYTES=4,START=1
         FIELD NAME=QTY,BYTES=6,START=5
         DBDGEN
         FINISH
         END
         PCB   TYPE=DB,DBDNAME=PARTDB,PROCOPT=A,KEYLEN=10
         SENSEG NAME=PART,PARENT=0
         SENSEG NAME=STOCK,PARENT=PART
         PSBGEN LANG=COBOL,PSBNAME=PARTPSB
         END

COBOL (GnuCOBOL)

The program inserts two parts and a stock record, reads them back with GU and GN, replaces one and deletes the other. An unqualified GN that goes up to a higher level returns GA ("IMS crossed a hierarchic boundary into a higher level"), the end of the database GB (IMS 15.5 Messages and Codes, Volume 4, Chapter 9 "DL/I status codes", GA p.157). Each call passes the argument count first (the optional parmcount of IMS), since the library cannot see how many arguments a COBOL CALL passed.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PARTPGM.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
      *    The argument count first (parmcount, optional in IMS): a C
      *    library cannot tell how many arguments a COBOL CALL passed.
       01  THREE      PIC S9(9) COMP VALUE 3.
       01  FOUR       PIC S9(9) COMP VALUE 4.
       01  FIVE       PIC S9(9) COMP VALUE 5.
       01  GU-FUNC    PIC X(4) VALUE 'GU  '.
       01  GN-FUNC    PIC X(4) VALUE 'GN  '.
       01  GHU-FUNC   PIC X(4) VALUE 'GHU '.
       01  ISRT-FUNC  PIC X(4) VALUE 'ISRT'.
       01  REPL-FUNC  PIC X(4) VALUE 'REPL'.
       01  DLET-FUNC  PIC X(4) VALUE 'DLET'.
       01  PART-SSA   PIC X(9)  VALUE 'PART     '.
       01  STOCK-SSA  PIC X(9)  VALUE 'STOCK    '.
       01  PART-Q.
           05 FILLER  PIC X(19) VALUE 'PART    (PARTNO  = '.
           05 Q-KEY   PIC X(6).
           05 FILLER  PIC X     VALUE ')'.
       01  PART-IO.
           05 PARTNO  PIC X(6).
           05 PDESC   PIC X(14).
       01  STOCK-IO.
           05 LOC     PIC X(4).
           05 QTY     PIC X(6).
       LINKAGE SECTION.
       01  DBPCB.
           05 DBD-NAME   PIC X(8).
           05 SEG-LEVEL  PIC X(2).
           05 STATUS-CD  PIC X(2).
           05 FILLER     PIC X(28).
       PROCEDURE DIVISION USING DBPCB.
      *    Insert two parts, each with a stock record.
           MOVE 'P00001BOLT          ' TO PART-IO
           CALL 'CBLTDLI' USING FOUR ISRT-FUNC DBPCB PART-IO PART-SSA
           MOVE 'A1  000010' TO STOCK-IO
           MOVE 'P00001' TO Q-KEY
           CALL 'CBLTDLI' USING FIVE ISRT-FUNC DBPCB STOCK-IO
                                PART-Q STOCK-SSA
           MOVE 'P00002NUT           ' TO PART-IO
           CALL 'CBLTDLI' USING FOUR ISRT-FUNC DBPCB PART-IO PART-SSA
           DISPLAY 'ISRT ' STATUS-CD
      *    Read one part back by key, then the database in sequence.
           MOVE 'P00001' TO Q-KEY
           CALL 'CBLTDLI' USING FOUR GU-FUNC DBPCB PART-IO PART-Q
           DISPLAY 'GU   ' STATUS-CD ' ' PART-IO
      *    GA: an unqualified GN went up a level; GB: the end.
           PERFORM UNTIL STATUS-CD NOT = SPACES AND STATUS-CD NOT = 'GA'
               MOVE SPACES TO PART-IO
               CALL 'CBLTDLI' USING THREE GN-FUNC DBPCB PART-IO
               IF STATUS-CD NOT = 'GB'
                   DISPLAY 'GN   ' STATUS-CD ' ' PART-IO
               END-IF
           END-PERFORM
      *    Replace a part's description, then delete the other part.
           MOVE 'P00002' TO Q-KEY
           CALL 'CBLTDLI' USING FOUR GHU-FUNC DBPCB PART-IO PART-Q
           MOVE 'HEX NUT' TO PDESC
           CALL 'CBLTDLI' USING THREE REPL-FUNC DBPCB PART-IO
           DISPLAY 'REPL ' STATUS-CD
           MOVE 'P00001' TO Q-KEY
           CALL 'CBLTDLI' USING FOUR GHU-FUNC DBPCB PART-IO PART-Q
           CALL 'CBLTDLI' USING THREE DLET-FUNC DBPCB PART-IO
           DISPLAY 'DLET ' STATUS-CD
           GOBACK.

A small C host schedules the PSB and calls the program with its PCB:

/* Runs the COBOL program PARTPGM against OliviaDB: opens the database directory,
 * loads the DBD and PSB, schedules PARTPSB, and calls the program with its PCB.
 * usage: run <database directory> <library directory> */
/* libcob.h of GnuCOBOL 3.1 uses size_t without including its header. */
#include <stddef.h>
#include <libcob.h>
#include <stdio.h>
#include "oliviadb_dli.h"

extern int PARTPGM(void *pcb);

int main(int argc, char **argv) {
    if (argc < 3) return 2;
    cob_init(0, NULL);
    oliviadb_system *sys = oliviadb_open(argv[1], 1);
    if (!sys || oliviadb_load_library(sys, argv[2]) != 0) {
        fprintf(stderr, "%s\n", oliviadb_last_error());
        return 1;
    }
    /* GnuCOBOL: ASCII data, big-endian binary items. */
    oliviadb_program *prog = oliviadb_schedule(sys, "PARTPSB", OLIVIADB_RUNTIME_ASCII_BE);
    if (!prog) {
        fprintf(stderr, "%s\n", oliviadb_last_error());
        return 1;
    }
    oliviadb_set_current(prog); /* CBLTDLI calls go to this program */
    PARTPGM(oliviadb_pcb_address(prog, 1));
    int rc = oliviadb_terminate(prog); /* commits */
    oliviadb_close(sys);
    return rc == 0 ? 0 : 1;
}
cobc -x -o run run.c PARTPGM.cbl -I <dir of oliviadb_dli.h> -L <dir of liboliviadb_dli> -loliviadb_dli
./run partdb lib

It prints (GnuCOBOL with its default big-endian binary items):

ISRT   
GU      P00001BOLT          
GN      A1  000010
GN   GA P00002NUT           
REPL   
DLET   

A COBOL CALL does not use C's variadic convention; CBLTDLI takes such calls on every platform (C ABI 13; on Apple silicon the header sends C's own variadic calls to oliviadb_CBLTDLI_va), see embedding the DL/I layer.

C

/* The DL/I C ABI from C: open a database directory, load the DBD and PSB, schedule the
 * PSB, then ISRT, GU and GN through CBLTDLI (oliviadb_set_argc gives each call's argument
 * count), and commit. usage: capi <database directory> <library directory> */
#include <stdio.h>
#include <string.h>
#include "oliviadb_dli.h"

static char *status(unsigned char *pcb) {
    static char s[3];
    memcpy(s, pcb + 10, 2); /* the PCB's status code */
    return s;
}

int main(int argc, char **argv) {
    if (argc < 3) return 2;
    oliviadb_system *sys = oliviadb_open(argv[1], 1);
    if (!sys || oliviadb_load_library(sys, argv[2]) != 0) {
        fprintf(stderr, "%s\n", oliviadb_last_error());
        return 1;
    }
    oliviadb_program *prog = oliviadb_schedule(sys, "PARTPSB", OLIVIADB_RUNTIME_ASCII_LE);
    if (!prog) {
        fprintf(stderr, "%s\n", oliviadb_last_error());
        return 1;
    }
    oliviadb_set_current(prog);
    unsigned char *pcb = oliviadb_pcb_address(prog, 1);
    char part[21], io[21];
    const char *parts[] = {"P00010WASHER        ", "P00011SPRING        "};
    for (int i = 0; i < 2; i++) {
        memcpy(part, parts[i], 20);
        oliviadb_set_argc(4);
        CBLTDLI("ISRT", pcb, part, "PART     ");
        printf("ISRT %s\n", status(pcb));
    }
    oliviadb_set_argc(4);
    CBLTDLI("GU  ", pcb, io, "PART    (PARTNO  = P00011)");
    io[20] = 0;
    printf("GU   %s %s\n", status(pcb), io);
    oliviadb_set_argc(3);
    CBLTDLI("GN  ", pcb, io);
    printf("GN   %s\n", status(pcb)); /* GB: the end of the database */
    int rc = oliviadb_terminate(prog);
    oliviadb_close(sys);
    return rc == 0 ? 0 : 1;
}
cc -o capi capi.c -I <dir of oliviadb_dli.h> -L <dir of liboliviadb_dli> -loliviadb_dli
./capi partdb2 lib
ISRT   
ISRT   
GU      P00011SPRING        
GN   GB

Rust

The crate oliviadb-dli loads liboliviadb_dli at run time (the file OLIVIADB_DLI_LIB names, else the system's library search path) and makes the same calls as the C example through oliviadb_call, each argument with its length; Ssa builds the segment search arguments. It prints what the C example prints. The crate is Apache-2.0; the library it loads is licensed separately.

[dependencies]
oliviadb-dli = "0.1"
// usage: main <database directory> <library directory>
use oliviadb_dli::{Library, Runtime, Ssa};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<String> = std::env::args().collect();
    let sys = Library::load()?.open(&args[1], true)?;
    sys.load_library(&args[2])?;
    let mut prog = sys.schedule("PARTPSB", Runtime::AsciiLittleEndian)?;
    let part = Ssa::new("PART").build()?;
    for p in [b"P00010WASHER        ", b"P00011SPRING        "] {
        let mut io = p.to_vec();
        println!("ISRT {}", prog.call(1, "ISRT", &mut io, &[&part])?);
    }
    let mut io = vec![b' '; 20];
    let q = Ssa::new("PART").qual("PARTNO", "=", b"P00011").build()?;
    let status = prog.call(1, "GU", &mut io, &[&q])?;
    println!("GU   {status} {}", String::from_utf8_lossy(&io));
    println!("GN   {}", prog.call(1, "GN", &mut io, &[])?); // GB: the end of the database
    prog.terminate()?; // commit
    Ok(())
}

Java

The Maven artifact com.olivia-db:oliviadb-dli carries the library (with its JNI binding) for Linux x86_64 and aarch64 and macOS aarch64 and x86_64, and loads it from the jar unless the system property oliviadb.jni.lib or the variable OLIVIADB_JNI_LIB names a file. Its Java code is Apache-2.0; the jar as a whole, with the library, is Apache-2.0 AND (AGPL-3.0-only OR LicenseRef-OliviaDB-Commercial). It prints what the C example prints.

<dependency>
  <groupId>com.olivia-db</groupId>
  <artifactId>oliviadb-dli</artifactId>
  <version>0.1.0</version>
</dependency>
// usage: java Main.java <database directory> <library directory>
import com.oliviadb.dli.DliProgram;
import com.oliviadb.dli.DliStatus;
import com.oliviadb.dli.OliviaDbSystem;
import com.oliviadb.dli.Pcb;
import com.oliviadb.dli.Runtime;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        try (OliviaDbSystem sys = OliviaDbSystem.open(Path.of(args[0]))) {
            sys.loadLibrary(Path.of(args[1]));
            try (DliProgram prog = sys.schedule("PARTPSB", Runtime.ASCII_LE)) {
                Pcb pcb = prog.pcb(1);
                for (String part : new String[] {"P00010WASHER        ", "P00011SPRING        "}) {
                    byte[] io = part.getBytes(StandardCharsets.ISO_8859_1);
                    System.out.println("ISRT " + pcb.call("ISRT", io, "PART     ").code());
                }
                byte[] io = new byte[20];
                DliStatus st = pcb.call("GU", io, "PART    (PARTNO  = P00011)");
                System.out.println("GU   " + st.code() + " " + new String(io, StandardCharsets.ISO_8859_1));
                System.out.println("GN   " + pcb.call("GN", io).code()); // GB: the end of the database
            } // close: the program ends normally, its updates are committed
        }
    }
}

Python

The PyPI distribution oliviadb-dli (pip install oliviadb-dli) has wheels for Linux x86_64 and aarch64 and macOS arm64 and x86_64 that carry the library, and loads it from the wheel unless OLIVIADB_DLI_LIB names a file. Its Python code is Apache-2.0; a wheel as a whole, with the library, is Apache-2.0 AND (AGPL-3.0-only OR LicenseRef-OliviaDB-Commercial). It prints what the C example prints.

# usage: python main.py <database directory> <library directory>
import sys

from oliviadb_dli import Library, Runtime, Ssa

with Library.load().open(sys.argv[1]) as system:
    system.load_library(sys.argv[2])
    prog = system.schedule("PARTPSB", Runtime.ASCII_LE)
    part = Ssa("PART").build()
    for p in (b"P00010WASHER        ", b"P00011SPRING        "):
        print("ISRT", prog.call(1, "ISRT", bytearray(p), [part]))
    io = bytearray(20)
    status = prog.call(1, "GU", io, [Ssa("PART").qual("PARTNO", "=", b"P00011").build()])
    print("GU  ", status, io.decode("latin-1"))
    print("GN  ", prog.call(1, "GN", io))  # GB: the end of the database
    prog.terminate()  # commit