|
|||||||
|
|
![]() |
|
|
Program Cloning to Prevent Overtime by G. Wayne Hawks [The code for this article is available for download.] Wouldn't it be neat if you could have a program write programs? Maybe you need to change one thing in a whole bunch of files, or count something that might be in many different files, or delete specific records in a bunch of files. Rather than writing a whole bunch of programs yourself, wouldn't it be great if you could just write one program and have another program make copies of it, modified to accomplish a different but related task against the same files? First, a disclaimer. As my daughter Suewellyn says, "I'm really an intelligent person, but it comes and goes." So you get to decide where I'm at in the intelligence cycle right now. Seriously, this is a great tool for your toolbox, but be VERY careful. It IS a very sharp tool and it can cut YOU if you're not careful. In my last article, "Dr. Programmer, Private Eye," I briefly touched on the fact that source code itself is data. It's organized in files, with multiple members. All source files, regardless of length, have three fields: SRCSEQ, SRCDTA, and SRCDAT. SRCSEQ is the field that contains the line number, SRCDAT contains the date on which that line was last changed (in YYMMDD order), and SRCDTA has the actual code lines. Say your iSeries has multiple companies in most of its files. Each file has a company-number field, to keep company 1 separate from company 2. You have to count how many records in each of your files contain company 1 and company 2. Maybe company 2 is doing so well that it is getting its own iSeries, and you want to copy its data off your system. But how much data, and in which files does that data reside? The problem is that each physical file probably has a different name. And each physical file probably has a different format name. And each file may have a different field name that contains the company number. To drive the cloning process, you need a file that contains the library, file, format, and field names to put in each copy of the program. Step 1 The first step is to use OS/400's DSPFFD (Display File Field Description) command to an output file for *all files in each of the libraries (be sure to add, not replace, each time you do the DSPFFD command) that contain the files you're interested in. The resulting output file has almost all the fields you need (note that in the list below, field lengths are in parentheses):
WHLIB is the Library Name (10)
WHFILE is the File Name (10)
WHFTYP is the Type of File. (1)
WHNAME is the Record Format Name (10)
WHTEXT is the File's Description (50)
WHFLDI is the Field's Name (10)
WHFTXT is the Field's Description (50)
WHFLDD is the number of digits (5,0)
WHFLDP is the number of decimals (2,0)
Step 2 In the second step, you create another file with all the above fields (named the same and the same lengths). Add a field to indicate that you want to use that record, maybe WHWANTS (1). Then copy the first file to this second file, using *map and *drop, and where WHFTYP *EQ 'P'. There's no sense in building programs for logical files, because they don't contain data. You just need programs for the physical files. Step 3 Now that you have your universe of all fields in all physical files in the libraries you are interested in, you need to reduce that to just the ones you want. How can you do that? Well, maybe you know that all of your company-number fields are a certain length--say they're 3 long with 0 decimal places. And say that you know that the company-number fields always have field names that look like xxCO# (where xx stands for anything). You might want to create a subfile program that loads into the subfile only those records whose fields are 3 long with 0 decimal places, and whose field name's third through fifth positions are CO#. Make sure to include (as an updatable field) the new WHWANTS field. Then put an x or a y, whatever you want to do to indicate that you do want that one. If you don't want to type that much, you can make it so that, if you just type in an n, that means you don't want it. The point is to give yourself a way to select which ones you want. Your candidates are then displayed in the subfile. You get to vote for the ones you're going to create programs for. Step 4 Next, copy the file to another file, indicating WHWANTS = 'Y' or X' or ISN'T 'N', depending on which method you chose in Step 3 to indicate you want it. Or create a logical file to select only those that you want. Use this file to drive your CL program. Step 5 Create a model RPG program with file, format, and field names that are easily identifiable from the rest of the code. I used the words RPLFIL, RPLFRM, and RPLFLD, but you can use anything you want. Have the model program output one record to a new file containing counts of whatever you want. You only need to write to this file once, at the end of the program. See MODEL in the source file sample. Step 6 Create an RPG program to modify the copies of the original model program. You'll want to use the SCAN op code to easily find what to replace. See MODMODEL in the source file sample. Step 7 Create an RPG program that creates new source member names for you. See MODMODELNM in the source file sample. Step 8 Create a new source file to put your cloned source into. This makes it much easier to clean out when you want to use it again. Also create a new library to put your cloned objects into (again, it's a lot easier to just clear a library). Step 9 Create the CL program that calls the program to get a new name, that copies the model program to the new source member name (in the new source file you create in Step 8), that overrides to the new source, that calls the modifying program, that compiles the new program, and then calls or submits it. See MODMODELC in the source file sample. Step 10 Back up your entire system, get your favorite beverage, submit the program, and watch the cloning process very closely. Step 11 If you're really brave, and you've checked the results of your output file, and everything looks great, reset the file that holds the program number counter, delete all the cloned source, clear the library with all the cloned objects, modify the model program to DELETE, instead of count, the records, and you could use this process to remove all that other company's records. This time, do us both a favor and really do the backup before you run it! Cloning programs requires some homework, but it's a very efficient way to get a lot of work done in a hurry. If you've done your homework, it's amazing to watch programs writing programs and doing what you want. If not, it's like a cloning experiment gone wrong, and don't say I didn't warn you when that monster sneaks up behind you. G. Wayne Hawks is a programmer/analyst with 14 years of experience in AS/400 programming. E-mail: donkey_hote@hotmail.com
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |