25
Thu, Apr
0 New Articles

Modernizing File Processing with RPG IV

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
Through the myriad revisions and updates to the RPG language during its lifecycle, one piece of the puzzle has remained virtually unchanged: file processing. The expeditious and efficient processing of files has been a hallmark of the RPG language definition since its inception. But in an object-oriented world that thrives on portability, the method by which RPG has processed files has fallen short of expectation...that is, until now!

The latest V5R2 RPG IV enhancements include some keywords on the file specification that go a long way toward closing the gap on encapsulating file processing to the lowest level of programming, the subprocedure. The new enhancement stops just short of this, allowing generalization down to the module level. But it gives us hope that we'll soon be able to code independent subprocedures around file processing without globalization of the file declarations themselves.

This newfound independence is garnered from two file specification keywords that are new as of V5: EXTFILE and EXTMBR. These keywords are similar to the OVRDBF command's TOFILE and TOMBR parameters, respectively.

EXTFILE

EXTFILE accepts a case-sensitive filename as its value. The filename is case-sensitive for the purposes of providing support for filenames outside of the DB2/400 environment. It is important to note that all DB2/400 file and member names are uppercase. EXTFILE can take any of the following forms:

  • A variable, such as DB2VariableName

Example: F MyFile if e k disk EXTFILE(DB2VariableName)

  • A literal value, such as 'DB2FILENAME'

Example: F MyFile if e k disk EXTFILE('DB2FILENAME')

  • A qualified literal value, such as 'DB2LIBNAME/DB2FILENAME'

Example: F MyFile if e k disk EXTFILE('DB2LIBNAME/
F DB2FILENAME')

  • A qualified literal that uses the default library list value, such as '*LIBL/ DB2FILENAME'

Example: F MyFile if e k disk EXTFILE('*LIBL/
F DB2FILENAME')

Where DB2VariableName is a DB2/400 filename of the format LIBNAME/FILENAME, LIBNAME is either the name of the DB2/400 library or the default value *LIBL, and FILENAME is the name of the DB2/400 file that is being overridden. Therefore, the value of the variable DB2VariableName could be any one of the three values in the EXTFILE examples listed above.

EXTMBR

EXTMBR, like the TOMBR parameter of OVRDBF, accepts the following as values:

  • A variable, such as DB2Member

Example: F MyFile if e k disk EXTMBR(DB2Member)

  • A literal value enclosed in single quotes, such as 'MyMember'

Example: F MyFile if e k disk EXTMBR('MyMember')

  • The default value of *FIRST

Example: F MyFile if e k disk EXTMBR('*FIRST')

  • The literal representing all of the file members, *ALL'

Example: F MyFile if e k disk EXTMBR('*ALL')

Note that use of EXTFILE and EXTMBR are independent of one another in the same manner as the TOFILE and TOMBR parameters on OVRDBF. If you wanted to override the file MyFile2 to DB2 filename PARTS in library ACCT using all of the members, then you would code the following file specifications:

F MyFile2 if   e            k disk    USROPN
F                                                     EXTFILE('ACCT/PARTS')
F                                                     EXTMBR('*ALL')


Normally, I would code the EXTFILE and EXTMBR keywords on the same line, but I generally code the USROPN keyword on a separate line for clarity.

If any file overrides occur to the files referred to by the RPG IV program, then those overrides take effect during program execution. This might seem confusing, so let's look at a couple of examples. If the previous override to MyFile2 exists in program TXR071, then what happens when the code in Figure 1 gets executed?

   A   OVRDBF   FILE(MyFile2) TOFILE(*LIBL/PARTS)
       CALL       PGM(*LIBL/TXR071)
                         .
                         .
                              .
   B   OVRDBF  FILE(PARTS) TOFILE(ACCT/PARTS) TOMBR(*FIRST)
       CALL        PGM(*LIBL/TXR071)
                         .
                         .
                              .
   C   OVRDBF  FILE(MyFile2) TOFILE(MyFile2) TOMBR(*ALL)
       CALL        PGM(*LIBL/TXR071)

Figure 1: Use these code fragments for testing EXTFILE and EXTMBR keywords.

The lines at section A in the figure will accomplish nothing in program TXR071 because the file specification actually affects a file named ACCT/PARTS, not MyFile2. MyFile2 is a dummy name in this sample RPG IV program. The real file name is the name assigned by the EXTFILE keyword.

The code at section B will override the file. But what's this? It's overriding the file to a file of the same name as before. Ah, but the member name on the TOMBR parameter is different from that of the EXTMBR keyword. Yes, instead of using the *ALL members that is listed on the EXTMBR keyword, this code uses the TOMBR value of *FIRST and only executes against the first member of the ACCT/PARTS file.

Section C is similar to section A in that it is not truly using any file called MyFile2. Realize however, that if there were such a file on the system, then the OVRDBF would affect the program file of the same name.

Keep reminding yourself that DB2/400 filenames are in uppercase. The names MYFILE2, myfile2, and MyFile2 are the names of three different files on the EXTFILE keyword. If you use either of the last two, an error will be generated; the program will not locate a DB2/400 file with the name myfile2 or MyFile2. Especially keep this in mind for those programs in which end- users may be keying in the filename. Turn on the "uppercase only" flag in your DDS specifications for the input field!

This is a style issue or perhaps a shop standard issue, but I recommend using the EXTFILE/EXTMBR keywords in conjunction with the USROPN keyword. User-controlled files are not used as often as they should be. The time has come to get friendly with this keyword if you're not already. This also allows a generalization of the EXTFILE/EXTMBR keywords to the module level. At this point, an explanation of what I mean by "generalization" is in order.

In an object-oriented world, it is preferable to be able to reference files within subprocedures related to those files. For example, wouldn't it be nice to be able to load an array with numeric data from a variety of accounting files? Most of the accounting fields would have a similar size (or at least a maximum that you'd be aware of). Wouldn't it be great to have a subprocedure where you pass in a filename--perhaps a field name or field position where the assigned field name could be looked up in a table--and have an array of data values returned for that file? Consider, for example, a year-end summary where data for the previous 12 months could be gathered and returned. Data in the subprocedure could open the file, read the monthly data, summarize the data, place the values in buckets for each month, close the file, and return the array to the calling program. This could be done via a call such as SumMonths = Summary('GLAcct':'GLPER'), where the subprocedure Summary and the variable SumMonths are both arrays, 'GlAcct' is the name of the file, and 'GLPER' is the name for the key used to gather the data. In the subprocedure, two files are actually opened: 'GLAcct' for reasons previously stated and another file, let's call it CompanyTbl, that associates the keys 'GLAcct'/'GLPER' with summing a particular series of fields. In fact, you could even code this directly in the subprocedure without the support of a CompanyTbl if you wanted to.

Maybe one day, this will become a reality. For now, it's just a dream. But what we do have is generalization down to a module level. In other words, we currently can't place a file specification into a subprocedure, but we can place it in a module. As such, we can have a module (*MODULE) or service program (*SRVPGM) that supports the passing of filenames and member names as parameters while executing in a fashion similar to the generalized subprocedure discussed above. The first two parameters are part of the procedure interface for an EXTPGM that passes those parameters. A third parameter is required to pass back the SumMonths array values unless a global variable or IMPORT/EXPORT is used.

Speaking of IMPORT/EXPORT, the variables used for the filenames and member names can be IMPORTed and EXPORTed from modules. However, I would avoid this because it has a tendency to destroy the generality of the code. If you know what your filename is, why not use it in the main RPG program's file specifications? Chances are, if you are using it for IMPORT/EXPORT, you need it for more than just your routines, so why not code it where it will be global? Still, you should be aware that this option is available to you.

Also, the variable can be set in the initialization subroutine (*INZSR) or in the D-spec on the INZ keyword. The same arguments I have about IMPORT/EXPORT lead me to frown on these options as well.

Generalize your programs by taking advantage of the usefulness of these keywords with files that have similar file structures. If you specifically know your filename, use a quoted literal on the EXTFILE/EXTMBR parameters along with USROPN. This may eliminate the need for a calling CL program. Use the *INZSR to initialize the field only if the value is passed as a parameter to the main program on its procedure interface. Give your particular situation some serious thought before coding these keywords. Many of our shops have multiple files that have either the same field names or field names in which one letter differs from file to file. This most often occurs because more than one of these files is needed in several programs. These are highly likely candidates to code EXTFILE routines around.

RPG has come a long way as a language, surviving numerous "it's dead" challenges. RPG IV is a much different creature than any prior incarnation of the language. Though the file specification hasn't changed much, it has changed for the better. Now, it's up to the programmers to start coding for the future with this specification. Personally, I think my dream for a general availability of file opens down to the subprocedure level will become a reality. But for now, I love the capabilities that I have in hand. I hope this article has been a revelation for some and an encouragement to all!

Vincent B. Goldsby has been an iSeries/400 programmer/analyst since its inception. Currently, he is a freelance speaker and instructor on ILE, RPG IV, and introductory Java. His most requested course is a four-day comprehensive session entitled "Cameras, Lights, RPG IV!!!" He can be reached for comments and questions at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

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: