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.


BLOG COMMENTS POWERED BY DISQUS