Query/400 is a flexible piece of software commonly used to produce ad hoc reports and temporary extraction files for batch jobs. Adding to Query/400s flexibility is its ease of use. With minimal training, users can produce their own reports and views of data. This user independence is a blessing in terms of freeing your IT staff from creating simple reports and inquiries but becomes a burden when these queries abend because of file changes such as those made by AS/400 shops to prepare for Y2K. Joe in accounting can crunch the monthly numbers at will, but when you change the general receivables file, those queries he has built over it feel the pain. Any file change necessitates a refresh before a Query/400 query can be executed with the Run Query (RUNQRY) command. Without a refresh, using RUNQRY results in error QRY1058 (Level for file XXX in YYY does not match query.)
We have developed a utility that builds a cross-reference file of queries and the files they read. We call it Build Query Cross-reference (BLDQRYXREF). Not only can it help you prepare for Y2K, but it is also useful for much more.
All you need to understand about BLDQRYXREF is its command interface and the physical file it loads, QRYXREF. Besides these objects, BLDQRYXREF also has three CL programs and one RPG III program. BLDQRYXREF loads information about queries and the files they read into physical file QRYXREF (see Figure 1). You can place this file into any library you want, but make sure that library is on the library list when you run BLDQRYXREF.
The Quest for Queries
BLDQRYXREF uses two parameters, as shown in Figure 2. The first parameter, which specifies the libraries to search for queries, can be a list of up to 50 library names or one of these special values: *LIBL, *USRLIBL, *CURLIB, *ALL, or *ALLUSR. The second parameter lets you tell BLDQRYXREF whether or not to clear the QRYXREF physical file before loading it.
BLDQRYXREF executes CL program QRY003CL, which extracts library names from the list passed by the command (see Figure 3). Each time it extracts a library name, it calls CL program QRY004CL to retrieve the source code of all queries in a single library or in all the libraries to which a special value such as *ALLUSR refers (see Figure 4). QRY004CL also uses the Display Object Description (DSPOBJD) command to find query objects. For each query found, it executes Retrieve Query Management Query
(RTVQMQRY) to generate source code for the query and place that source code into a new member of a temporary source file. Members are given system-generated names in case queries in different libraries have the same name, and the name of the query and name of the library in which it resides are stored in the source members text description for later retrieval.
After QRY003CL has processed the last library in the parameter list, it issues an override to the temporary source file so RPG III program QRY004RG can process all the members, not just the first one. QRY003CL calls QRY004RG, which reads the temporary source file members, searching for lines beginning with two blanks and the word FROM. Such source records indicate the beginning of the list of files used by a query. If a query reads more than one file, the subsequent lines begin with blanks and contain other file names. Each time this RPG program finds a record containing a file name, it writes a record to file QRYXREF (see Figure 5). Because QRYXREF contains other information not found in the query source code, QRY004RG calls CL program QRY005CL, which is shown in Figure 6. QRY005CL executes Retrieve Member Description (RTVMBRD) for this additional information.
Unfortunately, this utility cannot detect outfiles produced by Query/400 queries. We wish it could, but the source code produced by RTVQMQRY does not contain that information.
BLDQRYXREF has completed its task, and physical file QRYXREF now contains a list of files read by Query/400 queries from selected libraries. What you do with this information is up to you. Use relational tools, such as Query and SQL, or write programs to read QRYXREF, and coordinate a query refresh with the file information when you roll file changes into production.
Clean Up the Quiescent
Many queries are used once and forgotten. These derelict objects occupy valuable disk space. One shop had more than 12,000 Query/400 queries cluttering up its system. Pay attention to the last used date field in your QYRXREF file. Delete obsolete queries and consolidate redundant queries into one version.
This utility can manage not only your Y2K efforts but also any other file changes. With BLDQRYXREF, managing Query/400 queries is a breeze.
*===============================================================
* To compile:
*
* CRTPF FILE(XXX/QRYXREF) SRCFILE(XXX/QDDSSRC)
*
*===============================================================
A R QRYXREFR
A QNAME 10 COLHDG(' ' 'Query' 'name')
A QLIB 10 COLHDG(' ' 'Query' 'library')
A FNAME 10 COLHDG(' ' 'File' 'name')
A FLIB 10 COLHDG(' ' 'File' 'library')
A CRTUSR 10 COLHDG(' ' 'Created' 'by')
A CRTDAT 13 COLHDG(' ' 'Date' 'created')
A USEDAT 7 COLHDG('Date' 'last' 'used')
A USECT 5 0 COLHDG(' Number' 'of times' +
' used') /*==================================================================*/
/* To compile: */
/* */
/* CRTCMD CMD(XXX/BLDQRYXREF) PGM(XXX/QRY003CL) + */
/* SRCFILE(XXX/QCMDSRC) */
/* */
/*==================================================================*/
CMD PROMPT('Build query cross reference')
Figure 1: QRYXREF stores information about queries and the files they use.
PARM KWD(LIB) TYPE(*NAME) LEN(10) SNGVAL((*LIBL) +
(*USRLIBL) (*CURLIB) (*ALL) (*ALLUSR)) +
MIN(1) MAX(50) PROMPT('Libraries to search')
PARM KWD(CLEAR) TYPE(*LGL) RSTD(*YES) DFT(*NO) +
SPCVAL((*NO '0') (*YES '1')) +
CHOICE('*YES, *NO') PROMPT('Clear cross +
reference file?') /*==================================================================*/
/* Load the query cross reference file for a list of libraries. */
/*==================================================================*/
/* To compile: */
/* */
/* CRTCLPGM PGM(XXX/QRY003CL) SRCFILE(XXX/QCLSRC) */
/* */
/*==================================================================*/
PGM PARM(&LISTOFLIBS &CLEARXREF)
DCL VAR(&LISTOFLIBS) TYPE(*CHAR) LEN(502)
DCL VAR(&LIB) TYPE(*CHAR) LEN(10)
DCL VAR(&CLEARXREF) TYPE(*LGL)
DCL VAR(&QUERYCOUNT) TYPE(*DEC) LEN(9)
DCL VAR(&CURMBR) TYPE(*CHAR) LEN(10) VALUE(Q)
DCL VAR(&LIBCOUNT) TYPE(*DEC) LEN(5)
DCL VAR(&PROCESSED) TYPE(*DEC) LEN(5)
DCL VAR(&OFFSET) TYPE(*DEC) LEN(5) VALUE(3)
DCL &ERRBYTES *CHAR 4 VALUE(X'00000000')
DCL &ERROR *LGL VALUE('0')
DCL &MSGKEY *CHAR 4
DCL &MSGTYP *CHAR 10 VALUE('*DIAG')
DCL &MSGTYPCTR *CHAR 4 VALUE(X'00000001')
DCL &PGMMSGQ *CHAR 10 VALUE('*')
DCL &STKCTR *CHAR 4 VALUE(X'00000001')
MONMSG MSGID(CPF0000) EXEC(GOTO ERRPROC)
/* Process the next library in the list in the &LIB parameter */
CHGVAR VAR(&LIBCOUNT) VALUE(%BIN(&LISTOFLIBS 1 2))
NEXT_LIB:
IF COND(&PROCESSED *LT &LIBCOUNT) THEN(DO)
CHGVAR VAR(&LIB) VALUE(%SST(&LISTOFLIBS &OFFSET 10))
CALL PGM(QRY004CL) PARM(&LIB &QUERYCOUNT)
CHGVAR VAR(&PROCESSED) VALUE(&PROCESSED + 1)
CHGVAR VAR(&OFFSET) VALUE(&OFFSET + 10)
GOTO CMDLBL(NEXT_LIB)
ENDDO
/* Write to the cross reference file, QRYXREF */
IF COND(&CLEARXREF) THEN(CLRPFM FILE(QRYXREF))
OVRDBF FILE(QRYSRC) TOFILE(QTEMP/QRYSRC) MBR(*ALL)
CALL PGM(QRY003RG)
DLTOVR FILE(QRYSRC)
RETURN
/*==================================================================*/
/* Error processing routine */
/*==================================================================*/
ERRPROC:
IF COND(&ERROR) THEN(GOTO ERRDONE)
ELSE CMD(CHGVAR VAR(&ERROR) VALUE('1'))
/* Move all *DIAG messages to previous program queue */
CALL PGM(QMHMOVPM) PARM(&MSGKEY &MSGTYP +
&MSGTYPCTR &PGMMSGQ &STKCTR &ERRBYTES)
/* Resend last *ESCAPE message */
ERRDONE:
CALL PGM(QMHRSNEM) PARM(&MSGKEY &ERRBYTES)
MONMSG MSGID(CPF0000) EXEC(DO)
SNDPGMMSG MSGID(CPF3CF2) MSGF(QCPFMSG) +
MSGDTA('QMHRSNEM') MSGTYPE(*ESCAPE)
MONMSG MSGID(CPF0000)
ENDDO
ENDPGM /*==================================================================*/
/* Load the query reference file with information about queries */
Figure 2: BLDQRYXREF is easy to use because it has only two parameters.
Figure 3: Program QRY003CL processes the list of selected libraries.
/* from one library. */
/*==================================================================*/
/* To compile: */
/* */
/* CRTCLPGM PGM(XXX/QRY004CL) SRCFILE(XXX/QCLSRC) */
/* */
/*==================================================================*/
PGM PARM(&LIB &QUERYCOUNT)
DCL VAR(&LIB) TYPE(*CHAR) LEN(10)
DCL VAR(&QUERYCOUNT) TYPE(*DEC) LEN(9)
DCL VAR(&CURMBR) TYPE(*CHAR) LEN(10) VALUE(Q)
DCLF FILE(QADSPOBJ)
DCL &ERRBYTES *CHAR 4 VALUE(X'00000000')
DCL &ERROR *LGL VALUE('0')
DCL &MSGKEY *CHAR 4
DCL &MSGTYP *CHAR 10 VALUE('*DIAG')
DCL &MSGTYPCTR *CHAR 4 VALUE(X'00000001')
DCL &PGMMSGQ *CHAR 10 VALUE('*')
DCL &STKCTR *CHAR 4 VALUE(X'00000001')
MONMSG MSGID(CPF0000) EXEC(GOTO ERRPROC)
CRTSRCPF FILE(QTEMP/QRYSRC) TEXT('Source for +
Query/400 Files')
MONMSG MSGID(CPF7302)
DSPOBJD OBJ(&LIB/*ALL) OBJTYPE(*QRYDFN) +
OUTPUT(*OUTFILE) OUTFILE(QTEMP/QADSPOBJ)
MONMSG MSGID(CPF0000)
OVRDBF FILE(QADSPOBJ) TOFILE(QTEMP/QADSPOBJ) +
SHARE(*YES)
RCVF:
RCVF RCDFMT(QLIDOBJD)
MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF))
CHGVAR VAR(&QUERYCOUNT) VALUE(&QUERYCOUNT + 1)
CHGVAR VAR(%SST(&CURMBR 2 9)) VALUE(&QUERYCOUNT)
RTVQMQRY QMQRY(&ODLBNM/&ODOBNM) SRCFILE(QTEMP/QRYSRC) +
SRCMBR(&CURMBR) ALWQRYDFN(*YES)
CHGPFM FILE(QTEMP/QRYSRC) MBR(&CURMBR) +
TEXT(&ODLBNM *CAT &ODOBNM)
MONMSG MSGID(CPF0000)
GOTO CMDLBL(RCVF)
EOF:
DLTOVR FILE(QADSPOBJ)
RETURN
/*==================================================================*/
/* Error processing routine */
/*==================================================================*/
ERRPROC:
IF COND(&ERROR) THEN(GOTO ERRDONE)
ELSE CMD(CHGVAR VAR(&ERROR) VALUE('1'))
/* Move all *DIAG messages to previous program queue */
CALL PGM(QMHMOVPM) PARM(&MSGKEY &MSGTYP +
&MSGTYPCTR &PGMMSGQ &STKCTR &ERRBYTES)
/* Resend last *ESCAPE message */
ERRDONE:
CALL PGM(QMHRSNEM) PARM(&MSGKEY &ERRBYTES)
MONMSG MSGID(CPF0000) EXEC(DO)
SNDPGMMSG MSGID(CPF3CF2) MSGF(QCPFMSG) +
MSGDTA('QMHRSNEM') MSGTYPE(*ESCAPE)
MONMSG MSGID(CPF0000)
ENDDO
ENDPGM
*===============================================================
* Extract file & library names from query source and load the
* query cross reference file.
*===============================================================
* To compile:
*
* CRTRPGPGM PGM(XXX/QRY003RG) SRCFILE(XXX/QRPGSRC)
*
*===============================================================
*
FQRYSRC IF F 92 DISK KINFDS INFDS
FQRYXREF O E DISK
Figure 4: Program QRY004CL finds queries and retrieves their source code.
ISRCREC DS
I 13 92 SRCDTA
I 13 22 RECID
IINFDS DS
I 129 138 MBRNAM
C MOVE *OFF FROM 1
C EXSR READ
C EOF DOWEQ*OFF
C* If new member, retrieve member info
C MBRNAM IFNE MBRSAV
C CALL 'QRY005CL'
C PARM MBRNAM
C PARM QLIB
C PARM QNAME
C PARM CRTDAT
C PARM CRTUSR
C PARM USECT
C PARM USEDAT
C MOVELMBRNAM MBRSAV
C *LIKE DEFN MBRNAM MBRSAV
C ENDIF
C SELEC
C RECID WHEQ ' FROM '
C MOVE *ON FROM
C RECID WHNE *BLANKS
C MOVE *OFF FROM
C ENDSL
C FROM IFEQ *ON
C EXSR GETFIL
C WRITEQRYXREFR
C ENDIF
C EXSR READ
C ENDDO
C MOVE *ON *INLR
***********
C READ BEGSR
C*
C READ QRYSRC SRCREC 91
C MOVE *IN91 EOF 1
C*
C ENDSR
***********
* Extract the file name & library name from the query source
***********
C GETFIL BEGSR
C*
C* W = position of first character of library/file name
C* X = position of slash separator
C* Y = position of first character of file name
C* Z = position of first blank after library/file name
C*
C MOVE *ZERO W 30
C MOVE *ZERO X 30
C MOVE *ZERO Y 30
C MOVE *ZERO Z 30
C MOVE *BLANKS FNAME
C MOVE *BLANKS FLIB
C*
C ' ' CHECKSRCDTA:9 W
C W IFGT *ZERO
C '/' SCAN SRCDTA:W X
C ' ' SCAN SRCDTA:W Z
C ENDIF
C*
C X IFGT *ZERO SLASH FOUND
C X SUB W LEN 30
C LEN SUBSTSRCDTA:W FLIB
C X ADD 1 Y
C Z SUB Y LEN
C LEN SUBSTSRCDTA:Y FNAME
C ELSE
C Z SUB W LEN
C LEN SUBSTSRCDTA:W FNAME
C ENDIF
C*
C ENDSR /*==================================================================*/
/* Retrieve member info for a query definition. The query name and */
/* library name are stored in the text field of the source member. */
/*==================================================================*/
/* To compile: */
/* */
/* CRTCLPGM PGM(XXX/QRY005CL) SRCFILE(XXX/QCLSRC) */
/* */
/*==================================================================*/
Figure 5: Program QRY003RG reads query source and writes to the cross-reference file.
PGM PARM(&MEMBER &QRYLIB &QRYNAME &CRTDATE &CRTUSER &USECT +
&LASTUSEDAT)
DCL VAR(&MEMBER) TYPE(*CHAR) LEN(10)
DCL VAR(&QRYLIB) TYPE(*CHAR) LEN(10)
DCL VAR(&QRYNAME) TYPE(*CHAR) LEN(10)
DCL VAR(&CRTDATE) TYPE(*CHAR) LEN(13)
DCL VAR(&CRTUSER) TYPE(*CHAR) LEN(10)
DCL VAR(&USECT) TYPE(*DEC) LEN(5)
DCL VAR(&LASTUSEDAT) TYPE(*CHAR) LEN(7)
DCL VAR(&TEXT) TYPE(*CHAR) LEN(50)
MONMSG MSGID(CPF0000)
CHGVAR VAR(&QRYLIB) VALUE(' ')
CHGVAR VAR(&QRYNAME) VALUE(' ')
CHGVAR VAR(&CRTDATE) VALUE(' ')
CHGVAR VAR(&CRTUSER) VALUE(' ')
CHGVAR VAR(&USECT) VALUE(0)
CHGVAR VAR(&LASTUSEDAT) VALUE(' ')
RTVMBRD FILE(QTEMP/QRYSRC) MBR(&MEMBER) TEXT(&TEXT)
CHGVAR VAR(&QRYLIB) VALUE(%SST(&TEXT 1 10))
CHGVAR VAR(&QRYNAME) VALUE(%SST(&TEXT 11 10))
RTVOBJD OBJ(&QRYLIB/&QRYNAME) OBJTYPE(*QRYDFN) +
CRTDATE(&CRTDATE) CRTUSER(&CRTUSER) +
USEDATE(&LASTUSEDAT) USECOUNT(&USECT)
RETURN
ENDPGM
LATEST COMMENTS
MC Press Online