One of the shortcomings of RPG IV is the inability to access the input buffer of a database file between *MODULE objects. Of course, since modules are created from source code, this means there is no mechanism in RPG IV to export and import database files.
To solve this problem, IBM would need to add EXPORT and IMPORT keywords to the File description specification. Then, in MODULE-A, you would declare and then EXPORT the file, whereas in MODULE-B, you would declare and then IMPORT the file.
But first, let's review a little information about exporting and importing and what it means to us as RPG IV programmers.
Occasionally, it's necessary to make a field value available to other modules in a program. This may be required for a number of reasons. For example, when a field is used by many procedures, it's more practical to make it available via EXPORT than to pass it to a number of procedures as parameters.
What about when a file is shared between modules? What if I want to read a record from a file, call a procedure to manipulate the data in that file, and then return to the original main procedure?
If I declare the file in MODULE-A and MODULE-B and then do a read to the file in MODULE-A, when I call a procedure in MODULE-B, the input buffer in MODULE-A contains the record I just read, but the input buffer in MODULE-B is unchanged.
So if I want the current data from the input buffer in MODULE-A to be represented in MODULE-B, I would need to pass the key to the file to every procedure in MODULE-B and then access the database record using READ or CHAIN.
This can cause its own set of issues, such as a record lock conditions (the "deadly embrace") and latency problems with the data itself.
To resolve this problem, I came up with a workaround that seems to work in most cases. Since we are allowed to export only fields and data structures, we need to be able to move the data from the input buffer into a data structure and then EXPORT that data structure.
An easy way to do this is by declaring a data structure based on the database file in question. Use the EXTNAME keyword on the data structure declaration to ensure that the data structure is the same format as the database file. The EXTNAME keyword allows you to declare an externally described data structure based on a specific database file name, as shown below.
D myDS E DS EXTNAME( myFile )
The E in column 22 and the EXTNAME keyword indicate that the data structure is externally described. This causes all the subfields in the data structure to be the same name, data type, and length as the fields in the external file.
Since the field names are the same as the external file, if we use the same name on the EXTNAME keyword as we do on the File specification, we will get a duplicate of the input buffer for that file in the form of a data structure.
Once the fields are part of a data structure, we can use the EXPORT keyword to make it available to other modules in this program:
D myDS E DS EXTNAME( myFile )
D EXPORT
Since this data structure is exported, we can now import it into other modules. To do this, we declare the data structure exactly the way it is declared in the examples above; however, we use the IMPORT keyword instead of EXPORT, as shown below:
D myDS E DS EXTNAME( myFile )
D IMPORT
To use this in a program, the program would be made up of multiple modules. The Entry module would contain the file declaration and an externally described file based on that same file. You may also want to consider using the PREFIX keyword on both the File and Data Structure declarations to avoid field name collisions in the secondary modules.
Figure 1 illustrates the source member for an Entry module of a program. The file CUSTSALES (line 5) is externally described and uses the PREFIX(CS_) keyword. This renames all the fields of that file to CS_xxxxxx, where xxxxxx is the original field name.
On line 8, the SALES data structure is declared as an externally described data structure based on the CUSTSALES file. I've also included the PREFIX(CS_) keyword to force all the subfield names to be renamed in that data structure.
The subfields of the SALES data structure will have the same names and attributes as those in the CUSTSALES file. Therefore, when a record is read from CUSTSALES, the fields in the SALES data structure will be populated with that information.
|
Figure 1: This Entry module exports the database field names.
To import that SALES data structure and thus the database fields into a secondary module, the data structure SALES needs to be declared and imported, like so:
D PREFIX(CS_)
D IMPORT
We use the PREFIX(CS_) keyword in this example not because it is required, but to be consistent. The full source for a secondary module is listed in Figure 2.
|
Figure 2: The secondary module imports the SALES data structure.
You can use the two source members from Figures 1 and 2 to test this technique. Compile those source members using the CRTRPGMOD command (PDM option 15). Then, use the CRTPGM command as illustrated below to create the program object.
To see if the program is working, start debug mode and set a breakpoint at line 15 in the EXFILE source member (Figure 1) or at line 13 or 14 in the EXPROC source member (Figure 2).
To compile the EXFILE module, you will need the CUSTSALES database file. Its DDS is listed below:
|
Of course, you will need to add some data to the file using DFU or DBU/400.
Exporting an externally described data structure can help provide an easy way to share the input buffer of a database file between separately compiled modules.
Holiday Greetings
As we close 2003 with this issue and end our second full year of Midrange Developer, I'd like to thank all of our readers, who keep this job interesting and exciting. I'd also like to thank our advertisers for being so supportive this year, thereby allowing us to continue to deliver this publication to our readers free of charge. Next year, we are moving to a twice-monthly format. We will publish on the first and third Wednesdays of each month. You'll see tips from the group at RPG World and perhaps a surprise or two. In the meantime, have a safe and happy holiday season and a very happy new year.
Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.
LATEST COMMENTS
MC Press Online