RPG IV - Chapter 7

Download Report

Transcript RPG IV - Chapter 7

RPG IV
File Access and Record Manipulation
Chapter 7
Objectives:
Define sequential and random file
access
Discuss file maintenance
Define record locking
READ(Read a Record)
Externally described files, factor 2 can
actually be a record-format name
Unless you have a specific reason for
reading a record format, you are better off
using a file name as factor 2
You can code the %EOF bif to signal endof-file
You can optionally code an (E) operation
code extender, then use the %ERROR bif
SETLL(Set Lower Limit):
Allows you to begin sequential
processing at a record other than the
first on in the file
SETLL positions a file at the first record
whose key is greater than or equal to the
value specified in factor 1
%FOUND is turned on if there is a record
in the file that has a key equal to or
greater than the value in Factor 1
SETLL cont.
If you code an (E) operation code
extender, %ERROR is turned on if
some system error occurs upon
execution
%EQUAL comes on if a record is found
in the file whose key exactly matches
the value of factor 1
Does not actually retrieve a record; it
simply positions the file
SETLL cont.
To reposition the file to the beginning,
use figurative *LOVAL as factor 1
Can be used to determine whether or
not a record exists without actually
reading it
SETGT(Set Greater Than):
This operation positions the file to a
record whose key value is greater than
the value in factor 1
%FOUND is turned on if there is a
record in the file that has a key equal or
greater than the value in Factor 1
If you code an (E) operation code
extender, %ERROR is turned on if some
system error occurs upon execution
SETGT cont.
%EQUAL does not apply to SETGT
To reposition the file to the end, use
figurative *HIVAL as factor 1
READE(Read Equal Key):
The READE sequentially reads the next
record in a full procedural file if the key
of that record matches the value in
factor 1
%EOF is turned on if the record keys
do not match, or the file is at end-of-file
%EQUAL function is turned on if the
record’s key match
READE(Read Equal Key):
If you code an (E) operation code
extender, %ERROR is turned on if
some system error occurs upon
execution
The operation is used to process sets
of records with duplicate keys
READP(Read Prior Record) &
READPE(Read Prior Equal)
READP and READPE operation are
sequential reading operations that have
their parallels in READ and READE,
respectively
These operations read “backward” thru
the file, from the end to the beginning
For the READP, if the %EOF is turned on,
it indicates that you are at the beginning
of the file
READP & READPE cont.
For the READPE the EOF%comes on
when the key of the prior sequential
record does not match the factor 1
value for READPE
You must first position the file with
some input operation before using
READP or READPE
You can also use a READPE with a
blank factor 1
CHAIN(Random Retrieval from
a File):
The literal or data item name of factor 1
contains the key value of the record to
be randomly read
The %FOUND function is turned on if
the random read is successful
CHAIN operation can be used to
position the file in a manner similar to
the SETLL
KLIST(Define Composite Key),
KFLD(Define Parts of a Key)
KLIST operation allows you to define a
field for accessing records based on a
composite key
Factor 1 specifies the name you wish
to give the KLIST
At least one KFLD operation must
immediately follow a KLIST operation
Each KFLD entry declares a field that is
to participate in the concatenation
KLIST & KFLD cont
The field for the KFLD is entered in the
result field
The order in which the KFLDs are
listed determines the order in which
they are concatenated to form the
KLIST
KLIST can be used to access a file on a
partial key list, provided the portion
you want is the major, or high-order
OUTPUT FILES
Code an ‘O’ for output in the type
position 17
Code DISK in the device specification
in positions 36-42
If your are adding records to a file that
already contains records, enter an ‘A’
in position 20
If the file is keyed, code a ‘K’ in
position 34
OUTPUT FILES cont
If the file is externally described, code
an ‘E’ in position 22
EXCEPT
Similar to printing a line except the
data is written to a database file
The O specs will need to contain the
record name and the exception name
List specific fields you want to add or
*ALL
To append records to an existing file,
use the ADD keyword in positions 1820
WRITE(Write a Record)
Use a WRITE operation to add records to
a file
The WRITE operation requires a recordformat name, rather that a file name
You can include the (E) extender and the
%ERROR function with this operation to
signal an error
DELETE(Delete Record)
The DELETE operation deletes a single
record from the file specified in factor 2
If you leave factor 1 blank, the system
deletes the record most recently read
If duplicate records based on the factor
1 value exist in the file, the system
deletes only the first record
DELETE cont
If you code an entry in Factor 1, you
should use the NOT %FOUND function
to detect instances wherein the record
to be deleted is not found on the file
The DELETE operation logically
deletes records from a file, rather than
physically removing them
UPDATE FILES
Code an ‘U’ for update in the type
position 17
Code DISK in the device specification
in positions 36-42
If you are adding records to a file that
already contains records, enter an ‘A’
in position 20
If file is keyed code a ‘K’ in position 34
UPDATE FILES cont
Omitting the ‘K’ results in record
retrieval based on arrival sequence
If the file is externally described, code
an ‘E’ in position 22
An update file supports both input and
output operations
UPDATE(Modify Existing
Record)
The UPDATE operation modifies the
record most recently read
You can use this operation only with
files defined for update
This operation does not use factor 1;
factor 2 must contain a record-format
name, if your file is externally
described
UPDATE cont
Your program must have successfully
completed a sequential or random
read, retrieving that record before it
executes an UPDATE
UPDATE causes the current program
values of all the record’s fields to be
rewritten to the file
You cannot issue multiple UPDATEs for
a single read operation
UPDATE cont
If your program logic has resulted in
changes in field values that you do not
wish to be updated, you can use
EXCEPT to designate which fields are
to be rewritten
FILE and RECORD LOCKING
Phantom updates: in a multiuser
system when two users access the
same file and update the same record,
one of the user’s changes might get
lost
Two approaches you can use to deal
with this type of problem, are file
locking and record locking
FILE and RECORD LOCKING
cont
If your program designates a file as an
update file, RPG IV automatically
places a lock on a record when it is
read
Updating that record or reading
another record releases the record
from its locked state
FILE and RECORD LOCKING
cont
When a record is locked, other
application programs can only access
that record if they have defined the file
as an input file
Operation extender (N) updates without
locking the record
You can also use UNLOCK, OPEN, and
CLOSE to control locking
I/O Errors
Older RPG programs may use an
indicator in the Lo position on I/O
operations to trap errors
Good programming style dictates the
use of the (E) extender with %ERROR
Points to Remember
READ, READE, READP, and READPE
are input operations used to access
records sequentially from a full
procedural file whose type is declared
as input or update
SETLL and SETGT can be used to
position the file prior to a sequential
read operation
Points to Remember (cont)
CHAIN randomly retrieves a record and
also positions the file for subsequent
sequential reading
The KLIST and KFLD operations allow you
to position the file or retrieve a record
based on a composite key
By using a partial KLIST, you can initiate
access to sets of records that share a
common value on the first field(s) of a
composite key
Points to Remember (cont)
You can use WRITE or EXCEPT to put
records into an output file or an update
file
Operations UPDATE and DELETE are
specific to update files
You cannot UPDATE a record without
having first read it
Points to Remember (cont)
The AS/400 includes built-in record
locking to prevent the problem of
phantom updates
Techniques (including use of the
UNLOCK operation) exist to minimize
record locking, but they should not be
used if their implementation might
cause lost updates to occur