29
Fri, Nov
0 New Articles

Logical Files: More than Alternate Sort Sequences

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

Helping other programmers address Y2K issues made me realize that many AS/400 programmers don't understand logical files. Oh, they know how to use logical files. But knowing how to use logical files is not the same as understanding the purpose of logical files. In this article—and in others to come—I intend to address this deficiency so that programmers can better use this powerful feature of DB2 UDB for AS/400.

Two Key Concepts

To understand what logical files are all about, you must first understand two key concepts:

1. In a relational database management system, each user should be able to view the same data in different ways, according to that user's needs.

2. A user can be either a human being or a computer program.

Now let me expand further on each concept. The first concept means several things. First, users need to view data in different sort sequences. While Jim thinks of employees alphabetically by last name, first name, and middle initial, Mary thinks of employees in clock number order. Second, users need to be able to see different groups of fields. Jim cares only about name, department number, and supervisor's name; Mary needs to see payroll information, such as name, hourly wage, and number of dependents. Third, users need to see the same data formatted in different ways. For instance, users in the United States wish to see dates in MM/DD/YY format, while their counterparts in England, accessing the same database, wish to see dates in DD/MM/YY format.

The second concept should be simple to understand, but few programmers seem to be aware of it. When programmers talk about users (and boy, how programmers talk about users!), they're talking about humans. However, a user can also be a computer program, because a program also has specific access requirements. That is, a program may have to read a file sequentially in order by one or more fields or randomly by a certain key; a program may need only a subset of the fields in a file; and a program expects each field to be defined in a certain way.


I use the term program loosely in this context to mean any stored machine-executed process. According to this definition, a query qualifies as a program, even though the query is really not a program in the same way that executable objects generated from RPG source code are programs.

This Month's Lesson

Rather than write a monster article to tell you everything you ever wanted to know about logical files, I'm going to write a series of shorter articles that address the common problems IT shops face. This month, I show you how to easily add new fields to a file. The programmers at Shoddilymade Manufacturing Corporation have just left a meeting in which they were told that the customer master file (CUSTMAS), shown in Figure 1, needs a new field to contain the email address of a person with whom Shoddilymade deals. The programmers are not a happy bunch, because they remember the last time they had to add a new field to a file. They had to come in on a Sunday, add the field, and recompile several hundred programs. This should not have been a big deal, especially since they have a documentation package on their AS/400, but they missed a few programs, which canceled with level checks over the next few months. On top of that, one of the programs contained a variable that happened to have the same name, data type, and size as the new field. It took three programmers a day and a half to find that bug.

Here's how the Shoddilymade IT department can fix the customer master file so that adding a field will never be a big deal again:

1. Create a logical file with the same characteristics as the physical file. That is, this logical file must have an identical record format and the same key. All fields must be listed in the logical file's DDS. Shoddilymade's new logical file is CUSTMA01. (Its DDS is listed in Figure 2.)

2. Change all programs that use the physical file to use the new logical file instead and recompile them.

3. If there are any other logical files over the physical file, make sure that all of them list the fields in the physical file in the same order in which they're stored in the physical file. DB2 UDB doesn't require you to list the fields in a logical file. If you don't list the fields, the logical file has the same record format as the physical file. If you do list the fields, the logical file has its own record format. If you add a field to a physical file, then the record format of any logical file in which fields are not listed will include the new field, and you'll have to recompile all programs that use the logical file. You absolutely must list fields in all logical files.

When copying the list of fields from a physical file's DDS to a logical file's DDS, copy only the field names, not their definitions. If you are careful to copy the field list from the physical to the logical files, the record format level identifier will not change, and you will not have to recompile any programs that use these logical files.

Here's an example that my help you understand the third point. Shoddilymade's customer master file has a logical file, CUSTML1, which is used to sort in customer name sequence. The DDS of CUSTML1 before modification is as follows:

A R CUSREC PFILE(CUSTMAS)
A K CUNAME

Notice that fields are not listed, so CUSTML1 has the same record format as physical file CUSTMAS. The programmers would copy the list of fields from the physical


file into this logical file's DDS. The DDS of CUSTML1 after modification is shown in Figure 3.

At this point, no programs use the physical file; they use only logical files. All logical files have explicit field lists, so their level identifiers will not change when new fields are added to the physical file.

Adding the New Fields

To add a new field to the physical file, perform the following steps:

1. Edit the physical file's DDS, adding the new field as desired. Normally, I add new fields to the end of the record, but this is not required. See "TechTalk: Efficient File Conversion" (February 2000, MC) for more information. Figure 4 shows how Shoddilymade's customer file is defined after the email address field is added.

2. When no one else is using the physical file, execute the Change Physical File (CHGPF) command, specifying the source file and member of the DDS in the SRCFILE and SRCMBR parameters. The default value of SRCMBR is *FILE, so you probably will not have to fill in this parameter. I've never had to. Be aware that this step may take a while if the file is large, so budget plenty of time.

Is that simple enough? Notice that you don't need to recompile any programs. All existing programs continue to run as they always did, because the level identifiers of the logical files did not change when the new field was added to the physical file.

Obviously, something must update the new field, and something must access the new field. Otherwise, there would have been no need to add the field to the file. In the case of Shoddilymade Manufacturing, two programs need access to the email address field: the file maintenance program (so the new field can be entered into the computer) and one customer inquiry program (so human users can view the email address). In the near future, a program will be developed that generates email messages that will be sent to customers. That program will also need the new field.

How do the programmers proceed? They've learned their lesson. They do not allow these programs to use the physical file, nor do they add the new field to existing logical files. Existing logical files will continue to have only a subset of the fields in the physical file. Instead, they create one or more new logical files that have the customer master file's new format. The easy way to do this is to copy the DDS of existing logical files and add the new email address field to it. Figures 5 and 6 contain DDS for two new logical files—one keyed by customer number (Figure 5), the other keyed by customer name (Figure 6). Notice that the fields are listed, of course. The Shoddilymade programmers change the programs that need the email address field to use these new logical files. They modify the display files to include the new field and recompile these few objects. The new email address field is now in production.

Too Many Logical Files?

One downside to this method (if it has a downside) is that you may wind up with a lot of logical files over a physical file. This is not really a problem, because logical files can share access paths if they have the same key and record selection criteria. For example, if the customer master file has eight logical files keyed on customer number and five logical files keyed on customer name, the system maintains only two access paths.

Nevertheless, I think it is a good idea to periodically get rid of some of the oldest logical files. This is not a difficult process. I change the programs that use an old logical file to use a newer logical file instead—this is usually just a process of changing a single F- spec in each RPG program—and recompile them. Once all the programs have successfully recompiled, I delete the old logical file.


However, be aware of one potential problem. If a field that is in the newer logical file, but not in the old logical file, has the same name as a variable or field from another file used by a program, you may run into problems. Be sure that none of the extra fields are already defined in the program you're changing.

Establish Some Rules

Coding programs to use logical files with field lists, rather than physical files, makes adding a new field so easy that you can add new fields at the drop of a hat. I once added a new field to a physical file while everyone was on coffee break.

Because it is so easy, IT shops should establish a policy for adding new fields. I'm not a big fan of committees, but a committee is exactly what you need. A group of people should review each request for a new field and approve it or turn it down. The decision to add a new field should not be made lightly. If you add a new field to a file every time some Tom, Dick, or Harry asks you to, you'll wind up with a lot of useless fields that seemed a good idea at the time. Review by a committee also prevents adding a field to store information that's already in the database.

The committee should not only verify that the request for a field is legitimate but also determine the format of the data. I was recently asked to add a one-byte field to a database to contain either a Y or an N. A few days later, I was asked to add another field to contain more information about the Ys and to be blank for the Ns. I realized the one-byte field was unnecessary, so I expanded the field from one byte to 20 bytes instead of adding a new field. The point is that the users knew I could add a field to that file in a matter of minutes, so they didn't think the problem through well enough, nor did I.

It's Only Logical

Easily adding a new field to a physical file is only one thing logical files can do to improve the lives of people in the IT department. For another example of how logical files make program maintenance easier, see my article "Use Logical Files to Ease the Y2K Conversion," MC, January 1999.

I have not yet determined what I will cover in future articles, so if you have a favorite logical file technique you'd like me to write about, or if you would like to write about a logical file technique, please let me know.

References and Related Materials

"Use Logical Files to Ease the Y2K Conversion," Ted Holt, MC, January 1999

A UNIQUE

A R CUSREC

A CUNBR 5 0 TEXT('CUSTOMER NUMBER')

A CUNAME 25 TEXT('CUSTOMER NAME')

A CUTYPE 2 TEXT('TYPE CODE')

A CUCONT 16 TEXT('CONTACT NAME')

A CUPHON 20 TEXT('PHONE NUMBER')

A K CUNBR

Figure 1: This is the customer master file before the email address is added to it.


A UNIQUE

A R CUSREC PFILE(CUSTMAS)

A CUNBR

A CUNAME

A CUTYPE

A CUCONT

A CUPHON

A K CUNBR

A R CUSREC PFILE(CUSTMAS)

A CUNBR

A CUNAME

A CUTYPE

A CUCONT

A CUPHON

A K CUNAME

A UNIQUE

A R CUSREC

A CUNBR 5 0 TEXT('CUSTOMER NUMBER')

A CUNAME 25 TEXT('CUSTOMER NAME')

A CUTYPE 2 TEXT('TYPE CODE')

A CUCONT 16 TEXT('CONTACT NAME')

A CUPHON 20 TEXT('PHONE NUMBER')

A CUMAIL 40 TEXT('EMAIL ADDRESS')

A K CUNBR

A R CUSREC PFILE(CUSTMAS)

A CUNBR

A CUNAME

A CUTYPE

A CUCONT

A CUPHON

A CUMAIL

A K CUNBR

A R CUSREC PFILE(CUSTMAS)

A CUNBR

A CUNAME

A CUTYPE

A CUCONT

A CUPHON

A CUMAIL

A K CUNAME

Figure 2: Logical file CUSTMA01 has the same record format and key field as the physical file.

Figure 3: The field list has been added to logical file CUSTML1; the record format level identifier has not changed.

Figure 4: The email address field has been added to physical file CUSTMAS.

Figure 5: Logical file CUSTMA02 provides access to the email address field and allows records to be retrieved by customer number.

Figure 6: Logical file CUSTML2 provides access to the email address field and sequences records by customer name.


TED HOLT

Ted Holt is IT manager of Manufacturing Systems Development for Day-Brite Capri Omega, a manufacturer of lighting fixtures in Tupelo, Mississippi. He has worked in the information processing industry since 1981 and is the author or co-author of seven books. 


MC Press books written by Ted Holt available now on the MC Press Bookstore.

Complete CL: Fifth Edition Complete CL: Fifth Edition
Become a CL guru and fully leverage the abilities of your system.
List Price $79.95

Now On Sale

Complete CL: Sixth Edition Complete CL: Sixth Edition
Now fully updated! Get the master guide to Control Language programming.
List Price $79.95

Now On Sale

IBM i5/iSeries Primer IBM i5/iSeries Primer
Check out the ultimate resource and “must-have” guide for every professional working with the i5/iSeries.
List Price $99.95

Now On Sale

Qshell for iSeries Qshell for iSeries
Check out this Unix-style shell and utilities command interface for OS/400.
List Price $79.95

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: