File access within RPG programs is consistent with each type of file. All RPG database file-operation codes function identically on physical, logical, and join-logical files. The only exception to this is when a file has been defined to the operating system as read-only. In this situation, output operations aren't allowed.
When a file is defined to a program as a keyed file (i.e., the letter K is specified in column 31 of the file specification), RPG processes the file by the file's access path. When a file is defined as a sequential file (i.e., column 31 contains a blank), RPG processes the file's records in the order in which they appear in the file.
When a file is defined as a keyed file to the RPG program, the operations that retrieve records from a file can access those records randomly through the access path. With some operations, the record's data is copied to the input (buffer) area for the file, while others simply reposition the file cursor. Table 11.10 lists the effect of the file operations on a database file when the operation is successful.
Op Code | Data Returned | Record Status | Cursor Positioning |
---|---|---|---|
CHAIN | Yes | Locked | To record |
DELETE | No | Deleted | To no record |
EXCEPT | No | Released | To same record |
FEOD | No | Released | To no record |
OPEN | No | None | Beginning of file |
READ | Yes | Locked | To record |
READE | Yes | Locked | To record |
READP | Yes | Locked | To record |
READPE | Yes | Locked | To record |
SETGT | No | Released | After record |
SETLL | No | Released | Before record |
UNLOCK | No | Released | To same record |
UPDATE | No | Released | To same record |
WRITE | No | Released | To same record |
When any operation causes the file cursor to be positioned to the record or before the record, a subsequent READ or READE operation retrieves the next record in the file. A subsequent READP or READPE retrieves the prior record in the file. Additionally, if the N (no lock) operation extender is used with any of the input operations, no record lock is applied to the retrieved record.
The READE (Read Next Record with Equal Key) and READPE (Read Prior Record with Equal Key) operations can cause record-lock contention under the following conditions:
When the operation reads a record, the record is reviewed for the equal-key condition. If the key value for the record doesn't match the value in factor 1, the record is released, and data is never copied to the input (buffer) area.
During the review of the key value for the record, the record is locked based on the conditions specified for the file on the file specification.
If the record is already locked by another application, the READE or READPE operation waits for the record to be released for a time specified by the file's description. This wait period is specified external to the RPG program.
When the wait-time expires, the RPG exception/error handling routine receives control unless resulting indicator 2 is specified. If resulting indicator 2 is specified, it is set on. To avoid the record lock, the N operation extender (NO LOCK) can be used. Specify READE(N) or READPE(N) to avoid placing the lock on the database record.
Tables 11.11 through 11.26 list the position of the file cursor after the specified operation code has been performed successfully.
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
| 5738SS1 |
84VETTE | |
*END | |
*NORECORD |
The file PARTMAST is used in these examples. It contains the key field PART (part number). Only the key field is depicted in the figures. Prior to each operation, the record containing the part number 5738SS1 has been successfully retrieved with the CHAIN operation. Tables 11.11 through 11.26 diagram what happens to the file cursor and the part number record after the specified operation has been performed.
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
| . . . |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
| . . . |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
84VETTE | |
| *END |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
| *START |
LBY17YR | |
Q385381 | |
. . . | |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| 84VETTE |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
84VETTE | |
*END | |
| *NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
| Q385381 |
5738SS1 | |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
84VETTE | |
*END | |
| *NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
| |
5738SS1 | |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |
File Cursor Position | Part (Index) Number |
---|---|
*START | |
LBY17YR | |
Q385381 | |
5738SS1 | |
| |
84VETTE | |
*END | |
*NORECORD |