Qualifying a data structure can be a fun and effective coding practice, but did you know that with OS/400 V5 you can qualify database field names just like data structures? In V5, IBM added the capability to the File specification to include a data structure name for the PREFIX keyword. This means that the file will read into the data structure automatically. Let's see how this works.
First, a traditional PREFIX keyword is used on the File specification as follows:
Of course, this causes all the fields in the CUSTMAST file to be renamed to their original field names and adds the prefix CM_ to those field names. For example, the field named CSTNBR becomes CM_CSTNBR.
In V5, IBM enhanced the PREFIX keyword so that you could specify a data structure name. This coincided with the introduction of QUALIFIED data structures, which is a good thing, because only QUALIFIED data structures may be used on the PREFIX keyword.
This enhancement does not necessarily require you to rename the fields of the database file; instead, it allows you to qualify the field names to a data structure name, with or without renaming the fields.
The new PREFIX keyword syntax is as follows:
Note the subtle difference in the parameter specified on the PREFIX keyword. It is now 'CM.' instead of the simple CM_ prefix.
When a period is used at the end of the prefix string, the compiler assumes you are going to declare a data structure with the same name as the prefix string, so it generates code to cause the database fields to be qualified to that data structure.
Since the period is not a valid character for names, the PREFIX keyword is enhanced so that you may enclose the prefix in quotes. Quoting is required for data structure qualifications and is optional for the traditional prefix operations. So as of V5, the following are equivalent statements:
FCUSTMAST UF E K DISK PREFIX('CM_')
To allow the file's field names to be qualified to a data structure requires more than just the PREFIX keyword, however. You must also specify a data structure specification. That data structure should be externally described, based on the same database file name as declared on the File specification. For example:
0002 D CM E DS ExtName(CUSTMAST)
0003 D QUALIFIED
Line 1 in the previous example declares the file named CUSTMAST as an externally described file and indicates via the PREFIX keyword that the fields in that database file are to be qualified to the 'CM.' prefix. This indicates three things: A data structure is declared elsewhere in the program, it is named CM, and it is externally described.
Line 2 declares the data structure named CM as an externally described data structure based on the CUSTMAST database file. This means all the fields of the CUSTMAST file will be used as data structure subfields for the CM data structure.
The new QUALIFIED keyword (line 3) is used to cause the CM data structure to be declared as a qualified data structure. Qualified data structures require their subfields to be connected to their parent data structure name using a period.
Since the CM data structure is qualified and the subfield names are the same as those of the CUSTMAST file, the PREFIX keyword (line 1) allows you to read a record into the CM data structure and then refer to the subfields of that data structure in order to access the data from the CUSTMAST file.
Here's an example of how to access the subfields in the CM data structure:
C MYKEY Chain CustRec
C eval cm.City = City
C eval cm.state= State
C eval cm.country = Cntry
The compiler automatically reads the data from the CUSTMAST file into the CM data structure, so no resulting field is necessary on the CHAIN opcode. In fact, when this technique is being used, the only way to access the fields of the CUSTMAST file is through the qualified name syntax.
Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online