Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Mainframe Database Programming Fundamentals 1

A structured programming approach

6 min readNov 5, 2022

--

Press enter or click to view image in full size
Photo by Lenny Kuhne on Unsplash

Structured Programming approach is a technique to improve program codes reliability and clarity (refer Hunt 1979). This involves the use of Program Control Structures such as Sequential, Selection and Loop. Moreover, program codes are decomposed into small manageable chunks aka modularization. This post describes the application of Structured Programming approach in building mainframe database programs using ADABAS database and NATURAL programming language. The code examples are taken from the Nat914win official documentation (refer Nat914win fs-about).

PRE-REQUISITE:
(1) ADABAS and NATURAL Server Community Edition (Docker Version).
(2) NaturalONE IDE.
(refer here to read the setup guide for the above items).
(3) Adabas DDM for EMPLOYEES and VEHICLES database files.
(refer here to get Natural NSD format files for the above items).

(1) Program Control Structures

(1.1) Sequential Control Structure

Most programs by default start with the Sequential Control Structure i.e. the instruction statements are arranged by one after another.

Press enter or click to view image in full size
Photo by Siora Photography on Unsplash

A simple example is the classic Hello World! program below (refer Nat914win fs-hello). Here, the instruction statement execution starts from top and then move down to the bottom. The * symbol marks the lines that will be skipped by the computer during the execution. The DISPLAY statement instructs the computer to output some contents e.g. Hello world! to the user. The END statement marks the end of program execution.

* The "Hello world!" example in Natural.
*
DISPLAY "Hello world!"
END /* End of program

(1.2) Selection Control Structure

This control structure instructs the computer to select a branch of code blocks out of the available branches.

Press enter or click to view image in full size
Photo by Edu Grande on Unsplash

The following example (refer Nat914win if) demonstrates the use of the Selection Control Structure. In this example, the program is instructed to find employees who are located in the city ‘FRANKFURT’ and, if their salaries are greater than 40,000 and their birth dates are after 1st January 1945, print their name, their birth date, their salary and their vehicle’s make. This program applies a Nested IF-ELSE structure to support its complex query condition branches.

** Example 'IFEX1S': IF (structured mode)                               
************************************************************************
DEFINE DATA LOCAL
1 EMPLOY-VIEW VIEW OF EMPLOYEES
2 PERSONNEL-ID
2 NAME
2 FIRST-NAME
2 SALARY (1)
2 BIRTH
1 VEHIC-VIEW VIEW OF VEHICLES
2 PERSONNEL-ID
2 MAKE
*
1 #BIRTH (D)
END-DEFINE
*
MOVE EDITED '19450101' TO #BIRTH (EM=YYYYMMDD)
SUSPEND IDENTICAL SUPPRESS
LIMIT 20
*
FND. FIND EMPLOY-VIEW WITH CITY = 'FRANKFURT'
SORTED BY NAME BIRTH
IF SALARY (1) LT 40000
WRITE NOTITLE '*****' NAME 30X 'SALARY LT 40000'
ELSE
IF BIRTH GT #BIRTH
FIND VEHIC-VIEW WITH PERSONNEL-ID = PERSONNEL-ID (FND.)
DISPLAY (IS=ON)
NAME BIRTH (EM=YYYY-MM-DD)
SALARY (1) MAKE (AL=8)
END-FIND
END-IF
END-IF
END-FIND
END

(1.3) Loop Control Structure

Get Mohamad Mahmood’s stories in your inbox

Join Medium for free to get updates from this writer.

A Loop Control Structure is used to repeat the execution of certain instruction statements. Repetition structure helps to optimize the arrangement of instruction statements i.e. eliminating unnecessary statement redundancies.

Press enter or click to view image in full size
Photo by Ruben Christen on Unsplash

The following example (refer Nat914win repeat) demonstrates the use of loop structure to repeat certain computer processing tasks. Here, the computer user is requested to enter an employee number for the computer so that it can initiate a database search task. The search task will be repeated until the computer user decides to terminate it i.e. by entering a SPACE character.

** Example 'RPTEX1S': REPEAT (structured mode)                          
************************************************************************
DEFINE DATA LOCAL
1 EMPLOY-VIEW VIEW OF EMPLOYEES
2 PERSONNEL-ID
2 NAME
*
1 #PERS-NR (A8)
END-DEFINE
*
REPEAT
INPUT 'ENTER A PERSONNEL NUMBER:' #PERS-NR
IF #PERS-NR = ' '
ESCAPE BOTTOM
END-IF
/*
FIND EMPLOY-VIEW WITH PERSONNEL-ID = #PERS-NR
IF NO RECORD FOUND
REINPUT 'NO RECORD FOUND'
END-NOREC
DISPLAY NOTITLE NAME
END-FIND
END-REPEAT
*
END

(2) Program Modularization

Program modularization is the process of decomposing a program into smaller manageable group of codes with specific objectives. Through this way, development works such as coding and debugging would be easier. This increases programming productivities.

Press enter or click to view image in full size
Photo by TStudio_lv on Unsplash

The following example (refer Nat914win inlinesub) demonstrates the use of a type of code block called SUBROUTINE. It has a specific objective which is to mark a special employee. To achieve this objective, it assigns a data #MARK with the value ‘*’. Suppose in the future, if the developers plan a different way to mark the data, they can just make changes in the SUBROUTINE without having to concern on other parts of program. This saves their coding time while at the same time minimizing code changes risks.

DEFINE DATA
LOCAL
1 #NAME-START (A20)
1 #NAME-END (A20)
1 #MARK (A1)
1 EMPLOYEES-VIEW VIEW OF EMPLOYEES
2 FULL-NAME
3 NAME (A20)
2 DEPT (A6)
2 LEAVE-DATA
3 LEAVE-DUE (N2)
END-DEFINE
*
RP1. REPEAT
*
INPUT (AD=MT)
"Start:" #NAME-START /
"End: " #NAME-END
*
IF #NAME-START = '.' THEN
ESCAPE BOTTOM (RP1.)
END-IF
*
IF #NAME-END = ' ' THEN
MOVE #NAME-START TO #NAME-END
END-IF
*
RD1. READ EMPLOYEES-VIEW BY NAME
STARTING FROM #NAME-START
ENDING AT #NAME-END
*
IF LEAVE-DUE >= 20 THEN
PERFORM MARK-SPECIAL-EMPLOYEES
ELSE
RESET #MARK
END-IF
*
DISPLAY NAME 3X DEPT 3X LEAVE-DUE 3X '>=20' #MARK
*
END-READ
*
IF *COUNTER (RD1.) = 0 THEN
REINPUT 'No employees meet your criteria.'
END-IF
*
END-REPEAT
*
DEFINE SUBROUTINE MARK-SPECIAL-EMPLOYEES
MOVE '*' TO #MARK
END-SUBROUTINE
*
END

The examples discussed above demonstrates the fundamental aspects of Structured Programming with regard to Mainframe Database Programming. There are several more types of Program Control Structures and Program Modularization Strategies that are available in NATURAL language programming.

To be continued in the next part.

--

Mohamad Mahmood
Mohamad Mahmood

Written by Mohamad Mahmood

Programming (Mobile, Web, Database and Machine Learning). Learned life lessons at the Center for Artificial Intelligence Technology (CAIT), FTSM, UKM, Malaysia.

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development