Unit 8. Accessing the DB2 Database Using RPG IV
Copyright IBM Corp. 1996, 2000 8-1
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-2
Unit Objectives - Accessing the DB2 Database
After completing this unit, you should be able to:
• Describe the properties of a physical and logical file
• List and describe the RPG IV operation codes used for file handling
• Code RPG IV programs that read and update database filesOL868000
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-3
OL868105
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-4
OL868110
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-5
DB File Descriptions
• Define record formats for physical and logical files− Single record format for physical files− One or more record formats for logical files
• Coded on DDS at levels below (shown highest to lowest):− Physical file
— File— Record— Field— Key
− Logical file— File— Record— Join— Field— Key— Selection
OL868115
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-6
OL868120
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-7
OL868125
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-8
OL868130
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-9
DDS Field Reference File
�
A*****************************************************************A** Field Reference PF: DICTIONARYA*****************************************************************A R REFFMT TEXT(′ Field Reference File′ ):A** Fields Used in Purchase Order Line Item File, POLINE_PFA POLQTYOO 5 0 TEXT(′ PO Item Quantity On Order′ )A COLHDG(′ Qty′ ′ Ord′ )A POLITMCOST 5 2 TEXT(′ Item Unit Cost′ )A COLHDG(′ Item′ ′ Unit′ ′ Cost′ )A POLDATREC 8 0 TEXT(′ Date Received′ )A COLHDG(′ Date′ ′ Rec′ ′ YYYYMMDD′ )A POLQTYREC 5 0 TEXT(′ Item Quantity Received′ )A COLHDG(′ Qty′ ′ Rec′ )A POLSTATUS 1 TEXT(′ Blank=On Order, C=CompleteA D=Delete I=Incomplete′ )A COLHDG(′ PO′ ′ Ln′ ′ Sts′ )A VALUES(′ ′ ′ C′ ′ D′ ′ I′ )
OL868135
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-10
DDS - PF/LF
�A* PO line item PF: POLINE_PFA REF(DICTIONARY)A UNIQUEA R POLINE_FMT TEXT(′ PO Line Item Record′ )A PONBR RA ITMNBR RA POLQTYOO RA POLITMCOSTRA POLDATREC RA POLQTYREC RA POLSTATUS RA K PONBRA K ITMNBR
�A* PO Open Line Item LF: POOPNLI_LFA R POLINE_FMT TEXT(′ PO Line Item Record′ )A PFILE(POLINE_PF)A K PONBRA K ITMNBRA O POLSTATUS VALUES(′ D′ ′ C′ )
OL868140
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-11
OL868145
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-12
OL868150
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-13
File Operations
• Input Files− Sequential
— READ— SETLL / SETGT— READE / READP / READPE
− Random— CHAIN
• Output Files− EXCEPT− WRITE
• Update Files− All Operations Above− DELETE− UPDATE
• Key Declaration− KLIST / KFLD
OL868205
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-14
File Related BIFs and Extenders
• BIFs
%Found(FileName) Record Found?
%Open(FileName) File open?
%Eof(FileName) EOF (or BOF) Reached?
%Equal(FileName) Key match to search argument?
%Error Previous I/O failed?
• Operation Extenders
�E� Required with %ErrorOL868207
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-15
OL868210
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-16
Explicit Open and Close
�FFileX IF E Disk UsrOpn ** Explicit open of filexC Open (E) FileX **C If not %ErrorC Read FileXC If not %eofC :C :C :C EndIf ** Explicit close of filexC Close (E) FileX **C ElseC Eval Msg = ′ Problem opening FileX - +C Contact Support′C EndIf **C EVAL *inlr = *onC Return
OL868215
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-17
Initial Open at Program Start
�FFileX IF E Disk ** Implicit open of filexC :C : ** Explicit close of filexC Close (E) FileXC If not %errorC :C EndIfC : ** Explicit open of filexC Open (E) FileXC If not %errorC :C EndIf ** Implicit close of filexC Eval *inlr = *onC Return
OL868220
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-18
OL868225
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-19
Data Can Be Accessed
• Externally Described File
− by File Name
− by Record Format Name
• Program Described File
− by File Name onlyOL868230
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-20
OL868235
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-21
Sequential Processing - READ Example
�
FFileX IF E K Disk** ** Read with file nameC Read FileXC If not %EofC :C :C EndIf ** Read with record format nameC Read Format2C If not %EofC :C :C EndIf
OL868240
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-22
Random Processing - Writing Records by Key
�
** Add records to an existing fileFFileX �U�F �A� E K Disk **C Write (E) FileXFmtC If %ErrorC :
�
** ** Output to a new file **FFileY O E DISK **C Write (E) FileYFmtC If %ErrorC :
OL868245
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-23
Random Processing - WRITE by RRN
�
FFileY O E Disk RecNo(Rrn)
DRrn S 2 0 Inz(0)
C :C Eval Rrn = Rrn + 1 <=== Set RRNC Write (E) FileYFmt <=== Write at RRNC If %errorC :
�
FFileY O E Disk
C :C Write (E) FileyFmt <=== Write at EOFC If %errorC :
OL868250
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-24
OL868255
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-25
Positioning File Cursor - SETLL Example
�
FFileX IF E K Disk
C KEY SetLL FileX C If %equal C Read FileX C If not %eof ** Process the recordC EndIfC EndIf ** The LOVAL/HIVAL figurative constants can be used to ** position the file cursor for Keyed accessC *LOVAL SetLL FileXC If %equalC :
OL868260
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-26
OL868265
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-27
Random Processing - CHAIN by Key Example
�
FFileX IF E K Disk ** Chain with file name **C Key Chain FileXC If %Found(FileX)C :
OL868270
Random Processing - Composite Key
� ** PF: FILEXA REF(DICTIONARY)A UNIQUEA R FILEXFMT TEXT(′ FILEX Demo File′ )A FLDA RA FLDB RA DESCRIPT RA ACTIVE RA FLDC RA K FLDAA K FLDBA K FLDC
� ** Search key is composed of (FLDA/FLDB/FLDC)C KeyArg KListC KFld FldXC KFld FldYC KFld FldZC KeyArg SetLL FileXC If %equalC KeyArg Read FileXFmtC If %Found(FileX) ** Process recordC EndIf
C EndIf
OL868275
Copyright IBM Corp. 1996, 2000 8-28
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-29
OL868280
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-30
Random Processing - CHAIN by RRN Example
�
FFileY IF E Disk** ** Chain with file name **C Rrn Chain FileYC If %Found(FileY)C :
OL868285
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-31
OL868290
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-32
OL868295
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-33
Sequential Processing - READE Example
�FFileX IF E K Disk**
C Key Chain Filex C DoW %Found(FileX) C :
C If CustCode = ′ A′C Eval ActCust = ActCust + 1C ElseC Eval InActCust = InActCust + 1C EndIf
C :C Key ReadE FileXC EndDo
OL8682A0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-34
OL8682A5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-35
READP / SETGT
�
FFileX IF E K Disk** ** Set greater than pointer with file name **C Key SetGT FileX ** Beginning of File?C DoW not %eofC ReadP FileX ** ** Process the record **C EndDo
OL8682B0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-36
OL8682B5
SETGT/READPE
�FFileX IF E K DiskD Key S 5AD Msg S 15A**C *Entry PlistC Parm KeyC Key SetGt FileX ** No record key greater than Key argument. Last group encountered?C If not %Found(FileX)C Key Setll FileX **C If %Found(FileX) ** Must be at last group - reposition to end of group againC Key SetGt FilexC Endif **C Endif ** Read records backwards if group existsC If %Found(FileX)C Key ReadPE FileX ** Read group until beginning of group encountered (EOF).C Dow Not %Eof ** Process recordC Eval Msg = Field1 + ′ / ′ + Field2C Msg Dsply ′ *REQUESTER′C Key ReadPE FileXC Enddo **C EndifC Eval *InLR = *ON
OL8682C0
Copyright IBM Corp. 1996, 2000 8-37
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-38
UPDATE
�
FFileX UF E K Disk**DTotalSales S 5 0 INZ(0)****C Key Chain FileXC If %Found(FileX)C Eval TotalSales = TotalSales + SalesC Update Format1C EndIf **
OL8682C5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-39
DataBase Record Locks
Program can lock only one record per file
Locked:
Read record for update
Released:
Update / write record
Close file
Read another record in same file for updateOL8682D0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-40
OL8682D5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-41
OL8682E0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-42
Read Update File Without Locking Record
�
FVendor_PF UF E K Disk**
C Read (N) Vendor_PF C If Not %Eof
**C ReadE (N) Vendor_PFC If Not %Eof **C ReadP (N) Vendor_PFC If Not %Eof **C ReadPE(N) Vendor_PFC If Not %Eof **C Chain (N) Vendor_PFC If %Found(Vendor_PF)
OL8682E5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-43
Releasing a Locked Record
�
FVendor_PF UF E K Disk**
C KEY Chain Vendor_PF**
C If %Found(Vendor_PF) C : C Unlock Vendor_PF
** C EndIF
OL8682F0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-44
Considerations When Releasing Locked Record
• has record changed since read?
• if so, how to detect record change?
• use timestamps, flags, record image comparison?OL8682F5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-45
DELETE
�
FFileX UF E K Disk**DTotalSales S 5 0**** ** Delete current record based on CHAINC Key Chain FileX **C If %Found(FileX)C Delete FileXC EndIF
** Delete record using file keyC Key Delete FileXC If Not %Found(FileX)
OL8682G0
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-46
OL8682G5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-47
OL8682H5
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-48
CRTPF/CHGPF Parameters
�
DLTPCT (*NONE) ALWDLT* (*YES)(deleted-records-threshold-%) (*NO )
REUSEDLT (*NO) SIZE (#recs incr. #incr)(*YES)
WAITFILE (*IMMED)LVLCHK (*YES) (*CLS)
(*NO) (number-of-seconds)
EXPDATE* (*NONE) WAITRCD (*IMMED)(expiration-date) (*NOMAX)
(number-of-seconds)ALWUPD* (*YES)
(*NO ) SHARE (*YES)(*NO )
�* CRTPF only�
OL868305
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-49
OL86810L
C
op
yrigh
t IBM
Co
rp. 1
99
6, 2
00
08
-50
Unit Summary
Having completed this unit, you should be able to:
• Describe the properties of a physical and logical file
• List and describe the RPG IV operation codes used for file handling
• Code RPG IV programs that read and update database filesOL863ZZZ