Вы находитесь на странице: 1из 50

Unit 8.

Accessing the DB2 Database Using RPG IV

Copyright IBM Corp. 1996, 2000

8-1

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 files
OL868000

Copyright IBM Corp. 1996, 2000 8-2

Copyright IBM Corp. 1996, 2000

8-3

OL868105

Copyright IBM Corp. 1996, 2000

8-4

OL868110

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

Copyright IBM Corp. 1996, 2000 8-5

Copyright IBM Corp. 1996, 2000

8-6

OL868120

Copyright IBM Corp. 1996, 2000

8-7

OL868125

Copyright IBM Corp. 1996, 2000

8-8

OL868130

DDS Field Reference File


A***************************************************************** A** Field Reference PF: DICTIONARY A***************************************************************** A R REFFMT TEXT( Field Reference File ) : A** Fields Used in Purchase Order Line Item File, POLINE_PF A 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=Complete A D=Delete I=Incomplete ) A COLHDG( PO Ln Sts ) A VALUES( C D I )
OL868135

Copyright IBM Corp. 1996, 2000 8-9

DDS - PF/LF
A* A A A A A A A A A A A A
Copyright IBM Corp. 1996, 2000 8-10

PO line item PF:

POLINE_PF REF(DICTIONARY) UNIQUE TEXT( PO Line Item Record )

R POLINE_FMT PONBR R ITMNBR R POLQTYOO R POLITMCOSTR POLDATREC R POLQTYREC R POLSTATUS R K PONBR K ITMNBR

A* A A A A A

PO Open Line Item LF: R POLINE_FMT K PONBR K ITMNBR O POLSTATUS

POOPNLI_LF

TEXT( PO Line Item Record ) PFILE(POLINE_PF) VALUES( D C )


OL868140

Copyright IBM Corp. 1996, 2000

8-11

OL868145

Copyright IBM Corp. 1996, 2000

8-12

OL868150

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

Copyright IBM Corp. 1996, 2000 8-13

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 E Previous I/O failed?


Operation Extenders

Required with %Error


OL868207

Copyright IBM Corp. 1996, 2000 8-14

Copyright IBM Corp. 1996, 2000

8-15

OL868210

Explicit Open and Close


FFileX IF E ** Explicit open of filex C Open (E) ** C If C Read C If C : C : C : C EndIf ** Explicit close of filex C Close (E) ** C Else C Eval C C EndIf ** C EVAL C Return Disk FileX not %Error FileX not %eof UsrOpn

FileX Msg = Problem opening FileX - + Contact Support *inlr = *on

Copyright IBM Corp. 1996, 2000 8-16

OL868215

Initial Open at Program Start


FFileX ** C C ** C C C C C ** C C C C ** C C IF E Disk Implicit open of filex : : Explicit close of filex Close (E) FileX If not %error : EndIf : Explicit open of filex Open (E) FileX If not %error : EndIf Implicit close of filex Eval *inlr = *on Return
OL868220

Copyright IBM Corp. 1996, 2000 8-17

Copyright IBM Corp. 1996, 2000

8-18

OL868225

Data Can Be Accessed


Externally Described File

by File Name by Record Format Name


Program Described File

by File Name only


OL868230

Copyright IBM Corp. 1996, 2000 8-19

Copyright IBM Corp. 1996, 2000

8-20

OL868235

Sequential Processing - READ Example


FFileX ** ** C C C C C ** C C C C C IF E K Disk

Read with file name Read FileX If not %Eof : : EndIf Read with record format name Read Format2 If not %Eof : : EndIf

Copyright IBM Corp. 1996, 2000 8-21

OL868240

Random Processing - Writing Records by Key


** Add records to FFileX U F A ** C C C an existing file E K Disk Write (E) FileXFmt If %Error :

** ** Output to a new file ** FFileY O E DISK ** C Write (E) FileYFmt C If %Error C :


OL868245

Copyright IBM Corp. 1996, 2000 8-22

Random Processing - WRITE by RRN


FFileY DRrn C C C C C
Copyright IBM Corp. 1996, 2000 8-23

E S

Disk 2

RecNo(Rrn) 0 Inz(0)

: Eval Rrn = Rrn + 1 Write (E) FileYFmt If %error :

<=== Set RRN <=== Write at RRN

FFileY C C C C

Disk : Write (E) FileyFmt If %error :

<=== Write at EOF

OL868250

Copyright IBM Corp. 1996, 2000

8-24

OL868255

Positioning File Cursor - SETLL Example


FFileX C C C C KEY IF E K Disk

SetLL FileX If %equal Read FileX If not %eof ** Process the record C EndIf C EndIf ** The LOVAL/HIVAL figurative constants can be used to ** position the file cursor for Keyed access C *LOVAL SetLL FileX C If %equal C :
OL868260

Copyright IBM Corp. 1996, 2000 8-25

Copyright IBM Corp. 1996, 2000

8-26

OL868265

Random Processing - CHAIN by Key Example

FFileX IF E K Disk ** Chain with file name ** C Key Chain FileX C If %Found(FileX) C :

Copyright IBM Corp. 1996, 2000 8-27

OL868270

Random Processing - Composite Key


** PF: FILEX A A A R FILEXFMT A FLDA A FLDB A DESCRIPT A ACTIVE A FLDC A K FLDA A K FLDB A K FLDC REF(DICTIONARY) UNIQUE TEXT( FILEX Demo File ) R R R R R

** Search key C KeyArg C C C C KeyArg C C KeyArg C ** Process record C C

is composed of (FLDA/FLDB/FLDC) KList KFld FldX KFld FldY KFld FldZ SetLL FileX If %equal Read FileXFmt If %Found(FileX) EndIf EndIf
OL868275

Copyright IBM Corp. 1996, 2000

8-28

Copyright IBM Corp. 1996, 2000

8-29

OL868280

Random Processing - CHAIN by RRN Example

FFileY IF E Disk ** ** Chain with file name ** C Rrn Chain FileY C If %Found(FileY) C :

Copyright IBM Corp. 1996, 2000 8-30

OL868285

Copyright IBM Corp. 1996, 2000

8-31

OL868290

Copyright IBM Corp. 1996, 2000

8-32

OL868295

Sequential Processing - READE Example


FFileX ** C Key C C C C C C C C C C IF E Chain DoW : If Eval Else Eval EndIf : ReadE EndDo K Disk Filex %Found(FileX) CustCode = A ActCust = ActCust + 1 InActCust = InActCust + 1

Key

FileX

Copyright IBM Corp. 1996, 2000 8-33

OL8682A0

Copyright IBM Corp. 1996, 2000

8-34

OL8682A5

READP / SETGT
FFileX IF E K Disk ** ** Set greater than pointer with file name ** C Key SetGT FileX ** Beginning of File? C DoW not %eof C ReadP FileX ** ** Process the record ** C EndDo

Copyright IBM Corp. 1996, 2000 8-35

OL8682B0

Copyright IBM Corp. 1996, 2000

8-36

OL8682B5

SETGT/READPE
FFileX IF E K Disk D Key S 5A D Msg S 15A ** C *Entry Plist C Parm Key C 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 again C Key SetGt Filex C Endif ** C Endif ** Read records backwards if group exists C If %Found(FileX) C Key ReadPE FileX ** Read group until beginning of group encountered (EOF). C Dow Not %Eof ** Process record C Eval Msg = Field1 + / + Field2 C Msg Dsply *REQUESTER C Key ReadPE FileX C Enddo ** C Endif C Eval *InLR = *ON
OL8682C0

Copyright IBM Corp. 1996, 2000

8-37

UPDATE
FFileX UF ** DTotalSales ** ** C Key C C C C **
Copyright IBM Corp. 1996, 2000 8-38

E S

K Disk 5 0 INZ(0)

Chain If Eval Update EndIf

FileX %Found(FileX) TotalSales = TotalSales + Sales Format1

OL8682C5

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 update
OL8682D0

Copyright IBM Corp. 1996, 2000 8-39

Copyright IBM Corp. 1996, 2000

8-40

OL8682D5

Copyright IBM Corp. 1996, 2000

8-41

OL8682E0

Read Update File Without Locking Record


FVendor_PF UF ** C C ** C C ** C C ** C C ** C C E Read (N) If K Disk Vendor_PF Not %Eof

ReadE (N) Vendor_PF If Not %Eof ReadP (N) Vendor_PF If Not %Eof ReadPE(N) Vendor_PF If Not %Eof Chain (N) Vendor_PF If %Found(Vendor_PF)

Copyright IBM Corp. 1996, 2000 8-42

OL8682E5

Releasing a Locked Record


FVendor_PF UF ** C KEY ** C C C ** C
Copyright IBM Corp. 1996, 2000 8-43

E Chain If : Unlock EndIF

K Disk Vendor_PF %Found(Vendor_PF) Vendor_PF

OL8682F0

Considerations When Releasing Locked Record

has record changed since read?

if so, how to detect record change?

use timestamps, flags, record image comparison?


OL8682F5

Copyright IBM Corp. 1996, 2000 8-44

DELETE
FFileX UF E K Disk ** DTotalSales S 5 0 ** ** ** Delete current record based on CHAIN C Key Chain FileX ** C If %Found(FileX) C Delete FileX C EndIF
Copyright IBM Corp. 1996, 2000 8-45
OL8682G0

** Delete record using file key C Key Delete FileX C If Not %Found(FileX)

Copyright IBM Corp. 1996, 2000

8-46

OL8682G5

Copyright IBM Corp. 1996, 2000

8-47

OL8682H5

CRTPF/CHGPF Parameters
DLTPCT (*NONE) (deleted-records-threshold-%) ALWDLT* (*YES) (*NO ) (#recs incr. #incr)

REUSEDLT (*NO) (*YES) LVLCHK (*YES) (*NO)

SIZE

WAITFILE (*IMMED) (*CLS) (number-of-seconds) WAITRCD (*IMMED) (*NOMAX) (number-of-seconds) (*YES) (*NO )

EXPDATE* (*NONE) (expiration-date)


Copyright IBM Corp. 1996, 2000 8-48

ALWUPD*

(*YES) (*NO )

SHARE

* CRTPF only
OL868305

Copyright IBM Corp. 1996, 2000

8-49

OL86810L

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 files
OL863ZZZ

Copyright IBM Corp. 1996, 2000 8-50

Вам также может понравиться