reusing a single file description on multiple files _ rpg

7
2/5/2014 Reusing a Single File Description on Multiple Files | RPG http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 1/7 Support MC Press - Visit Our Sponsor Forums Sponsor Popular Forums POPULAR FORUMS Forums Reusing a Single File Description on Multiple Files Programming - RPG Written by Thomas Snyder Wednesday, 17 June 2009 01:00 0 Comments 0 Dynamically specify files in RPG without the use of embedded SQL. Do you have physical files with the same file format that are separated into multiple physical files--whether it be for legal reasons, query speed, or storage archiving--which requires you to keep modifying your programs when the new files are created? This article will show you how to continue creating new physical files, without the need for additional program compiling and maintenance. In my shop, we had a physical file that needed to be physically separated into different files for legal reasons. These files reused the same physical file layout for each version, and at the end of each year a new file would be created to isolate the data. There was at least one program that would perform historical reporting by accessing several of the files, and this program would need to be maintained each year to include the additional physical file that was created. This type of programming maintenance is highly undesirable, especially at year-end, when you're at your busiest! My initial solution to this problem was to write dynamic embedded SQL in the RPG program. This would have allowed me to specify the file name within the SQL statement to retrieve the data from the file. But this solution would have required a notable amount of program logic to be rewritten. Instead, I chose to use the EXTFILE and EXTMBR keywords on my file specifications to accommodate this capability. Using EXTFILE instead of embedded SQL allowed me to make minimal programming changes to the existing program. All I needed to do was change the file specifications and the existing logic that opened the files. The File Specification Keywords: EXTFILE and EXTMBR The file referred to in the file specification must exist in order for your program to compile. You could create an empty generic dummy file to represent the file you will be using throughout the years, or you may chose to use an arbitrary file from a specific year. The only problem with this would come into play when you decide sometime down the line that you want to start archiving older files off of the system. Then the file that your program is referring to would no longer

Upload: mickeychennai

Post on 28-Dec-2015

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 1/7

Support MC Press - Visit Our Sponsor

Forums Sponsor

Popular Forums

POPULAR FORUMS

Forums

Reusing a Single File Description on Multiple Files

Programming - RPG

Written by Thomas Snyder

Wednesday, 17 June 2009 01:00

0 Comments

0

Dynamically specify files in RPG without theuse of embedded SQL.

Do you have physical files with the same fileformat that are separated into multiple physicalfiles--whether it be for legal reasons, queryspeed, or storage archiving--which requires youto keep modifying your programs when the newfiles are created? This article will show you howto continue creating new physical files, withoutthe need for additional program compiling andmaintenance.

In my shop, we had a physical file that needed tobe physically separated into different files forlegal reasons. These files reused the samephysical file layout for each version, and at theend of each year a new file would be created toisolate the data. There was at least one programthat would perform historical reporting byaccessing several of the files, and this programwould need to be maintained each year to includethe additional physical file that was created.

This type of programming maintenance is highlyundesirable, especially at year-end, when you'reat your busiest! My initial solution to this problemwas to write dynamic embedded SQL in the RPGprogram. This would have allowed me to specifythe file name within the SQL statement to retrievethe data from the file. But this solution wouldhave required a notable amount of program logicto be rewritten. Instead, I chose to use theEXTFILE and EXTMBR keywords on my filespecifications to accommodate this capability.

Using EXTFILE instead of embedded SQL allowedme to make minimal programming changes tothe existing program. All I needed to do waschange the file specifications and the existinglogic that opened the files.

The File Specification Keywords: EXTFILE

and EXTMBR

The file referred to in the file specification mustexist in order for your program to compile. Youcould create an empty generic dummy file torepresent the file you will be using throughout theyears, or you may chose to use an arbitrary filefrom a specific year. The only problem with thiswould come into play when you decide sometimedown the line that you want to start archivingolder files off of the system. Then the file thatyour program is referring to would no longer

Page 2: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 2/7

Search Sponsor

Popular Searches

Search

exist, and your program would not be able tocompile.

FEMPLOYEE IF E K DISK USROPN

F EXTFILE(fileName)

F EXTMBR(fileMember)

In the file specification above, you can see theEXTFILE and EXTMBR keywords being specifiedfor the file. Each of these keywords will accept aparameter to specify the external file andmember, respectively, to use within the program.The fileName and fileMember parameters arevariables that will be defined in your program todetermine the file and member to use.

The EXTFILE keyword allows you to specify thefile to use during run time, instead of at compiletime. The compiler will still validate the fileexistence and its use during the compile, but theactual file that will be used will be determinedduring run time. When you are using the EXTFILEkeyword, you must also specify the USROPNkeyword. This is required to specify the file thatwill be used prior to opening it.

You can specify the name of the file with orwithout the library, and you can also use *LIBL asthe library reference. The library is separatedfrom the file using the forward slash. For ourexample, we will be using the EMPLOYEE file inthe MYLIB library, so the fileName will be set toMYLIB/EMPLOYEE. The file name parameter of theEXTFILE keyword is case-sensitive.

The EXTMBR keyword can be used to specify themember of the file to open. The EXTMBR keywordis independent of the EXTFILE keyword, so youcan use EXTFILE and EXTMBR together orseparately. You may decide to use the EXTMBRkeyword as a replacement to the OVRDBFcommand to override your physical file memberseven if you aren't using EXTFILE. The memberparameter of the EXTMBR keyword is case-sensitive.

The DDS

For our programming example, we will beworking with a common file layout that will benamed EMPLOYEE. The EMPLOYEE DDS will bereused for each new file that is created with thenaming convention of "EMP" + YYYY, where YYYY indicates a four-digit year.

A R MCFMT

A MCACCT 6S 0 COLHDG('ACCOUNT NUMBER')

A MCFNAME 32A COLHDG('FIRST NAME')

A MCLNAME 32A COLHDG('LAST NAME')

A K MCACCT

The Example

Page 3: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 3/7

Suppose we have an employee named Eibeamma that seems to remarry every year and keeps changing her name.The data in our EMPLOYEE file will have a record of this, and we will be able to see any name changes when we readthrough the EMP2007, EMP2008, and EMP2009 files with our program using EXTFILE and EXTMBR keywords on our Fspecification.

Eibeamma's name may change every year, but her account number of 400 remains the same. So, we will open andclose the EMPLOYEE files for the past three years and use her account number to see the changes.

The RPG Code (Free-Format)

FEMPLOYEE IF E K DISK USROPN

F EXTFILE(fileName)

F EXTMBR(fileMember)

D*

D currentKey S 6S 0

D currentDate S D

D currentYear S 4S 0

D displayBytes S 52A

D fileName S 21A

D fileMember S 10A

C*

/free

currentKey = 400;

// Current Year

currentDate = %date();

exsr getAcctName;

// Last Year

currentDate = currentDate - %years(1);

exsr getAcctName;

// Two Years Ago

currentDate = currentDate - %years(1);

exsr getAcctName;

*inlr = *ON;

//------------------------------------------------------

//--- Subroutine: getAcctName ---

//------------------------------------------------------

begsr getAcctName;

currentYear = %subDt(currentDate: *YEARS);

fileName = 'MYLIB/EMP' + %editc(currentYear: '3');

fileMember = 'MCPRESS';

displayBytes = 'fileName: ' + %trim(fileName)

+ ' MBR: ' + %trim(fileMember);

DSPLY displayBytes;

open EMPLOYEE;

chain currentKey EMPLOYEE;

displayBytes = 'Account '

+ %trim(%editc(currentKey: '3'));

if %found();

displayBytes = %trim(displayBytes) + ': '

+ %trim(MCLNAME) + ', '

+ %trim(MCFNAME);

else;

displayBytes = %trim(displayBytes) + ': '

+ ' NOT FOUNDÜ';

endif;

DSPLY displayBytes;

close EMPLOYEE;

Page 4: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 4/7

endsr;

/end-free

The RPG Code (Fixed-Format)

FEMPLOYEE IF E K DISK USROPN

F EXTFILE(fileName)

F EXTMBR(fileMember)

D*

D currentKey S 6S 0

D currentDate S D

D currentYear S 4S 0

D displayBytes S 52A

D fileName S 21A

D fileMember S 10A

C*

C EVAL currentKey = 400

C* Current Year

C EVAL currentDate = %date()

C EXSR getAcctName

C* Last Year

C EVAL currentDate = currentDate - %years(1)

C EXSR getAcctName

C* Two Years Ago

C EVAL currentDate = currentDate - %years(1)

C EXSR getAcctName

C EVAL *inlr = *ON

C***********************************************************************

C* --- Subroutine: getAcctName ---

C***********************************************************************

C getAcctName BEGSR

C EVAL currentYear = %subDt(currentDate: *YEARS)

C EVAL fileName = 'MYLIB/EMP'

C + %editc(currentYear: '3')

C EVAL fileMember = 'MCPRESS'

C EVAL displayBytes = 'fileName: '

C + %trim(fileName)

C + ' MBR: '

C + %trim(fileMember)

C displayBytes DSPLY

C OPEN EMPLOYEE

C currentKey CHAIN EMPLOYEE 68

C EVAL displayBytes = 'Account '

C + %trim(%editc(currentKey: '3'));

C IF *IN68 = *OFF

C EVAL displayBytes = %trim(displayBytes) + ': '

C + %trim(MCLNAME) + ', '

C + %trim(MCFNAME)

C ELSE

C EVAL displayBytes = %trim(displayBytes) + ': '

C + ' NOT FOUNDÜ'

C ENDIF

C displayBytes DSPLY

C CLOSE EMPLOYEE

C ENDSR

The Output

Page 5: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 5/7

There are DSPLY statements throughout the program, so running the program will generate the following results:

DSPLY fileName: MYLIB/EMP2009 MBR: MCPRESS

DSPLY Account 400: Systumeye, Eibeamma

DSPLY fileName: MYLIB/EMP2008 MBR: MCPRESS

DSPLY Account 400: Isereez, Eibeamma

DSPLY fileName: MYLIB/EMP2007 MBR: MCPRESS

DSPLY Account 400: Ausfurhundrid, Eibeamma

As you can see, Eibeamma has been busy. But she'll always be known as Ausfurhundrid to me.

Download the Code

You can download this article's code (in both free-format and fixed-format) as well as the DDS for the EMPLOYEE fileby clicking here.

Thomas Snyder

About the Author:

Tom Snyder has a diverse spectrum of programming experience encompassing IBMtechnologies, open-source, Apple, and Microsoft and utilizing these technologies withapplications on the server, on the web, or on mobile devices.

Tom has over 20 years experience as a software developer in various environments,primarily in RPG, Java, C#, and PHP and holds certifications in Java from Sun and PHPfrom Zend. Prior to software development, Tom worked as a Hardware Engineer at Inteland is a proud United States Naval Veteran Submariner who served aboard the USSWhale SSN638 submarine.

Tom is the best-selling author of Advanced Integrated RPG, which covers the latestprogramming techniques for RPG ILE and Java to utilize open-source technologies.

Originally from and currently residing in Scranton, Pennsylvania. Tom is currentlyinvolved in a Mobile Application Start-up company named JoltRabbit LLC.

MC Press books written by Thomas Snyder available now on the MC Press Bookstore.

Advanced, Integrated RPG

This book shows you how to take advantage of the latesttechnologies from within existing RPG applications.

List Price $79.95Now On Sale

Read More >>

Articles by this Author:

Page 6: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 6/7

back to top

TechTip: Getting Started with Microsoft Visual Studio Express 2012TechTip: Installing Microsoft SQL Server 2012 Express on Your Local MachineUnderstand Your Remote Git Repository Options and Choose the Best Solution for YouUse Git to Document and Manage Any Source Code with Version ControlUpdate Your Data with Peace of Mind Using Commitment Control

View all articles by this author

Windows XP: So Long, Old Friend |

Microsoft

2 comments • 2 months ago

Bob LeMay — Isee this as more of a

VERSION issue than an OPERATING

SYSTEM issue. Soinstead of comparing

Windows XP and IBM i, you should compare

Windows XP and,say, IBM i …

RPG Academy, Part 1: From OPM to ILE,

ASAP | RPG

2 comments • 4 months ago

Markus — I'm looking forward to this series.

For many years I'm trying to include

intelligent ILE concepts to my application

design, but still I haven't found a consistent

design scheme. So I quite …TechTip: So Many Logical Files, So Little

Time | RPG

2 comments • 3 months ago

Mitch Gallman — Jim, yes it works with SQL

generated indexes.

The Keys to Linux Security | Linux / Open

Source

1 comment • 4 months ago

Enigma0 — What the article doesn't

reveal/discuss is the fact that the NSA, with

the co-operation/connivance of certain

manufacturers, 'updates' the microcode

(BIOS) in USA-made …

ALSO ON MCPRESSONLINE.COM

0 Comments mcpressonline.com Login

Sort by Newest Share

Start the discussion…

Be the first to comment.

WHAT'S THIS?

Favorite

< Prev Next >

Last Updated on Wednesday, 17 June 2009 01:00 User Rating: / 8

Poor Best Rate

Page 7: Reusing a Single File Description on Multiple Files _ RPG

2/5/2014 Reusing a Single File Description on Multiple Files | RPG

http://www.mcpressonline.com/programming/rpg/reusing-a-single-file-description-on-multiple-files/print.html 7/7

Related Articles:

04/16/2014 - More Free-Form for RPG, Part 304/02/2014 - Practical ILE: Building Your Service Program Library, Part 104/02/2014 - What's New with RPG Open Access and the Open Standard (OAMOS)?03/19/2014 - More Free-Form for RPG, Part 203/05/2014 - Practical RPG: Data in the Cloud, Part 2