29
Fri, Nov
0 New Articles

Practical RPG: Using EVAL-CORR in Business Logic Servers

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:

 

R ORDHDRR

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:

 

R CUSMASR

CMCUST 6S 0 TEXT('Customer Number')

CMNAME 30A TEXT('Customer Name')

CMEMAL 80A TEXT('Email Address')

 

And finally, the terms code table:

 

R TRMTBLR

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).

 

R MSG00100

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.

 

/free

chain iOrderNumber ORDHDR;

chain OHCUST CUSMAS;

chain OHTERM TRMTBL;

 

if not %found(ORDHDR) or

not %found(ORDHDR) or

not %found(ORDHDR);

FatalError();

endif;

 

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!

Implementing EVAL-CORR

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.

Caveats

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!

 

 

Joe Pluta

Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. He has been extending the IBM midrange since the days of the IBM System/3. Joe uses WebSphere extensively, especially as the base for PSC/400, the only product that can move your legacy systems to the Web using simple green-screen commands. He has written several books, including Developing Web 2.0 Applications with EGL for IBM i, E-Deployment: The Fastest Path to the Web, Eclipse: Step by Step, and WDSC: Step by Step. Joe performs onsite mentoring and speaks at user groups around the country. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..


MC Press books written by Joe Pluta available now on the MC Press Bookstore.

Developing Web 2.0 Applications with EGL for IBM i Developing Web 2.0 Applications with EGL for IBM i
Joe Pluta introduces you to EGL Rich UI and IBM’s Rational Developer for the IBM i platform.
List Price $39.95

Now On Sale

WDSC: Step by Step WDSC: Step by Step
Discover incredibly powerful WDSC with this easy-to-understand yet thorough introduction.
List Price $74.95

Now On Sale

Eclipse: Step by Step Eclipse: Step by Step
Quickly get up to speed and productivity using Eclipse.
List Price $59.00

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: