File Access and Record Manipulation
File Access and Record Manipulation
Chapter 7
Objectives
Define sequential and random file access Discuss file maintenance Define record locking
%EOF function signals end-of-file %ERROR function signals file I/O error
Requires (E) extender
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE READ(E) CustMast; SELECT; WHEN %ERROR; // Error processing goes here WHEN %EOF(CustMast); // End-of-file processing goes here OTHER; // Record processing goes here ENDSL; /END-FREE
Programming in RPG IV Third Edition 3
%EQUAL function is turned on if the record keys match %ERROR signals file I/O error
Requires (E) extender
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE SETLL InDate Orders; IF %EQUAL; READE InDate Orders; DOW NOT %EOF(Orders); EXCEPT OrderLine; READE InDate Orders; ENDDO; ELSE; EXCEPT NoOrders; ENDIF; /END-FREE
Programming in RPG IV Third Edition 10
%EOF function signals beginning of file You can also use READPE with Factor 1 blank
Position the file first with an input operation
Programming in RPG IV Third Edition 11
At least one KFLD (Define parts of a key) operation immediately follows KLIST
Declares a field that is part of the key
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq.... C StdSemCrs KLIST C KFLD Student C KFLD Smster C KFLD Course ... /FREE CHAIN StdSemCrs StudGrades; /END-FREE
Programming in RPG IV Third Edition 13
Output Files
File Specification
O (output) in position 17 If you are adding records to a file that already contains records, enter an A in position 20 If the file is externally described, code an E in position 22 If the file is keyed, code a K in position 34 Code DISK as the device (36-42)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 FFilename++IPEASFRlen+LKlen+AIDevice+.Keywords+++++++++++++++++++++++++++++ FCustMast O E K DISK
Programming in RPG IV Third Edition 15
To append records to an existing file, use the ADD keyword in positions 18-20
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 OFilename++DF..N01N02N03Excnam++++B++A++Sb+Sa+............................. O..............N01N02N03Field+++++++++YB.End++PConstant/editword/DTformat++ OCustRecordEADD Record O *ALL
Programming in RPG IV Third Edition 16
If Factor 1 is blank, the most recently read record is deleted If duplicate records based on the Factor 1 value exist in the file, the system deletes only the first record
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE CustNbr = '100'; DELETE(E) CustNbr CustMaster; IF %ERROR(CustMaster); //Error processing goes here ENDIF; /END-FREE
Programming in RPG IV Third Edition 18
Update Files
Support both input and output operations File Specifications
U (update) in position 17 If you are adding records to a file that already contains records, enter A in position 20 Code E in position 22 for externally described file Code K in position 34 for keyed file
Otherwise arrival sequence is assumed
Does not use Factor 1 Factor 2 must contain a record format name
If file is externally described
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE CHAIN CustNbr CustMaster; IF %FOUND(CustMaster); BalanceDue = BalanceDue + InvoiceAmt; UPDATE CustRecord; ENDIF; /END-FREE
Programming in RPG IV Third Edition 20
21
Record Locking
If your program designates a file as an update file, RPG IV automatically places a lock on a record when it is read When a record is locked, other application programs can only access that record if they have defined the file as an input file Updating that record or reading another record releases the record from its locked state
23
I/O Errors
Use the (E) extender to trap file I/O errors
Older RPG programs may use an indicator in positions 72-73 (Lo) for I/O operations to trap errors
Be sure to check the status of the %ERROR function after an I/O operation
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE UPDATE(E) CustRecord; IF %ERROR; EXSR IOError; ENDIF; /END-FREE
Programming in RPG IV Third Edition 25
Points to Remember
READ, READE, READP, and READPE are input operations used to access records sequentially
Used with full procedural files Used with input or update files
SETLL and SETGT position the file prior to a sequential read operation
26
Points to Remember
CHAIN randomly retrieves a record
Also positions the file for subsequent sequential reading
The KLIST and KFLD operations define a composite key and the fields that comprise the key A partial KLIST initiates access to sets of records that share a common value on the first field(s) of a composite key
Programming in RPG IV Third Edition 27
Points to Remember
WRITE or EXCEPT put records into an output file or an update file UPDATE and DELETE are specific to update files You cannot UPDATE a record without having first read it
28
Points to Remember
OS/400 includes built in record locking to prevent the problem of phantom updates Techniques exist to minimize record locking, but they should not be used if they might cause phantom updates
For example, UNLOCK
The (E) extender and the %ERROR function provide error checking capabilities for file operations
Programming in RPG IV Third Edition 29