Embedded QUEL Companion Guide > Embedded QUEL Companion Guide > Embedded QUEL for Fortran > Sample Applications > Linux--The Department-Employee Master/Detail Application
Was this helpful?
Linux--The Department-Employee Master/Detail Application
This application runs in a master/detail fashion, using two database tables joined on a specific column. This typical example of a department and its employees demonstrates how to process two tables as a master and a detail.
The program scans through all the departments in a database table to reduce expenses. Department information is stored in program variables. Based on certain criteria, the program updates department and employee records. The conditions for updating the data are the following:
Departments:
If a department has made less than $50,000 in sales, the department is dissolved.
Employees:
If an employee was hired since the start of 1985, the employee is terminated.
If the employee's yearly salary is more than the minimum company wage of $14,000 and the employee is not nearing retirement (over 58 years of age), the employee takes a 5% pay cut.
If the employee's department is dissolved and the employee is not terminated, the employee is moved into a state of limbo (the Toberesolved database table, described below) to be resolved by a supervisor.
This program uses two cursors in a master/detail fashion. The first cursor is for the Department table, and the second is for the Employee table. The create statements used to create the tables are shown below. The cursors retrieve all the information in their respective tables, some of which is updated. The cursor for the Employee table also retrieves an integer date interval whose value is positive if the employee was hired after January 1, 1985.
Each row that is scanned, both from the Department table and the Employee table, is recorded into the system output file. This file serves as a log of the session and as a simplified report of the updates.
Each section of code is commented for the purpose of the application and also to clarify some of the uses of the EQUEL statements. The program illustrates table creation, multi-query transactions, all cursor statements and direct updates. For purposes of brevity, error handling on data manipulation statements is simply to close down the application.
The application runs in the Linux environments.
Linux: The following two create statements describe the Employee and Department database tables:
##    create dept 
##        (name        = c1      /* Department name */
##         totsales    = money,  /* Total sales */
##         employees   = i2)     /* Number of employees */

##    create employee
##        (name        = c20,    /* Employee name */
##         age         = i1,     /* Employee age */
##         idno        = i4,     /* Unique employee id */
##         hired       = date,   /* Date of hire */
##         dept        = c10,    /* Employee department */
##         salary      = money)  /* Yearly salary */
C
C   Procedure: MAIN
    Purpose:   Main body of the application. Initialize the 
C              database, process each department, 
C              and terminate the session.
C

       program main

       print *, 'Entering application to process expenses.' 
       call InitDb()
       call PrcDpt()
       call EndDb()
       print *, 'Successful completion of application.'
       end

C
C   Procedure: InitDb
C   Purpose:   Initialize the database.
C              Start up the database, and abort if an error. 
C              Before processing employees, create the table for
C              employees who lose their department, 'toberesolved'.
C              Initiate the multi-statement transaction. 
C
##     subroutine InitDb()

##     declare

##     integer*4 errnum
##     character*256 errtxt

       external ErrEnd
       integer*4 ErrEnd

##     ingres personnel

       print *, ' Creating "To_Be_Resolved" table.'
##     create toberesolved
##          (name   = char(20),
##           age    = smallint,
##           idno   = integer,
##           hired  = date,
##           dept   = char(10),
##           salary = money)
##      inquire_ingres (errnum = errorno)
        if (errnum .NE. 0) then
##          inquire_ingres (errtxt = ERRORTEXT)
            print *, ' Fatal error on creation:'
            print *, errtxt
##          exit
            call exit(-1)
        endif

C
C    Inform Ingres runtime system about error handler.
C    All subsequent errors close down the application.
C
           call IIserr(ErrEnd)

##         begin transaction

##         end

C
C   Procedure: EndDb
C   Purpose:   Close off the multi-statement transaction and access
C              to the database after successful completion of the
C              application.
C
##         subroutine EndDb()
##         declare

##         end transaction
##         exit
##         end
C
C   Procedure: PrcDpt
C   Purpose:   Scan through all the departments, processing each
C              one. If the department has made less than 
C              $50,000 in sales, then the department is dissolved. 
C              For each department process all the employees 
C              (they may even be moved to another table). 
C              If an employee was terminated,
C              then update the department'employee counter. No
C              error checking is done for cursor updates.
C

##         subroutine PrcDpt()

##         declare

C    Corresponds to the 'dept' table
##         character*12      dptnam
##         double precision  dptsal
##         integer*2         dptemp

C                  Cursor loop control
##         integer*4         nmrows
C                  Minimum sales goal for department
##         parameter         (SALMIN = 50000.00)
C                  Number of terminated employees
##         integer*2         emptrm
C                  Department deleted indicator
           integer*2         deldpt
C                  Formatting value
           character*21      dptfmt

           emptrm = 0 
           nmrows = 0

##     range of d is dept
##     declare cursor deptcsr for
##        retrieve (d.name, d.totsales, d.employees)
##        for direct update of (name, employees)

##     open cursor deptcsr

100    continue
##          retrieve cursor deptcsr 
##              (dptnam, dptsal, dptemp)
##          inquire_equel (nmrows = ENDQUERY)

            if (nmrows .EQ. 0) then

C      Did the department reach minimum sales?
                if (dptsal .LT. SALMIN) then

##                  delete cursor deptcsr

                    deldpt = 1
                    dptfmt = ' -- DISSOLVED --'
                else
                    deldpt = 0
                    dptfmt = ' '
                endif

C      Log what we have just done

           print 11, dptnam, dptsal, dptfmt 11
           format (' Department: ', a14, ', Total Sales: ', 
                            f12.3, a)

C      Now process each employee in the department
           call PrcEmp( dptnam, deldpt, emptrm )

C      If some employees were terminated, record this fact
           if (emptrm .GT. 0 .AND. deldpt .EQ. 0) then
##              replace cursor deptcsr
##                   (employees = dptemp - emptrm)
           endif

           endif
       if  (nmrows .EQ. 0) goto 100

##     close cursor deptcsr
##     end
C
C   Procedure: PrcEmp
C   Purpose:   Scan through all the employees for a particular
C              department. Based on given conditions the employee 
C              may be terminated or    given a salary reduction.
C              1.    If an employee was hired since 1985 then the
C                    employee is terminated.
C              2.    If the employee's yearly salary is more than 
C                    the minimum   company wage of $14,000 and the
C                    employee is not close to Retirement (over 58 
C                    years of age), then the employee takes a 5% 
C                    salary reduction.
C              3.    If the employee's department is dissolved and 
C                    the employee is not terminated, then the 
C                    employee is moved into the
C                    'toberesolved' table.
C   Parameters:
C              dptnam     -  Name of current department.
C              deldpt     -  Is current department being dissolved?
C              emptrm     -  Set locally to record how many 
C                            employees were terminated for the 
C                            current department.
C

##     subroutine PrcEmp( dptnam, deldpt, emptrm )
##     character*12   dptnam
       integer*2      deldpt
C                Number of terminated employees
       integer*2      emptrm

##     declare

C    Corresponds to 'employee' table
##     character*20   empnam
##     integer*2      empage
##     integer*4      empid
##     character*25   emphir
##     real*4         emppay
##     integer*4      emp85

C             Cursor loop control
##     integer*4      nmrows
C             Minimum employee salary
##     parameter      (MINPAY = 14000.00)
C             Age of employees near to retirement
##     parameter      (NEAR65 = 58)
C             Percentage of current salary to receive
##     parameter      (SALRED = 0.95)
C             Formatting values
       character*14   title
       character*25   desc

       nmrows = 0

C    Note the use of the Ingres function to find out who was
C    hired since 1985.

##        range of e is employee
##        declare cursor empcsr for
##        retrieve (e.name, e.age, e.idno, e.hired, e.salary, res =
##        int4(interval('days', e.hired - date('01-jan-1985'))))
##               where e.dept = dptnam
##               for direct update of (name, salary)
##        open cursor empcsr

       emptrm = 0
10     continue
##     retrieve cursor empcsr (empnam, empage, empid,
##           emphir, emppay, emp85)
##     inquire_equel (nmrows = ENDQUERY)

       if (nmrows .EQ. 0) then

          if (emp85 .GT. 0) then

##            delete cursor empcsr

              title = 'Terminated:'
              desc = 'Reason: Hired since 1985.'
              emptrm = emptrm + 1

          else

C      Reduce salary if not near retirement
          if (emppay .GT. MINPAY) then
                 if (empage .LT. NEAR65) the

##                      replace cursor empcsr
##                        (salary = salary * SALRED)

                      title = 'Reduction: ' 
                      desc = 'Reason: Salary.'

C      Do not reduce salary
                 else
                      title = 'No Changes:'
                      desc = 'Reason: Retiring.'
                 endif

C      Make no changes in salary
             else
                title = 'No Changes:'
                desc = 'Reason: Salary.'
             endif
C      Was employee's department dissolved ?
             if (deldpt .NE. 0) then
##               append to toberesolved (e.all)
##                    where e.idno = empid

##               delete cursor empcsr
             endif
         endif

C      Log the employee's information
          print 12, title, empid, empnam, empage, emppay, 
      &               desc 
12          format (' ', a, ' ', i6, ', ', a, ', ', i2, ', ', f8.2,
      &                   ';', ' ' a)
       endif

       if (nmrows .EQ. 0) goto 10

##       close cursor empcsr

##     end
C
C   Procedure: ErrEnd
C   Purpose:   If an error occurs during the execution of an EQUEL
C           statement, this error handler is called. Errors are
C           printed and the current database session is terminated.
C           Any open transactions are implicitly closed.
C   Parameters:  
C           ingerr - Integer containing Ingres error number.
C
##    integer function ErrEnd(ingerr)
      integer*4 ingerr

##    declare
##    character*256 errtxt

##    inquire_ingres (errtxt = errortext)
      print *, ' Closing down because of database error:'
      print *, errtxt

##    abort
##    exit
      call exit(-1)

      ErrEnd = 0
##    end
Last modified date: 04/03/2024