Physical files can contain only one record format, but logical files can contain many. If you need to combine the records contained in several files into a single file, whether or not they have the same record layout, you can use a multiformat logical file. This technique is especially useful when databases are converted from a System/36.
The difference between a join logical file and a multiformat logical file is that join logicals have one record format that contains the fields from more than one file, while multiformat logicals are an accumulation of different record formats. Each format is from a separate physical file.
To build a multiformat logical file, you must do the following:
Get the names of all the physical files and their record formats that you are combining.
Decide what field or fields you are going to use as key to the multiformat logical file. All multiformat logicals must be keyed.
Write the DDS and compile it using the CRTLF command to create the file.
Let's create a multiformat logical file that will contain the records from the following two physical files:
Item Master File (ITMMST) in Figure 22.12.
Item Balance File (ITMBAL) in Figure 22.13.
Figure 22.12: Multiformat logical files and DDS.
Figure 22.13: Multiformat logical files and DDS.
Here is the DDS for the multiformat logical file. It is called INVMST (Inventory Master) and is shown in Figure 22.14.
Figure 22.14: Multiformat logical files and DDS.
Remember the rules:
The record formats in the multiformat logical must have the same names as they do in the physical file from which they come. That restriction forces us to use record format names IMREC and IBREC in the multiformat logical file.
Each record format must have a reference to the physical file from which it comes, via the PFILE keyword. Each record format must have a key.
An RPG IV program can use a multiformat logical file in the same way it uses a simple logical file or a join file. The F-specs do not change at all. However, keep in mind that:
When you write a new record, you must move the field values to those fields contained in the appropriate record format, then issue the WRITE operation with the record format name in Factor 2.
To read a particular record, use a KLIST/KFLD operation group to define the key, then CHAIN to the record by specifying the record format name in Factor 2.
To read any record in the file, put the file name in Factor 2 of the READ statement. This result causes your program to read the next sequential records in the logical view, regardless of the physical file.
To update or delete a record, read it first, then issue an UPDATE or DELETE operation, referencing the record format name in Factor 2.
If you cannot use record names in the WRITE, CHAIN, UPDATE, or DELETE operations, you can use a format selector program. Format selector programs are described next.
Format selector programs let you perform operations on multiformat logical files without referencing the record format name. In essence, you are letting the format selector program determine which record format is applicable. This technique is particularly useful for databases that have been brought over from the System/36 into the i5's S/36 Environment. The format selector program can be written in any language, including CL. It must have two parameters:
A character string that contains the unformatted ("flat") record data, at least up to and including the field that distinguishes one record format from all others (the "record ID" field).
A 10-byte character string that the format selector program uses to return the name of the record format to be used.
For example, suppose you have created a multiformat logical file named LGLFILE that uses a two-byte record ID field located in positions 11 and 12 of the record. Figure 22.15 shows how you could code the format selector program in CL.
Figure 22.15: Multiformat logical files and a selector CL program.
This program, PGM1, receives the record data (up to the 12th byte, which includes the record ID field). The program could have received the entire record; however, it really makes no difference. The first CHGVAR statement extracts the record ID field into CL variable &RCD_ID.
The first IF compares that against ‘AA.’ If equal, the program assigns ‘AAREC’ to the record format name (in variable &FORMAT). This value is the name you coded in the DDS of the logical file. If the record ID field contains ‘BB,’ however, &FORMAT is assigned the value ‘BBREC.’ If neither is true, &FORMAT is blanked out, which represents an error condition.
To use this program, you must specify its qualified name in the FMTSLR parameter of the CRTLF or CHGLF command for the logical file. The format selector program then becomes, for all intents and purposes, part of the file itself.