02
Sat, Nov
2 New Articles

How to Find an Existing Logical File Key

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

Logical files are one of the features of DB2/400 that make it so powerful. They’re so easy to create that make databases are cluttered with unnecessary logical files. Use this handy utility to find an existing access path when you need one.

Recently, I was working on a project when I came across a physical file that had 40 dependent logical files. Forty! This got my curiosity going, so I started looking at the keys to the various logical files, and I discovered that several of them were duplicates of each other. I know that, right now, you’re either wondering how this could possibly happen or nodding your head in complete understanding. Well, our shop is lean and mean, which means we have no database administrator to control the addition of logical files. And, like most other shops, we have had our share of “come-and-go” contract programmers. You can probably see where I’m going with this: Inexperienced or hasty programmers might create a logical file without first checking to see if one already exists with the key they need.

This unnecessary duplication is a problem that can quickly multiply, and the result is a drain on performance, since each change to a record requires DB2/400 to maintain more and more access paths. The obvious solution is to look for the needed key in the existing logical files before creating a new one. But how do you convince your programmers to manually check 40 logical files for a particular key, especially if they are pressed for time? You could provide them with a quick, easy, and accurate utility to do the work for them. Our shop didn’t have such a utility, however, so I created one myself.

This utility, called Check Logicals (CHKLGL) combines basic CL commands with the power of API messaging, and the result is an automated file search that takes a fraction of the time that a manual search would take. (For the source code to this utility, see MC’s Web site at www.midrangecomputing.com/mc/99/01.) The programmer simply enters the name of a physical file, its library, and the appropriate key fields in the desired order. The program then displays the applicable logical file name, if one exists. If the physical file itself has the appropriate key list, that file name is displayed, and the logical files are not checked.

The display file is simple enough. It collects the physical file name, the library where it resides, and the fields required for the file key. I have allowed for 10 key fields. The display file uses the Cursor Location (CSRLOC) keyword to position the cursor on fields in error without using indicators. I use a basic line-24 message subfile to display error and information messages to the programmer. I have enabled F3 to exit the program and F5 to refresh the screen, and an indicator is set to protect the input fields when the results are displayed. I have also enabled Help, which I have written in the User Interface Manager (UIM) panels. The Help panels are included on MC’s Web site for review but will not be discussed in this article.

You will need to create source for three logical files. These files will be based on the output from the following three commands that will run in the CL modules. Each logical file name is a combination of the command, Q for library QTEMP, and L for logical file.

• DSPFFDQL will be a view of the output from the Display File Field Descriptions (DSPFFD) command by field name. This file will be used to validate the field names entered by the programmer.

• DSPDBRQL will be a view of the output from the Display Database Relations (DSPDBR) command by file name. This file will be used to capture all of the dependent logical files.

• DSPFDQL will be a view of the output from the Display File Description (DSPFD) command by file name, key field name, and key field number in key list, omitting records for join logical files. The programmer will use this file to search for the first logical file that has the same key as the key entered.

The main RPGLE module, CHKLGL_A, collects the file information from the programmer and edits the information for accuracy. When an error is encountered, it uses the system-supplied Send Program Message (QMHSNDPM) and Remove Program Message (QMHRMVPM) APIs to write the error message(s) to the line-24 message subfile and to clear the error message subfile. When a screen field is found in error, the row and column positions are placed in the ROW and COL fields in order to position the cursor on the field in error. I wanted to capture as many errors as possible, so the main module performs the following edits:

• The file name and library name must be entered.
• The programmer must enter at least one key field.
• The programmer must not enter the same key field more than once.
• The file must exist in the specified library and must be a physical file. This implies that the library must also exist. Module CHKLGL_1 is called to validate the file and library.

• All fields entered must exist in the physical file. Module CHKLGL_B is called to validate each field.

Once all of the edits have been performed and the data is correct, module CHKLGL_C is called. This module looks for a logical file with the specified key list. If the key is found in the physical or a logical file, the file name and library are displayed in a message to the programmer. If the key list is not found, an informational message is displayed to the programmer.

Module CHKLGL_1 is the first module called by the edit routine of the main module. It is used to validate the file and library name entered by the programmer. It also creates the work files that will be used in this utility.

The Check Object (CHKOBJ) command is used to validate the file and library. If the file or library doesn’t exist, an error flag is set and returned to the main module. The Retrieve Object Description (RTV-OBJD) command is used to retrieve the file attribute. If the selected file is not a physical file, an error flag is set and returned to the main module.

If the file exists in the library and is a physical file, the work files will be created in library QTEMP for use by this utility. First, the logical and physical work files are deleted

from QTEMP if they exist. The physical work files are then created with file commands, with the output going to outfiles. I then take the following steps to create the files:

• I use the DSPFFD command to retrieve the file layout for the specified file. The resulting file will contain one record for every field in the file.

• I use the DSPDBR command to retrieve the dependent logical files. The resulting file will contain one record for every dependent logical file.

• I use DSPFD with the TYPE(*ACCPTH) keyword and parameter to retrieve the access path description for a file. This file will contain one record for every key field in the file.

After each work file is created, a logical file is created for it, using the source members that were created earlier.

Module CHKLGL_B is also called from the edit routine of the main module. It is used to validate the fields entered by the programmer. It does a Set Lower Limits (SETLL) operation on the work file DSPFFDQL with the field name received from the main module. The resulting indicator will be set on if the field is found in the file. (Note that I use a SETLL operation and not CHAIN. I just want to see if the field exists; I don’t want to do anything with it right now.)

Now that all of the edits are complete, there is just one more task to accomplish, and that is to find the file. Module CHKLGL_C is called to do this. It receives the key fields (in one string) and a result flag as parameters, as well as result fields to return the logical file name and library to the main module.

Recall that earlier we created a work file called DSPFDQL. It contains one record for every key field in the file in the correct order. The module first checks the physical file to determine if it contains the required key. If not, it starts reading the work file DSPDBRQL, which contains one record for every dependent logical file, except join logical files. Each time a record is read, the module closes the work file DSPFDQL and calls module CHKLGL_2. CHKLGL_2 re-creates file DSPFDQ with the access path information for the dependent file.

Look at the D-specs for this module, and note that the string of key fields received as a parameter is overlaid by an array. A key list is set up with the file name, the field name as an element of the array, and the element number. This element number will correspond to the key field number in the key list. The program steps through the array, again using the SETLL operation, to determine if the current key element matches the key in the access path. The first time the key fields do not match, you know that the key fields in the access path do not match the key order specified by the programmer. When this happens, the module will exit the loop, get the next record from work file DSPDBRQL, and begin the process again.

A logical file is considered a match if it has the same key list as entered or if it has a longer key list that matches the one entered. For example, if the programmer entered the fields FLD1, FLD2, and FLD3 in that order, and the logical has the fields FLD1, FLD2, FLD3, and FLD4 in its key in that order, that logical file is accepted.

When CHKLGL_C returns control to the main module, CHKLGL_A will display a message to the programmer. If a match is found, the message will contain the matching logical (or physical) file and its library. If no match is found, an informational message will be displayed and the programmer can try another key.

You can see that this utility will come in very handy. It takes advantage of system- supplied APIs and messages, and its modular approach makes for easy coding and maintenance. You will enjoy better performance on your AS/400 if you can cut down on the number of logical files, and this utility will pay for itself after just a few uses with the time that you save.

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: