I recently had a requirement to retrieve the number of members currently contained in any given file. So I looked at the RTVOBJD CL command, figuring IBM would have made it object-type sensitive by now. Nope, no luck there.
I know DSPFD will give it to me, but I need it in a command or call-level interface. So I looked at RTVMBRD to see if there was an option for *NONE on the member name (and perhaps it would include the member count as a return value). Nope, no luck there.
Then I decided to use the QDBRTVFD API because I knew the information was in there. In fact, it is in the header returned by this API, so knowing all the ins and outs of the API to get, for example, the list of key fields, is unnecessary. Finally, some luck.
The QDBRTVFD API essentially does everything that the DSPFD command does, except produce a list of member names (although, now that I think about it, it probably does that too). You pass it the name of the file whose information you need, and it will return that information. Just make sure you don't use the online InfoCenter API documentation when setting up the return value data structures. The documentation for this API has several inaccuracies that can cause you days of headaches and debugging that goes nowhere.
Use the QSYSINC source member in QRPGLESRC named QDBRTVFD. The data structure named QDBQ25 is the one that contains the header information. That's all you need to retrieve the file's attributes, including the current member count.
The QDBRTVD API is a program that can be prototyped as follows:
D szRtnBufrer 32765A OPTIONS(*VARSIZE)
D nBuffLen 10I 0 Const
D rtnFileName 20A
D szAPIFmt 8A Const
D szQualFile 20A Const
D szRcdFmt 10A Const
D bOverride 1A Const
D szRmtLocSys 10A Const
D szIntFileType 10A Const
D api_error LikeDS(QUSEC_T)
D OPTIONS(*VARSIZE)
The first parameter is the return buffer—a data structure that receives the return value. The data structure needs to be at least 400 bytes, but 700 is the minimum the API likes to return.
The second parameter is the length of the data structure specified on the first parameter. The API will return up to the value specified on this parameter. So normally you would pass in %size(myReturnDS) for this parameter.
The third parameter is a variable that receives the name of the actual file whose information is returned. This file name may be different from that specified on the fifth parameter if, for example, the library name was specified as *LIBL or *CURLIB or if overrides are applied to the file and *ON is specified for the bOverride parameter.
The fourth parameter is the API's format identifier. This tells the API what kind of information to return. In our example, we only need the basic file information, so format ID 'FILD0100' is always specified.
The fifth parameter is the name of the file whose information is going to be retrieved. The first 10 positions contain the file name. The second 10 positions contain the library name, *LIBL, or *CURLIB.
The sixth parameter is the name of the record format of the file whose information is being retrieved. For our example, we are not using this parameter, so specify either blanks or '*FIRST'. The parameter is ignored when the API format (fourth parameter) is 'FILD0100'.
The seventh parameter is an override processing flag. If specified as *ON or '1', then any overrides that apply to the file specified on the fifth parameter are taken into consideration when retrieving the file information. If *OFF or '0' is specified for this parameter, then overrides are ignored.
The eighth parameter is the local or remote file indicator. Specify *LCL if the file is located on the iSeries that is calling the API, specify *RMT if the file is on a remote system, such as a DDM file, or specify *FILETYPE if you want the API to look at the file description to determine where the file is located.
The ninth parameter is one of those parameters that isn't used very often, and in our example it is ignored. Specify either '*INT' or '*EXT' as the result will be the same.
The final parameter is the standard API error data structure. It is based on a data structure named QUSEC_T, which I created for my upcoming book RPG TNT: 101 Dynamite Tips and Techniques for RPG Programmers. The source code for QUSEC_T follows:
D bytesProv 10I 0 Inz(%size(qusec_t))
D bytesProvided 10I 0 Overlay(bytesProv)
D bytes_Provided...
D 10I 0 Overlay(bytesProv)
D bytesAvail 10I 0 Inz
D bytes_Avail 10I 0 Overlay(bytesAvail)
D bytes_Returned...
D 10I 0 Overlay(bytesAvail)
D cpfmsgID 7A
D reserved 1A Inz(X'00')
D exceptionData 64A Inz(*ALLX'00')
The first parameter of the QDBRTVFD API is a data structure. Although it would be beneficial to use a user space for the first parameter, it is not necessary in our example. Instead, the follow data structure can be specified to receive all the information necessary to retrieve the current number of members along with a lot of other information.
D bytes_returned 10I 0
D bytes_avail 10I 0
** Need to use %BITAND() to test the fileAttr subfield
D fileAttr 2A
D reserved1 4A
D dataMbrs 5I 0
D keySeqAttr LikeDS(Qdb_Qdbfkdat_t)
D pubAuth 10A
D prefStorageUnit...
D 3U 0
D maxMbrs 5I 0
D waitFile 5I 0
D FRCWRITE 5I 0
D curMbrCount 5I 0
D accPathPageSize...
D 5I 0
D reserved2 7A
D waitRCD 5I 0
D qaaf 1A
D rcdFmtCount 5I 0
D qdbfhfl2 2A
D OSVRM 5I 0
D qaaf2 2A
D fileCRTDate 13A
D fileText 52A
D reserved3 2A Overlay(fileText)
D textDesc 50A Overlay(fileText:*NEXT)
D reserved4 13A
D srcFileName 30A
D srcFile 10A Overlay(srcFileName)
D srcLib 10A Overlay(srcFileName:*NEXT)
D srcMbr 10A Overlay(srcFileName:*NEXT)
D recovery 1A
D reserved5 23A
D CCSID 5U 0
D asp 2A
D cpxObj 1A
D fldCount 5I 0
D reserved6 76A
D dictOffset 10I 0
D reserved7 14A
D keyLength 5I 0
D rcdLength 5I 0
D reserved8 8A
D keyFieldCount 5I 0
D scopeOffset 10I 0
D reserved9 8A
D altColOffset 10I 0
D reserved10 4A
D accPathType 2A
D fileVRM 6A
D reserved11 20A
D pfAttrOffset 10I 0
D lfAttrOffset 10I 0
D sortSeq 6A
D ss_flag 1A Overlay(sortseq)
D LangID 3A Overlay(sortseq:*NEXT)
D cntry 2A Overlay(sortseq:*NEXT)
D jrnOffset 10I 0
D encVectCount 10U 0
D reserved12 14A
To use this data structure, declare a new one and specify it as the first parameter on a call to QDBRTVFD, as follows:
/free
QDBRTVFD(myFileInfo: %len(myFileInfo): rtnFileName :
'FILD0100' : inFile : '*FIRST' :
'0': '*FILETYPE' :'*INT': apiError );
/end-free
Note that the QDBQ25_T data structure has a KeySeqAttr subfield. This field is a nested data structure. If you are on OS/400 V5R2 or later, this syntax will work. If not, you need to create a nested data structure using the OVERLAY keyword and remove the LIKEDS keyword. The data structure on which this subfield is based follows:
D Qdb_Qdbfkdat_t DS Qualified Based(NULL)
D Qdbfknum 5I 0
D Qdbfkmxl 5I 0
D Qdbfkflg 1A
D Qdbfkfdm 1A
D reserved 8A
Once the API was set up, I wanted to create a Retrieve File Description (RTVFILED) CL command so I could easily retrieve the member count from within CL. With today's CL capabilities, I could call the API directly and be done with it, but I wanted a nicely wrapped up CL command definition. The source for this command definition follows:
/* Command processing program is: RTVFILED */
© 2006 Robert Cozzi, Jr. – From "RPG TNT"
PARM KWD(FILE) TYPE(QUAL1) MIN(1) PROMPT('File')
QUAL1: QUAL TYPE(*NAME) MIN(1) EXPR(*YES)
QUAL TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL) +
(*CURLIB)) EXPR(*YES) PROMPT('Library')
PARM KWD(RTNRCDLEN) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for max record length')
PARM KWD(RTNKEYLEN) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for key length')
PARM KWD(RTNKEYCNT) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for key field count')
PARM KWD(RTNMBRCNT) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for member count')
PARM KWD(RTNFLDCNT) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for field count')
PARM KWD(RTNCCSID) TYPE(*INT4) RTNVAL(*YES) +
PROMPT('CL Var for file CCSID')
As you can see from the Command Definition source, I chose to allow not only the current member count to be returned, but also the record length, key length, key field count, field count, and CCSID of the file. A lot more information can be returned, so you could easily modify the above command to return whatever other information you need.
To compile the command definition source, be sure to specify ALLOW(*IPGM *BPGM *IMOD *BMOD); otherwise, it won't compile. I'm not sure why, after 20+ years of hearing us complain, IBM still requires you to enter these ALLOW options, when they are in fact required for command parameters that include RTNVAL(*YES). Why not just default that way? And IBM wonders why most people don't bother creating commands.
The command processing program is interesting. It uses all the latest and greatest features of RPG IV, such as prototyped entry-plist, free-format, nested data structures, and data structure templates.
H Copyright('© 2006 – Robert Cozzi, Jr.')
D RTVFILED PR extpgm('RTVFILED')
D inFile LikeDS(FileLib_T)
D rtnRcdLen 10I 0
D rtnKeyLen 10I 0
D rtnKeyFldCount...
D 10I 0
D rtnMbrCount 10I 0
D rtnFldCount 10I 0
D RTVFILED PI
D inFile LikeDS(FileLib_T)
D rtnRcdLen 10I 0
D rtnKeyLen 10I 0
D rtnKeyFldCount...
D 10I 0
D rtnMbrCount 10I 0
D rtnFldCount 10I 0
// Remember, this is included in above article.
/include rpgtnt/qcpysrc,rpgtnt
D fileLib_T DS Qualified Based(NULL)
D file 10A
D library 10A
D myFileInfo DS LikeDS(QDBQ25_T)
D rtnFileName DS LikeDS(fileLib_T)
D apiError DS LikeDS(QUSEC_T)
C eval *INLR = *ON
/free
clear apiError;
apiError.bytesProvided = %len(apiError);
myFileInfo = *ALLX'00';
QDBRTVFD(myFileInfo: %len(myFileInfo): rtnFileName :
'FILD0100' : inFile : '*FIRST' :
'0': '*FILETYPE' :'*INT': apiError );
if (apiError.bytes_Returned=0);
if %parms() >=2 and %addr(rtnRcdLen) <> *NULL;
rtnRcdLen = myFileInfo.rcdLength;
endif;
if %parms() >=3 and %addr(rtnKeyLen) <> *NULL;
rtnKeyLen = myFileInfo.keyLength;
endif;
if %parms() >=4 and %addr(rtnKeyFldCount) <> *NULL;
rtnKeyFldCount = myFileInfo.keyFieldCount;
endif;
if %parms() >=5 and %addr(rtnMbrCount) <> *NULL;
rtnMbrCount = myFileInfo.curMbrCount;
endif;
if %parms() >=6 and %addr(rtnFldCount) <> *NULL;
rtnFldCount= myFileInfo.fldCount;
endif;
endif;
return;
/end-free
Once the RPG IV program and the command are created, they can be used from with CL as follows:
DCL VAR(&RCDLEN) TYPE(*INT) LEN(4)
DCL VAR(&KEYLEN) TYPE(*INT) LEN(4)
DCL VAR(&KEYCNT) TYPE(*INT) LEN(4)
DCL VAR(&MBRCNT) TYPE(*INT) LEN(4)
DCL VAR(&FLDCNT) TYPE(*INT) LEN(4)
RTVFD FILE(MYLIBR/CUSTMAST) RTNRCDLEN(&RCDLEN) +
RTNKEYLEN(&KEYLEN) RTNMBRCNT(&MBRCNT) +
RTNFLDCNT(&FLDCNT)
ENDPGM: ENDPGM
As you can see, CL supports integer data types. If you don't have a release with integer data types, you can use a four-position character field and then convert that character data to numeric as follows:
DCL VAR(&MBRCNT) TYPE(*DEC) LEN(5 0)
RTVFD FILE(MYLIBR/CUSTMAST) RTNMBRCNT(&MBRCNT)
RTNFLDCNT(&FLDCNT)
chgvar var(&mbrcnt) value(%bin(mbrcnta))
Hopefully, everyone will soon be on V5R3 so we're all on a good, consistent release.
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online