RPGIV @ Work

A unique site for RPG and System i Lovers


Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

Chain op-Code New features Print E-mail
User Rating: / 5
Written by Chamara Withanachchi   
Chain op-Code New features

With V5R1

The RPG enhancements in V5R1 blew us all away. IBM added a new way to code, comment, and end a line, with free-formed calculations. Also added to the language were new op codes, new functions, new data-types, and much more. Along with all of this, the CHAIN operation was slightly modified. This modification allows you to use the CHAIN operation in free-formed calculations. The format is as follows:

        CHAIN  Key   File;
	If  %Found;
	    ExSR Somthing;

With V5R2

What CHAIN missed out on in V5R1 it more than made up for with the release of V5R2. For starters, if you are accessing an externally described file, in addition to a KLIST, as your search argument, you can now list a series of values. This is only allowed in free-formed syntax and would be coded as follows:

       CHAIN ('12345' : '01')  File;
       If  %Found;
         ExSR Something;

This tells the CHAIN operation to use the composite key made up of 12345 and 01 to access the file. You can also use field names as values:

       CHAIN (FieldA : FieldB)  File;
       If  %Found;
         ExSR Something;

Where this enhancement really takes off is in its ability to handle expressions. Check this out:

      CHAIN (%SubSt(CusNum : 1 : 5) : CompanyNum + StoreNum) File;
      If  %Found;
         ExSR Something; 

The composite key in the previous example is made up of the first five characters of the CusNum field, along with the combination, or concatenation, of the CompanyNum field and the StoreNum field. Your expressions can be as complex and long as you need them to be. In this case, I am resolving two key fields. If your file has more, you will have more values or expressions, separated by colons (:).

Another enhancement to the CHAIN operation in V5R2 is the ability to place the data from a successful CHAIN into a data structure. You have had this ability for program, or internally described, files for some time, but as of V5R2 you can do this with externally described files. Here is how you code it:

    D FileDs       ds                  likerec(FileRec)
      // Read the record directly into the data structure
      CHAIN (FieldA : FieldB)  File FileDs;

      // Use the data structure fields
      If  %Found
         DsplyFname = FileDs.Fname;
         DsplyLname = FileDs.Lname;

This enhancement is a little more involved. At the end of the CHAIN operation, you can see the data structure name, FileDs, which was defined in the D-specs. This allows you to take the contents retrieved with a successful CHAIN and place them in that data structure. This enhancement works for both free-formed (as shown above) and traditional C-specs. Notice the D-spec in the above code. I added this to show you that in order to use this enhancement, you have to define the data structure properly. You can do this with the new (V5R2) LIKEREC keyword or the modified EXTNAME keyword. The LIKEREC keyword allows you to define a data structure, data structure subfield, prototyped return value, or prototyped parameter like a record, based on an external record format (in this case, the record format for File). The LIKEREC keyword has two parameters. The first parameter is the record format name, and it must be a record format from a file defined in your file specification (more than likely, the file you will CHAIN to). The second parameter tells which fields you want to include in the data structure. The options are as follows:

1. *ALL - All fields in the external record are extracted.

2. *INPUT - All input-capable fields are extracted. (This is the default.)

3. *OUTPUT - All output-capable fields are extracted.

4. *KEY - The key fields are extracted in the order that the keys are defined on the K specification in the DDS.

However, to define the data structure to be used in a CHAIN operation, you can only use the *INPUT option in the LIKEREC keyword. Data structures defined with LIKEREC are qualified data structures, so you can use them as I did in my example with the data structure.field name format. An enhancement was also made to EXTNAME keyword to also include which records you want to include in the data structure, so you can use that keyword, too. The same restriction holds true, that you have to use the default value of *INPUT.

So when would you use something other than *INPUT? That brings me to the last enhancement to the CHAIN operation. In addition to a KLIST entry and a list of values, you can use the new %KDS built-in function to reference search arguments in a data structure. The following block of code shows how this technique would be used:

     FOrderFile  if   e           k disk    rename(ORDR:OrdRec) 
     D OrdRecKeys     ds                  likerec(OrdRec : *key)

        // OrdRecKeys is a qualified data structure
        OrdRecKeys.OrderNum = wOrderNum;
        OrdRecKeys.OrderLine = wOrderLine;
        // the *KEY data structure is used as the search argument for CHAIN
        chain %kds(OrdRecKeys) OrderRec;

As you can see, the file name is OrderFile and its record format is OrdRec. I used the RENAME keyword so you could see the record format name. Then I define a data structure based on the OrdRec record format, except that this time I used the *key parameter, which dictates that only the key fields are to be defined in the data structure. In my example, assume OrdFile contains two keys, OrderNum and OrderLine. The OrdRecKeys data structure will contain subfields to match the keys, and only the keys, in the same order that they exist in the physical file. This is a qualified data structure, so you can populate the data structure appropriately and use that data structure with the %KDS built-in function to CHAIN to the file or record format. Pretty cool, right?

<Previous   Next>