The first part of this article series showed how DSPOBJD can easily provide statistics to manage a general recompilation project. But it only touched the surface of all the things you get with the DSPxxx commands.
Â
Let's start with a review of the information we gathered in Part I by just using DSPOBJD.
Objects by Type and Year
This data is concerned with counting objects by type and by year of source update (for those that have a source, of course).
Â
If we had used ODCCEN plus ODCDAT (i.e., the object creation date) instead of ODSRCC plus ODSRCD, we'd have gotten a report on recompilation instead of a report on source update.
Â
with stat as (SELECT ODOBTP "Type" , odobat "Attribut", trim(char( ODSRCC+ 19)) concat substr(ODSRCD , 1 , 2) "Year" FROM jpltools.objs where odsrcc <> '') select "Year" ,"Type" , "Attribut",  count(*) Number from stat group by "Type" , "Attribut", "Year" order by  "Year","Type" , "Attribut"Â
Â
Figure 1: Here's the list of objects by type and year.
Â
The code below counts programs (in fact, programs and modules, to also take care of ILE objects) by compiler. Note that objects without source are eliminated by the WHERE clause.
Â
SELECT odobat "Attribut" , count(*) Number FROM jpltools.objs where odsrcc <> '' and ODOBTP in('*PGM', '*MODULE') group by odobat order by  odobat
Â
Â
Figure 2: The programs are counted by compiler type.
Â
There are two OPM programs in the libraries analyzed: one CLP and one RPG. But this analysis doesn't show whether the ILE programs use the ILE default activation group or a specific activation group. This information is provided by DSPPGM, which does not offer an output file. In the third part of this series, we will use the program description APIs for this information. Â
Sources in Library
The following SQL will reveal the number of instances of source code per library.
Â
select count(*) number, ODSRCL srclib FROM jpltools.objs group by ODSRCL
Â
Â
Figure 3: JPLTOOLS has 70 source files.
Â
Old applications may have had their libraries of code renamed many times and can show traces of these old libraries' names, so this list can be overwhelming. There are often names of libraries that no longer exist. This does not mean that the source code is lost, but it would be wise to check that the available source code has the same date indicated in the object.
Sources (aka File Members) per File
The following code will show you the number of instances of source code per file. Note that the objects without source are eliminated by the WHERE clause.
Â
select count(*) number, ODSRCF srcpf FROM jpltools.objs where ODSRCF <> '' group by ODSRCF order by number desc
Â
Â
Figure 4: Here's the number of instances of source code per file.
Â
Much less often than in the list per library, this list can show source file names that have disappeared ... or should have disappeared.
Objects by Version
In a complex environment, it is common to have multiple machines with different version levels. This analysis ensures that the application can be used on a machine that is not yet at the latest version level.
Â
SELECT count(*)number , ODcpvr i5os FROM jpltools.objs where odobtp in ('*PGM','*SRVPGM') group by  odcpvr order by i5os
Â
Â
Figure 5: Objects are shown by version.
             Â
Here, the objects are all compiled for V7.1. Installation on a machine running an older version will require a recompilation. Sometimes, problems can appear if one or more programs use specific features of V7.1.
Â
Note: In the case of JPLTOOLS, the question does not arise since I offer only the source code. And the JPLTOOLS code is backward-compatible until V5.4.
Objects by Owner
Is management of the owners being done correctly? DSPOBJD gives no information on adoption of rights. We will use the program description APIs for this information.
Â
SELECT count(*)number , ODobow owner FROM jpltools.objs group by  odobow
Â
Â
Figure 6: Objects are shown by owner.
Â
Ha! I have to make corrections.
Â
All the above information was obtained only by using the result of DSPOBJD. Now we will extend our investigations to other commands. Â We'll use SQL to mix the results provided.
Check the Date of the Source Code
The target of this analysis is to check that the source has not changed since last compilation.
Â
Obviously, this runs only for programs built with a module of same name. By doing this, I'll artificially create a link between the source member and the program. In fact, when context is more complex than a source producing a module producing a program, I will need a true link between these three elements. APIs provide this information . I will return to that point later.
Â
How do I do it? Run a DSPFD MBRLIST and join!
Â
DSPFD FILE(JPLTOOLS/*ALL) TYPE(*MBRLIST)Â OUTPUT(*OUTFILE)Â FILEATR(*PF)Â
 OUTFILE(JPLTOOLS/MBRLIST)
Â
OK, but this list contains members of all files: PF-DTA and PF-SRC.
Â
To keep only the membership list of source files, retrieve the attributes of all files:Â
Â
DSPFD FILE(JPLTOOLS/*ALL)Â TYPE(*ATR)Â OUTPUT(*OUTFILE)Â FILEATR(*PF)
 OUTFILE(JPLTOOLS/FD_ATR)
Then, isolate the source filenames:
Â
SELECT PHFILE FROM JPLTOOLS.FD_ATR WHERE PHDTAT='S'
Â
Keep only the members of the source files:
Â
DELETEÂ FROM JPLTOOLS.MBRLIST WHERE MLFILE NOT IN (SELECT PHFILE FROM JPLTOOLS.FD_ATR WHERE PHDTAT='S')
Â
Reminder: DSPOBJD gives us the exact date of the source code when compiling the module. DSPFD MBRLIST gives us the exact date of last modification of the source code.
Â
The next step is to simplify reading of DSPFD-MBRLIST. For the modification date of the source code, use this code:
Â
SELECT MLNAME, case when MLCHGc = '' then ' ' else trim(char(Â MLCHGC+ 19)) concat MLCHGD concat '-' concat MLCHGt end Upddate,MLMTXT FROM jpltools.mbrlist WHERE MLCHGc <> ''Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Â
Figure 7: Simplify the reading of DSPFD-MBRLIST. (Click images to enlarge.)
Â
Now, simplify the reading of DSPOBJD. For the date of compilation of source code, use this code:
Â
SELECT ODOBNM, ODSRCM , case when odsrcc = '' then ' ' else      trim(char( ODSRCC+ 19)) concat ODSRCD concat '-' concat odsrct end cpldate , ODOBTX FROM jpltools.objs WHERE odsrcm <> '' and odsrcc <> ''  Â
Â
Figure 8: Simplify the reading of DSPOBJD.
Â
 The join of the two previous results (with SQL CTE, common table expression) gives the list of programs without source (based on an SQL exception join):
Â
with cpl as ( SELECT ODOBNM, ODSRCM , case when odsrcc = '' then  Â
' ' else trim(char( ODSRCC+ 19)) concat ODSRCD concat '-' concat  Â
odsrct end cpldate , ODOBTX FROM jpltools.objs WHERE odsrcm <> ''Â Â
and odsrcc <> '' ), upd as ( SELECT MLNAME, case when MLCHGc = ''Â Â
then ' ' else trim(char( MLCHGC+ 19)) concat MLCHGD concat '-'Â Â Â Â Â
concat MLCHGt end Upddate,MLMTXT FROM jpltools.mbrlist WHERE MLCHGc
<> '' ) select ODOBNM, ODSRCM , cpldate, odobtx from cpl exceptionÂ
join upd on odsrcm = mlname                                       Â
Â
Figure 9: Here's the list of programs without source.
Â
Now find the list of source that are not used by a compiler to build an object—for example, code description of a service program, code of an SQL procedure, or copies of code:
Â
with cpl as ( SELECT ODOBNM, ODSRCM , case when odsrcc = '' then Â
' ' else trim(char( ODSRCC+ 19)) concat ODSRCD concat '-' concat Â
odsrct end cpldate , ODOBTX FROM jpltools.objs WHERE odsrcm <> ''Â
and odsrcc <> '' ), upd as ( SELECT MLNAME, case when MLCHGc = ''Â
then ' ' else trim(char( MLCHGC+ 19)) concat MLCHGD concat '-'Â Â Â Â
concat MLCHGt end Upddate,MLMTXT FROM jpltools.mbrlist WHERE MLCHGc
<> '' ) select upd.* from upd exception join cpl on odsrcm = mlname
Â
Figure 10: Here's a list of sources without objects.
Â
Now you'll want the list of sources modified but not recompiled:
Â
with cpl as (
SELECT ODOBNM, ODSRCM , case when odsrcc = '' then ' ' else trim(char( ODSRCC+ 19)) concat ODSRCD concat '-' concat odsrct end cpldate , ODOBTX FROM jpltools.objs WHERE odsrcm <> ''Â Â and odsrcc <> ''
), upd as (
SELECT MLNAME, case when MLCHGc = ''Â Â then ' ' else trim(char( MLCHGC+ 19)) concat MLCHGD concat '-'Â Â Â Â Â concat MLCHGt end Upddate,MLMTXT FROM jpltools.mbrlist WHERE MLCHGc <> ''
) select ODOBNM "Object", ODSRCM "Src Mbr",
cpldate concat case when upddate = cpldate then '' else ' was updated ' concat upddate end  "Compiled_date"
, case when mlmtxt = odobtx then '  ' concat odobtx else '>> ' concat odobtx concat ' changed to ' concat mlmtxt end   "Text"
from cpl join upd on odsrcm = mlname and (cpldate <> upddate Â
or odobtx <> mlmtxt)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
                                           Â
Figure 11: These are the sources modified but not recompiled.
Â
 I changed my machine; we see the traces dated 2010-11-12-20:41:42.
Â
Remember that these analyses are based on the fact that program and source code members have the same name. It is common, but not 100 percent accurate.
Program Cross-Reference
I'm gaining momentum, and after getting so much information from two system commands, other questions have quickly come to mind:
Â
How can we tell if a program or file is no longer used? Explore DSPPGMREF. Obviously, we cannot answer this question if the program name is determined dynamically in the application.
Â
Here's a more difficult question: A program that should not be used remains called in some programs (last-used date is recent, days-used count continues to grow…). How can we get an impact analysis of deleting  this program? The answer is again provided by SQL—specifically by the recursive SQL on the DSPPGMREF outfile.
Â
In the same way and by writing the recursion in the opposite direction, I have obtained for a given program a list of all programs and files it uses. SQL recursion has been available since V5R4.
Â
This is the DSPPGMREF, simple version:
Â
DSPPGMREF PGM(JPLTOOLS/*ALL)Â OUTPUT(*OUTFILE)Â OBJTYPE(*ALL)
 OUTFILE(JPLTOOLS/PGMREF)
Â
Why a "simple version"? Because there's a complex version. Indeed, DSPPGMREF allows you to find cross-references—not only PGM, but also SQLPKG, SRVPGM, MODULE, and QRYDFN. Yes, but with several minor problems.
Â
First problem: The structure of the DSPPGMREF outfile does not give the type of the object scanned. This information is actually provided by the column WHSPKG:
Â
WHSPKG Â Â Â Type
P Â Â Â Â Â Â Â Â *PGM
S Â Â Â Â Â Â *SQLPKG
V Â Â Â Â Â Â *SRVPGM
M Â Â Â Â Â Â *MODULE
Q Â Â Â Â Â Â *QRYDFN
Â
This forces us to, at least, require that the transformation be able to make joins with the DSPOBJD file:
Â
alter table jpltools.pgmref add column whptyp char (10 ) not null with default  ;
update jpltools.pgmref set whptyp = case
when whspkg ='P' then '*PGM'
when whspkg ='S' then '*SQLPKG'
when whspkg ='V' then   '*SRVPGM'
when whspkg ='M' then '*MODULE'
when whspkg ='Q' then '*QRYDFN' end      Â
Â
Second problem: Variable names are exposed (dynamically called programs). We may want to ignore them. And the modules overlap program references, so they are not useful.
Â
Third problem: DSPPGMREF provided a list of all formats, especially for DSPF. My analysis of cross-references does not descend to the file format level; this information is then simply useless duplicates.
Â
Fourth problem: Depending on whether the target application uses the library list or the qualified objects, joins will, or will not, integrate the objects  library.
Â
Fifth problem: WHLNAM and WHFNAM columns for the library and the referenced object are 11 characters instead of 10, causing some errors in the joins on WHLIB and WHPNAM
Â
Embedding SQL transformation (needed to correct these problems) in the recursive SQL would make the SQL statement not understandable. There are two methods to deal with this problem: Either build a view on jpltools/pgmref, or do an insert into another table. I chose the insert.
Â
Which brings us to this SQL to repackage the PGMREF file:
Â
drop table jpltools.root ;
create table jpltools.root as (
select distinct –- solve duplicates on file name
  whlib caller_lib
, whpnam callerÂ
, cast (case
   when whspkg ='P' then '*PGM'
   when whspkg ='S' then '*SQLPKG'
   when whspkg ='V' then   '*SRVPGM'
   when whspkg ='M' then '*MODULE'
   when whspkg ='Q' then '*QRYDFN' end as char(10)) as caller_type
,  cast(whlnam as char(10)) called_lib -- resize correctly called
,  cast(whfnam as char(10)) called
, whotyp called_type
, cast( '' as char(30) ) caller_key
, cast( '' as char(30) ) called_key
 from jpltools.pgmref
 where whspkg <> 'M'
) with data including column defaults
;
update jpltools.root set
caller_key = --caller_lib concat -– don't use object lib in join
  caller concat caller_type
,called_key = --called_lib concat
  called concat called_type
;
Â
The Caller_key and Called_key columns are used to simplify the writing of recursive joins.
Â
It is likely that you already have a scheduled batch job that runs DSPPGMREF. I suggest that you either compile the three samples of SQL above by using CRTQMQRY and then add these three with a repackaging STRQMQRY in the same batch, or copy the three samples of SQL above in a source member and then add one repackaging RUNSQLSTM in the same batch.Â
Cascading Caller to Called
This recursive SQL takes as root all the lines of repackaged DSPPGMREF (ROOT file). The Title_Caller column is used to keep track of the path and also to detect loops in the recursive calls. The Title_Called column is used to power the previous one. Level column can master the infernal loops and the too-deep analysis.
Â
with cascade (caller, title_caller, called, title_called
            , level, called_key) as (
   select caller,
       cast(trim(caller_lib) concat '/' concat trim(caller)
           as varchar(1000) ) title_caller
     , called
     , cast(trim(called_lib) concat '/' concat trim(called)
           as varchar(21) ) title_called
     , 1 level
     , called_key
   from jpltools.root
/* here choose the caller to analyze */
-- where caller='MYPGM'
   union all Â
   selectÂ
       cascade.caller
     , cascade.title_caller concat ' > '
               concat cascade.title_called
     , child.called
     , cast(trim(child.called_lib) concat '/'Â
               concat trim(child.called)
          as varchar(21) ) title_called
     , level + 1
     , child.called_key
   from cascade, jpltools.root child
   where cascade.called_key = child.caller_keyÂ
/* here is a loop stopper */
       and cascade.title_called not like '% ' concat trim(child.called) concat ' %'
/* here is another loop stopper */
       and cascade.level <= 20
/* here choose the called to follow */
--
) select cascade.* from cascade order by title_caller
;
Â
Figure 12: Cascade caller to called.
Â
Now, the same recursion, but the other way. This analysis allows you to, for example, search to find whether a file or program is still present that should have disappeared, understand the impact of a change, or prepare a test plan.
Â
with cascade (caller, title_caller, called, title_called
    , level, caller_key) as (
   select caller,
       cast(trim(caller_lib) concat '/' concat trim(caller) as varchar(21) ) title_caller
     , called
     , cast(trim(called_lib) concat '/' concat trim(called) as varchar(1000) ) title_called
     , 1 level
     , caller_key
   from jpltools.root
/* here choose the caller to analyze */
-- where whpnam='MYPGM'
   union all
   selectÂ
       child.caller
     , cast(trim(child.caller_lib) concat '/' concat trim(child.caller) as varchar(21) ) title_caller
     , cascade.called
     , cascade.title_called concat ' < ' concat cascade.title_called
     , level + 1
     , child.caller_key
   from cascade, jpltools.root child where cascade.caller_key = child.called_keyÂ
/* here is a loop stopper */ and cascade.title_called not like '%' concat trim(child.caller) concat '%'
/* here is another loop stopper */ and cascade.level <= 20
/* here choose the called to follow */
) select title_called, called, caller, level from cascade order by title_called
;
Â
Figure 13: Cascade called to caller.
Â
This SQL statement, which is already complex, suffers from two defects: The lines are not displayed in a particularly consistent order, and the presentation is not very friendly.
Â
Here is the version I used to prepare the analysis to Excel:
Â
with cascade(called, called_type, called_title
           ,caller, caller_type, caller_title
           , level, called_key, caller_key) as (
 select
 called
, called_type
, called
, called
, called_type
, cast(called as varchar(1000))
, 0
, called_key
, called_key
from jpltools.root
Â
 union all
Â
select
 cascade.called
, cascade.called_type
, cascade.called_title concat ' < ' concat cascade.caller_title
, child.caller
, child.caller_type
, trim(child.caller)Â as title_caller
, level + 1
, cascade.called_key
, child.caller_key
 from cascade join jpltools.root child on
     cascade.caller_key = child.called_key
where
/* here is the loop stopper on recursive call */
  cascade.called_title not likeÂ
     '% ' concat trim(child.caller) concat ' %'
/* here is the loop stopper on too deep analysis */
  and cascade.level <= 20
/* here choose the caller type to follow */
--
 ) --select * from cascade order by called_title;
, ordered as (
Â
select distinct
  cascade.called_title concat ' < '
     concat cascade.caller_title order_by
  , cascade.*
from cascade
 ) -- select * from ordered order by order_by;
Â
select
 case when level = 0 then called_type else '' end "Called Type"
, case when level = 0 then called else '' end "Called"
, level "Level"
, case when level > 0 then caller_type else '' end as "Type"
, case when level > 0 then OBJS.ODOBAT else '' end as "Attribute"       Â
, case when level > 0 then
   SUBSTR('. . . . . . . . . . . . . . . . . . . . ',1,ordered.LEVEL*2)
   CONCAT   ordered.CALLER else '' end AS "Caller"
,Objs.Odobtx "Description"
 from ordered
left join jpltools.objs objs on caller = odobnm and odobtp=caller_type
order by order_by
Â
The advantage of iNavSql (iSeries Navigator SQL Editor) is that it offers an easy way to modify and/or adapt queries.
Â
Unfortunately, we can so quickly adapt an SQL statement that often we forget to back it up. The backup is not automatic. And iNavSql stores the previous executions in the message window. This window is not very visible and is erased each time you close iNavSql.
Â
Excel, in contrast, with its minuscule editing window, really does not facilitate the editing of the SQL…if you can retrieve it! It is here:
Â
Figure 14: Retrieve SQL connections used into Excel.
Â
Â
Figure 15: Retrieve SQL statements used into Excel.
Â
Be sure not to confuse the properties of the connection with the properties of the import.
Â
  Â
Figure 16: Retrieve the properties of the import process.
Â
Â
Figure 17: Update the properties of the import process.
Â
These maintenance difficulties are one reason that iNavSql is dedicated to the development of SQL and Excel for use in production.
Â
There's nothing complicated about this next part: When the SQL is developed, each result is in a different tab. After opening the Excel sheet, click here for updated information:
Â
Â
Figure 18: Refresh SQL data into the Excel spreadsheet.
Â
Note that MS QUERY doesn't really try to understand the SQL to execute. That makes it not very tolerant to syntax error. Â When building the connection, follow this dialogue:
Â
Â
Figure 19: Here's an example of syntax error.
Â
Note the syntax error: The final semicolon (;) is not supported.
Â
The classic error message shown in Figure 20 is followed by the explanation in Figure 21.
Â
Â
Figure 20: Yes, continue!
Â
Â
Figure 21: Excel shows the syntax error.
Â
These constraints should encourage you to prepare your application analysis in batch at night and leave the bare minimum SQL in Excel.
Â
But the night batch, with an output file, brings a new constraint: The recursive SQL is a powerful tool based on the common table expression (CTE). But the use of CTE is prohibited in a CREATE AS (SELECT clause) or in an INSERT INTO (SELECT clause). That forbids us from using most tools capable of executing SQL statements here because we want an output file. The only native tools capable of providing output to a file without going through the SQL clauses CREATE AS or INSERT INTO are STRSQL in interactive and STRQMQRY in batch mode.
Â
For example, let's consider the SQL Called to Caller. The QMQRY for the caller list and the called list is available in my toolbox at http://jplamontre.free.fr/jpltools.htm#Index. Follow "The savf with all the sources." Compile with CRTQMQRY:
Â
CRTQMQRY QMQRY(JPLTOOLS/ANZAPCALED) SRCFILE(JPLTOOLS/JPLTOOLS)
Â
Then run it:
Â
STRQMQRY QMQRY(JPLTOOLS/ANZAPCALED) OUTPUT(*OUTFILE) OUTFILE(JPLTOOLS/CALLEDPGM) NAMING(*SQL)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Â
Then, in Excel, just do this:
Â
Select * from jpltools.calledpgm
Â
Figure 22: The simplified SQL goes into Excel.
Â
Presentation is improved. The line order is correct, the called object appears only when changed, and the caller is indented depending on level of deepness.
Â
Note: To be able to use QMQRY with *SQL naming, I had to explicitly qualify the name of the *FILE object. It works. I have found the parameter RDB(*NONE) to specify "use the local database" but nothing to specify "use the current schema."
Â
You now have a complete overview of the opportunities available to you, ranging from the need for response time, which encourages us to prepare the analysis by night, and the need for flexibility, which encourages us to do analysis at the last minute. In our case, knowing that DSPPGMREF is much more efficient when analyzing a library rather than one program at a time, analysis is needed by night.
Â
You have seen how—from system commands DSPOBJD, DSPPGMREF, and DSPFD—it is possible to draw an accurate picture of an application.
Â
In these two articles of this series, you also noticed that some information that could have been provided by DSPPGM remains missing because DSPPGM does not offer OUTPUT (*FILE). In the third article, you will find a program, based on several APIs, that can fill the gap.
LATEST COMMENTS
MC Press Online