As we mentioned in our recent blog post, “Open Access—Oh the Thinks You Can Think!” , we’ve been thinking about different ways we can use RPG Open Access (OA). One approach suggested by IBM’s Dan Cruikshank was in the area of processing lists of data returned by APIs. That thought inspired this month’s column.
Despite the list API inspiration, we decided not to build the example around that but rather provide a more generic list-handling example. There are several reasons for this, one being that we didn’t want you to ignore this example simply because you had no interest in the API that we chose to use. Lists can come from many sources, including calls to Web services. In our example, we have a simple array. The data in an element of the array plus the element position in the array make up the record content for each access (i.e., READ or CHAIN). The array data itself also serves as the key of the “file.”
By using an example based on a simple array, we can focus our attention on what we need to do to in the handler to enable it to respond appropriately to the RPG op-codes we typically use in processing lists—namely CHAIN, READ and SETLL. For some lists, you might also need to implement READE or even other flavors of SETxx and READxx, but hopefully should you need such functionality, the examples provided here will make it clear what you need to do.
If this is your first introduction to OA, you should read one or two of our earlier articles first as this piece is going to assume that you’re familiar with the basics. A good starting point would be “RPG Open Access Revisited” and “Implementing Open Access With RPG.” The first of these discusses the basic theory behind OA; the second introduces a buffer handler that utilizes a Web service. Since the handler used in this article also uses the buffer approach, that one would be a good introduction for you.
Time to Get Started
First, let’s look at the test programs we used to exercise the handler. The first uses CHAIN, displays the result and then issues a single READ to show the following “record.” The second uses SETLL to position to a key and then READs to the end of the file displaying all the “records” it finds.
Both programs use the file ArrayFile, which simply consists of a five-digit sequence number followed by a four-character key. The DDS for the file is shown here. Hopefully at this point in your OA education, it’s clear to you that no data will ever be placed in this file. It simply forms the contract between the user program and the handler for the format of data, and the key information that they will use.
A R ARRAYREC
A SEQNO 5P 0
A DATA 4A
A K DATA
Looking now at the first test program, the F-spec at (A) defines a perfectly normal DISK file with, of course, the minor exception that it associates the file with the handler program. At (B) we CHAIN to the file and then at (C) check the result, displaying an appropriate message. This is followed at (D) by a simple READ to retrieve the next record and report its status.
// This program demonstrates the CHAIN and READ capabilities of
// the OA list handler
(A) FArrayFile IF E K DISK Handler('LISTHANDLR')
D key s Like(Data)
D exit c 'exit'
/Free
DoU key = exit;
Dsply ('Enter record key: ("exit" to end)') ' ' key;
If key = exit;
Leave;
EndIf;
(B) Chain key ArrayFile; // Chain using key entered
// Report success/failure
(C) If %Found(ArrayFile);
Dsply ('Key ' + key + ' found at sequence ' + %Char(seqno));
Else;
Dsply ('Unable to locate ' + key);
EndIf;
// Read and display the next record regardless of the CHAIN's success
(D) Read ArrayFile;
If %Eof(ArrayFile);
Dsply ('EOF reached');
Else;
Dsply ('Key ' + data + ' found at position ' + %Char(seqno));
EndIf;
EndDo;
*InLr = *On;
What we’d like you to notice about this program is how normal it is. Other than the HANDLER keyword, there’s nary a hint that we’re doing anything other than reading a simple DISK file—and that’s just as it should be. We won’t waste space describing the second test program but take it from us that it too is a perfectly normal SETLL/READ program just like hundreds you’ve probably written in the past. If you want to study the source, you’ll find both test programs and the handler on our website.
Designing the Handler
For this particular handler, we used our standard OA Handler template as the base. It’s amazing how much quicker it is to write new handlers now that we have the base code in place.
As we stated earlier, we decided that we’d only handle the basic CHAIN, READ and SETLL operations in this initial version. We should also note that the *START and *END variants of SETLL are not supported. Later, as we develop more of these list handlers, we will undoubtedly add support for additional op-codes and the SETLL variants. One other feature that would be simple to add is a feedback to RPG of the current RRN.
As far as possible, we attempted to mimic all of the expected behaviors of the op-codes we were using. For example, a READ following a successful SETLL will always return the actual key requested on the SETLL if one was found or the next higher key if an exact match was not found. There are probably behaviors under specific conditions that we’ve not mirrored. If you discover any omissions, please let us know.
Before we look at the handler logic for each of the supported operation codes, let’s take a quick look at the data that is going to represent our list. Here are the definitions we used:
// This is the list that this demo handler will process - it could be
// anything and it could be anywhere. For example a web service
// might return a list of orders for a customer. It could be a list
// of spool files from a spool API. etc. etc.
(E) D listKeys DS
D keyData 60a Inz('A001+
D B002+
D B099+
D B199+
D C200+
D C399+
D D001+
D E002+
D F099+
D G199+
D J200+
D J399+
D P001+
D R399+
D T001')
D keyArray 4a Dim(15) Overlay(keyData) Ascend
As you can see, there are 15 elements currently in the array. If the array were larger, but not completely filled, then in subsequent logic we’d simply use the active number of elements together with %SubArr and %Lookupxx to restrict sorts and searches to the active portion. We decided not to complicate this example by adding that logic.
As is typical of all our OA handlers, the main logic of the handler program simply finds out what operation (i.e., Open, Chain, Read, etc.) was requested and calls the appropriate procedure to implement that operation.
Select;
When (info.rpgOperation = QrnOperation_SETLL);
doSETLL();
When (info.rpgOperation = QrnOperation_CHAIN);
doCHAIN();
When (info.rpgOperation = QrnOperation_READ);
doREAD();
When (info.rpgOperation = QrnOperation_OPEN);
OpenFile();
When (info.rpgOperation = QrnOperation_CLOSE);
CloseFile();
Other; // We don't know how to do this operation
Dsply ('Unsupported function ' + %Char(info.rpgOperation)
+ ' on queue ' + info.externalFile.name );
info.rpgStatus = errIO;
EndSl;
Open
The logic for the open begins (F), as usual, by checking to see if we’ve already allocated space for the file’s state information. If not, then we allocate the storage and store the pointer in the state information area. In this handler, the only value we’re retaining in the state area is the current record position. At (G) we sort the data in the list and then set the current “record number” to 1 (i.e., we’ve set the cursor to the beginning of the file). You may have noticed that strictly speaking the sort wasn’t necessary as our test data was already in sequence but we just wanted to highlight the point at which any setup logic would be placed. For example, if this handler were processing data from a list API, the call to the API and the logic to retrieve the data would go here.
// Basic logic for Open function
P OpenFile B
D PI
/Free
// If no state info area yet allocate now and initialize
(F) If info.stateInfo = *null;
info.stateInfo = %Alloc( %Size(stateInfo));
Clear stateInfo;
EndIf;
// Here is where you would load list data in from an external source
// We're just going to sort the data already in the array
(G) SortA keyArray;
(H) stateInfo.currentPos = 1; // Effectively set to beginning of "file"
Return;
/End-Free
P OpenFile E
SETLL
The processing for SETLL begins with performing a %LOOKUPGE operation on the array (I) and setting the posn variable. If you think about it, this version of %LOOKUP is a perfect match for a SETLL.
We store the array position in the local variable posn as we only want to actually change the stored position if the lookup was successful.
P doSETLL B
D PI
d posn s 5i 0
/Free
// Lookup given key and set posn.
(I) posn = %LookUpGE( key.data: keyArray );
// If a record was found set cursor position and relevant RPG flags
(J) If ( posn > 0 );
stateInfo.currentPos = posn;
info.found = *On;
info.eof = *Off;
(K) info.equal = ( key.data = keyArray(posn) ); // Set %Equal on match
EndIf;
If a match was found by the lookup, posn will be non zero (J). We can then proceed to set the %Found flag and set off the %EOF flag just in case RPG currently thinks we are at EOF.
At (K) we compare the key value in the array position indicated by the lookup operation with the key we used on the SETLL and set the %Equal flag based on the true/false status of that comparison.
Note that if the lookup didn’t find a match, then the current cursor position and EOF status are left unchanged. RPG will have cleared the %Found and %Equal flags prior to calling the handler so we do not need to clear those, only set them if needed.
If you needed to implement SETGT instead of SETLL the only change required to this logic would be the removal of the logic to set the %Equal flag.
CHAIN
The primary difference between the CHAIN and SETLL logic is that for CHAIN we use a straight %LOOKUP operation against the array as we are only concerned with a found/not found condition. In addition, when we find a match, we need to copy the data from the list to the corresponding variables in the input buffer (M). We also need to set the current position to the item following the one we just retrieved. This ensures that a subsequent READ will retrieve the correct “record,” i.e. the one following the one we CHAINed to.
If we didn’t find a match, then we set the %Found flag off (N). Strictly speaking, this isn’t necessary as the RPG runtime will have cleared it before calling us, but somehow it just “feels” better to make that status obvious. We’re funny that way.
P doCHAIN B
D PI
d posn s 5i 0
/Free
// Lookup given key and return appropriate data
(L) posn = %LookUp( key.data: keyArray );
If posn > 0;
info.found = *On; // Set on %Found
info.eof = *Off; // and clear %EOF
stateInfo.currentPos = posn;
(M) inputBuffer.seqNo = stateInfo.currentPos;
inputBuffer.data = keyArray(stateInfo.currentPos);
stateInfo.currentPos = posn + 1; // Set to next position
Else;
(N) info.found = *Off; // Set off %Found
EndIf;
/End-Free
READ
The READ logic is very similar to CHAIN, except that instead of performing a lookup for the required key, we simply test to make sure that we haven’t yet reached the end of the list (O). We then copy the list data to the input buffer as we did for the CHAIN. The only other thing we need to do is to set the new cursor position by incrementing the current position.
If the test at (O) indicates that we have read to the end of the list then we set the %EOF flag (P) before returning control to RPG.
// Read routine. Only READ supported currently
P doREAD B
D PI
/Free
// Obtain next entry from array and return data or set eof etc.
(O) If stateInfo.currentPos <= %elem(keyArray);
// Return data at current position
inputBuffer.seqNo = stateInfo.currentPos;
inputBuffer.data = keyArray(stateInfo.currentPos);
// And increment to next position
stateInfo.currentPos += 1;
Else;
(P) info.eof = *On; // Set EOF if at end of list
EndIf;
/End-Free
Wrapping It Up
That is all there is to it! As we noted earlier, this is by no means a complete implementation, but all of the fundamentals are in place. We were actually quite surprised at how easy the logic was.
While it’s conceivable that one could build a generic names/values handler for this kind of list processing, we’re not convinced it would be worth the effort. It might make some sense to build a generic handler for a specific group of IBM APIs that return their data in the same basic manner. Perhaps you could use the file name to identify the API to be used, and then use the format names as the record format names. It’s certainly possible but is it worth expending the energy? We’d be interested in hearing your thoughts on the subject.