Sunday 25 March 2012


Data-Area  Datastructures Operations

The data area operations are:
The IN and OUT operations allow you to retrieve and write one or all data areas in a program, depending on the factor 2 entry.
The IN and OUT operations also allow you to control the locking or unlocking of a data area. When a data area is locked, it can be read but not updated by other programs.
The following lock states are used:
  • For an IN operation with *LOCK specified, an exclusive allow read lock state is placed on the data area.
  • For an OUT or UNLCK operation, the exclusive allow read lock state is released.
During the actual transfer of data into or out of a data area, there is a system-internal lock on the data area. If several users are contending for the same data area, a user may get an error message indicating that the data area is not available.
Remember the following when using the IN, OUT, and UNLCK operations:
  • A data-area operation cannot be done on a data area that is not defined to the operating system.
  • Before the IN, OUT, and UNLCK operations can be done on a data area, you must specify that data area in the result field of an *NAMVAR DEFN statement. (For further information on the DEFN statement, see DEFN (Field Definition).)
  • The data-area operations can be done on a data-area data structure that is implicitly retrieved only if the data-area data structure name is specified in the result field of an *NAMVAR DEFN statement.
  • A locked data area cannot be updated or locked by another RPG/400 program; however, the data area can be retrieved in your own program by an IN operation with factor 1 blank.
  • A data-area name cannot be the name of a multiple-occurrence data structure, an input record field, an array, an array element, or a table.
  • A data area cannot be the subfield of a multiple occurrence data structure, a data-area data structure, a program-status data structure, a file-information data structure (INFDS), or a data structure that appears on an *NAMVAR DEFN statement.
A data structure defined with a U in position 18 of the input specifications form indicates that the data structure is a data area. The data area is automatically read and locked at program initialization time, and the contents of the data structure are written to the data area when the program ends with LR on.
Specify *LDA in factor 2 of a *NAMVAR DEFN statement to define the LDA data structure.
Use the *NAMVAR DEFN operation with *PDA in factor 2 to define the name in the result field as the PDA data area. The result field follows the current conventions for *NAMVAR DEFN.

RPG IV (RPG ILE) and AS400 Data Area Programming

Data Area processing in ILE RPG program
All examples in this section of article use a data area created by the following command
CRTDTAARA DTAARA(EMP_INFO) TYPE(*CHAR) LEN(10) VALUE('01TutoIndi')
To use a data area in an RPG IV program we need to first declare the data area as a variable in the source code of the program. This variable can either be a stand-alone or a data structure. In RPG IV a data area can be declared in the following two ways.
  1. One line declaration ( RPG IV method of declaring a data area; Recommended)
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++
    DW@DtaAra         S             10A   DTAARA(EMP_INFO)
    
    
    Notice that the data type of the stand alone variable should be same as the data type of the data area. However, if you need to declare a data area which has multiple data formats you should declare it as a data structure. The above dclaration can be made as
    
    DW@DtaAra         DS                  DTAARA(EMP_INFO)
    DW@Emp#                   1      2  0                 
    DW@EmpName                3     10                    
    
    
  2. Declaration Using Like Definition (Old method)
    This section is given for the sake of completion of this article as I've found such declaration at several places in RPG III and even RPG IV, both types of programs!
    This method of declaration is usually a two step method.
    Firstly, an RPG variable is declared. A stand alone variable is declared in the C Spec result section or D-Spec and a data structure is defined in the I Specification or D Spec in RPG IV.
    After that, The data area is declared in the C-Spec using the opcode "Define" as given below. Notice that the first factor of this opcode is *DTAARA which tells the compiler that the factor 2 is a data area and look for it in the library list when compiling.
    
    C     *DTAARA       Define    EMP_INFO      W@DtaAra
    
    Here, W@DtaAra can be a data structure or a stand alone variable. If it's a stand alone variable, it can be defined here itself.
    Note:- The idea is to declare variable and then the Data area. How you achieve this is upto you. Due to this flexibility, I have found several combinations of declaration of data area.

  3. Read a Data Area in ILE RPG (IV)
    Reading a data area in an RPG IV program is a two step process. The first step is to read a data area using the opcode IN as given blow.
    
    C     *Lock         In        W@Dtaara
    
    As soon as this statement is executed, the variable/s is/are populated with the data area value. Here it's important to note that the first factor '*LOCK' is required to maintain data integrity and hence can not be omitted. The exception to this rule is the local data area where no lock is required.
    The second step would to unlock the data area. If you are using a data area for read only purpose, you need to Unlock the data area using the opcode UNLOCK as given blow.
    
    CL0N01Factor1+++++++Opcode&ExtFactor2++
    C                   Unlock    W@DtaAra 
    
    Other way to unlock a data area will be to update it. To update a data area we use the opcode 'OUT'. The opcode OUT updates the date area with the current value of the RPG variable/Data structure. The syntax to update the data area is as given below.
    
    CL0N01Factor1+++++++Opcode&ExtFactor2+
    C                   Out       W@DtaAra
    
    The OUT opcode requires a lock on the data area except in case of local data area where it's not required to take a lock beforehand.
    That's all about data area processing in RPG IV. Now refer the examples given below to become more intimate with data area processing in RPG programs.

    Program using Read only Data Area
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++
    DW@DtaAra         DS                  DTAARA(EMP_INFO)
    DW@Emp#                   1      2  0                 
    DW@EmpName                3     10                    
     **                                                   
    CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++
    C     *Lock         In        W@Dtaara                
    C     W@Emp#        Dsply                             
    C     W@EmpName     Dsply                             
    C                   Unlock    W@DtaAra                
     **                                                   
    C                   Return                                              
    
    Output:-
    DSPLY 1
    DSPLY TutoIndi

    Program Updating Data Area
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++
    DW@DtaAra         DS                  DTAARA(EMP_INFO) 
    DW@Emp#                   1      2  0                  
    DW@EmpName                3     10                     
     **                                                    
    CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++++
    C     *Lock         In        W@Dtaara                 
    C     W@Emp#        Dsply                              
    C     W@EmpName     Dsply                              
     **                                                    
    C                   Eval      W@Emp# +=1               
    C                   Out       W@DtaAra                 
     **                                                    
    C     W@Emp#        Dsply                              
    C     W@EmpName     Dsply                              
    C                   Return                                                      
    
    Output:-
    DSPLY 1
    DSPLY TutoIndi
    DSPLY 2
    DSPLY TutoIndi
    The above program shows how to update specific portion of a data area. 'OUT' opcode in itself updates all values of the data area. However, with judicious use of data structure we accomplish partial update.

    Summary
    The Data Area article has been quite a long one hence a summary is given below.
    1. Data area is an object. It stores data but it's not a physical file.
    2. CRTDTAARA, DSPDTAARA, CHGDTAARA commands create, display and change a data area respectively. These commands are run from command line.
    3. RTVDTAARA command return the current value of data area in a similar data type CL variable.
    4. We can use specific portion of a data area using substring optional feature of RTVDTAARA and CHGDTAARA commands.
    5. Data area should be defined in the D-Spec in an RPG program.
    6. Reading a data area in an RPG program is a two step process (Except local data area). Read with lock and Unlock.
    7. Data area is read using the opcode IN with *LOCK as factor 1. *LOCK is required if data area is not a local data area.
    8. Data area can be unlocked by UNLOCK or OUT opcodes. Out opcode updates and unlocks the data area.


No comments:

Post a Comment