RPG steals a trick from its older brother COBOL to make moving data just a little bit easier.
Practical programming is often about the little things. For example, one of the issues in programming multi-tiered applications is moving data between tiers. In message-based programming, you can and should reduce the number of traveling fields by using a data structure that contains only the fields you need. The problem is that you have to populate those fields with individual MOVE instructions and, with large tables, identifying every field can be tedious and error-prone (especially when the fields have short names). EVAL-CORR gets around that problem.
The Problem Is Not Unique
The issue of field (or column) names is not unique. If you use SQL, you have a choice of either getting all fields in a table using SELECT * or enumerating your fields in the SELECT statement. The former is frowned upon both because it makes your code more fragile and because it adds unnecessary overhead between the tiers, so you often see SQL statements where a large part of the statement is simply listing all the fields to be selected.
With message-based programming, it's a little bit different, but the problem still remains. Let's say I want to call a program to return order header information. While the bulk of the information might actually be in the order header record itself, chances are that many of the fields that the user needs to see will be located in other files. Examples include the customer name, accessed from a customer master, or descriptions for fields like terms codes and freight types.
Let's Set the Table
Let me show you what I mean with a couple of simple record layouts. Here are the fields we want from the order header record:
OHORDR 10A TEXT('Order Number')
OHCUST 6S 0 TEXT('Customer Number')
OHORDT L TEXT('Ordered Date')
OHDLDT L TEXT('Delivery Date')
OHTAX 9S 2 TEXT('Total Tax')
OHFRGT 7S 2 TEXT('Freight')
OHTERM 3A TEXT('Terms Code')
And the fields from the customer record:
CMCUST 6S 0 TEXT('Customer Number')
CMNAME 30A TEXT('Customer Name')
CMEMAL 80A TEXT('Email Address')
And finally, the terms code table:
TTTERM 3A TEXT('Terms Code')
TTDESC 30A TEXT('Description')
The listings above are only partial listings; the files may have lots of other fields, but these are the fields I'm interested in for this particular message. In a typical message-based system, I would then create the message definition for the message containing all these fields using an externally described data structure (which in turn is basically just a physical file).
ID 5A TEXT('Message ID')
OHORDR R REFFLD(OHORDR ORDHDR)
OHCUST R REFFLD(OHCUST ORDHDR)
OHORDT R REFFLD(OHORDT ORDHDR)
OHDLDT R REFFLD(OHDLDT ORDHDR)
OHTAX R REFFLD(OHTAX ORDHDR)
OHFRGT R REFFLD(OHFRGT ORDHDR)
OHTERM R REFFLD(OHTERM ORDHDR)
CMNAME R REFFLD(CMNAME CUSMAS)
CMEMAL R REFFLD(CMEMAL CUSMAS)
TTDESC R REFFLD(TTDESC TRMTBL)
This is the definition for message ID 00100. I've defined the fields I need from the order header record as well as the two additional files: the customer master and the terms code table. The code to retrieve this data would be pretty simple:
FORDHDR IF E K DISK
FCUSMAS IF E K DISK
FTRMTBL IF E K DISK
D MSG00100 E DS
These are the externally described files for the database and the externally described data structure for the message. Now, due to a peculiarity in RPG, the code for this could be very simple.
chain iOrderNumber ORDHDR;
chain OHCUST CUSMAS;
chain OHTERM TRMTBL;
if not %found(ORDHDR) or
not %found(ORDHDR) or
Because I gave the same names to the fields in the data structure as I did in the files and I did not qualify the MSG00100 data structure, this particular programming technique would automatically load the fields in MSG00100 from the fields in the three files. That's because RPG uses global variable names, and if it finds a field with the same name as one in an externally described file, it will move the data into that field when the file is read (provided, of course, that the field definitions are the same; if they're not compatible, RPG will complain and the program will not compile).
However, there are reasons that you might want the data structure MSG00100 to be qualified. You might want an array of MSG00100 data structures, and data structure arrays must be qualified. Or you might want two different message structures that share at least one common field; you cannot do that without the qualified keyword.
Whatever the reason, once you qualify the message data structure, you have to move the fields from the externally described files into the data structure yourself; the RPG runtime won't do it for you. If I were to code this myself, the changes would look a little like this:
D MSG00100 E DS QUALIFIED
I change the data structure to be qualified, and then I add EVAL operations after the endif for the fatal-error checking:
eval MSG00100.OHORDR = OHORDR;
eval MSG00100.OHCUST = OHCUST;
You get the idea. And even with a limited example like this one, you can see how tedious this could get. As I mentioned before, it's also very error-prone because you can make a typo and not catch it, like this:
eval MSG00123.ARDAT1 = ARDAT1;
eval MSG00123.ARDAT2 = ARDAT2;
eval MSG00123.ARDAT3 = ARDAT3;
eval MSG00123.ARDAT4 = ARDAT3;
eval MSG00123.ARDAT5 = ARDAT5;
eval MSG00123.ARDAT6 = ARDAT6;
Do you see the mistake? On the fourth line, I mistyped the right side, and now the wrong data is in the ARDAT4 field of MSG00123.This is exactly the sort of error you get when you have to type lists of dozens or even hundreds of fields.
And this is where EVAL-CORR comes to the rescue!
EVAL-CORR is the RPG equivalent to COBOL's MOVE-CORRESPONDING verb, and it moves all "corresponding" fields (that is, all fields with the same names) from one data structure to another. Two things are required in order to implement EVAL-CORR. First, you have to create data structures for your externally described files. Typically, that's not too difficult, and if I'm doing it, I use a standard naming convention:
D IOORDHDR E DS EXTNAME(ORDHDR)
D IOCUSMAS E DS EXTNAME(CUSMAS)
D IOTRMTBL E DS EXTNAME(TRMTBL)
I do this because you can't use the same name in a data structure as for a file name in an F-spec. So I prepend "IO" to identify this as an I/O data structure. At this point, I'm done and all I have to do in my code is an EVAL-CORR for each database file from which I'm extracting data:
eval-corr MSG00100 = IOORDHDR;
eval-corr MSG00100 = IOCUSMAS;
eval-corr MSG00100 = IOTRMTBL;
That's all there is to it. All fields in MSG00100 that correspond to fields from the three files will be populated. In fact, the compiler will give you a detailed listing of all the fields that are moved just so you know. It will also complain if there are no corresponding fields, which is probably a sign that your code might need a perusal.
What's beautiful about this technique is that if I needed an additional field from any of these files, I would simply have to add the field to the MSG00100 data structure and recompile; the new field would be taken care of automatically. Heck, adding a field from another file is as simple as adding the field to MSG00100 and then adding the file, the CHAIN, the data structure, and the eval-corr.
The technique is not foolproof. If you have fields from two files with the same name, you run into two different issues, depending on whether you need those fields in the message or not. In either case, you'll have to qualify the I/O data structures as well, and then your CHAIN opcodes change slightly. More importantly, though, if you need the data from two fields with the same name in the same message, you'll obviously have to rename one of them, and that drops it outside the scope of this article. It's also one of the reasons I argue against using the same field name in different files. I'm a firm believer in field name prefixes, although they look a bit silly with the longer field names of SQL tables.
But regardless of how you name your fields, I've given you a little taste of what can be done with the EVAL-CORR opcode and given you a technique that can help relieve some of the tedium of your programming. Enjoy!