14
Thu, Nov
5 New Articles

Retrieve File Description

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

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 QDBRTVFD        PR                  extPgm('QDBRTVFD')
     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 QUSEC_T         DS                  Qualified
     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.

     DQDBQ25_T         DS                  Qualified Based(NULL)
     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                         1
     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:

     D myFileInfo      DS                  LikeDS(qdbq25_T) Inz
      /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:

      // Used as a template for the keySeqAttr subfield
     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:

RTVFILED:   CMD        PROMPT('Retrieve File Description')
             /*         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 DFTACTGRP(*NO) BNDDIR('QC2LE') OPTION(*SRCSTMT)
     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:

TESTRTV:     PGM
             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(&MBRCNTA) TYPE(*CHAR) LEN(4)
             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.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

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: