RPG Programming

Purpose of the language

ภาษา RPG ออกแบบมาเพื่อการพัฒนาโปรแกรมใช้ในเชิงธุรกิจ และตัว AS/400 อยู่บนระบบปฏิบัติการ OS/400

คำสั่ง CL ที่จะได้เรียนจากบทนี้

Command Task Description
WRKMBRPDM Work with Members เข้าไปยังลิสต์ของ Member ใน Source file
STRSEU Source code editing Start Source Entry Utility
STRSDA Screen display Screen Design Aid
CALL program-name เรียกโปรแกรม ใช้เรียกโปรแกรมอ็อบเจ็กต์
CRTDSPF Display file สร้างคอมไพล์ Display file
CRTRPGPGM การคอมไพล์โปรแกรม สร้างคอมไพล์โปรแกรม RPG

ตัวอักษร

  • The letters A-Z
  • The numbers 0-9
  • The characters + - * , . ' & / $ # : @
  • The blank

ลำดับของฟอร์ม

ภาพแสดงลำดับการประกาศหัว

Specification Describe
H : Control ใช้สำหรับกำหนด Config ต่าง ๆ ให้กับโปรแกรม
F : File Description ใช้กำหนดเกี่ยวกับไฟล์
E : Extension ใช้ประกาศอาร์เรย์ และตาราง
L : Line Counter ใช้กำหนดความยาวของ บรรทัด Overflow
I : Input ใช้กำหนด Data structure, constant, records และ field
C : Calculation ใช้ทำดำเนินการคำสั่งต่าง ๆ
O : Output ใช้สำหรับแสดงผลเป็นแบบ spool file
**: File Translation records  
**: Alternate collation sequence records  
**: Compile-time array and table data  

Indicators

Indicators เก็บค่าตัวอักษร ‘0’ และ ‘1’ โดยใช้เป็นตัวกำกับว่า เปิด หรือ ปิด

  • MR, 1P, KA-KN และ KP-KY จะไม่สามารถใช้คำสั่ง SETON บังคับได้
  • MR และ 1P จะไม่สามารใช้คำสั่ง SETOFF บังคับได้

Structured programming

XXXX

Sequential programming

ชุดคำสั่งที่ทำงานต่อกัน

Conditional branching

  • If Else

    ตัวอย่าง สร้างเงื่อนไขเช็คตัวแปร CENTR ว่าใช่ 'Y' หรือ 'N' หรือไม่ ถ้าใช่ให้เซ็ต '0' ให้กับตัวแปร IN52 ถ้าไม่ใช่ให้เซ็ต '1' ให้กับตัวแปร IN52

    Ex. javascript

          const CENTR = ''
          let IN52 = '0'
    
          if (CENTR == 'Y' or CENTR == 'N'){
              IN52 = '0'
          }else{
              IN52 = '1'
          }
    

    ตัวอย่างภาษา RPG If-else statement

  • Select When Other

    ตัวอย่าง Select when statement

    • CASxx
    • GOTO
    • CABxx

      xx สามารถเป็นได้ดังนี้

      อักษร ความหมาย
      GT X1 > X2
      LT X1 < X2
      EQ X1 == X2
      NE X1 != X2
      GE X1 >= X2
      LE X1 <= X2
  • Repeatting an operation based on a certain condition

    • Do

      ตัวอย่าง Do statement

    • Do While

      ตัวอย่าง Do whilte statement

    • Do Until

      ตัวอย่าง Do until statement

Operation Codes

Arithmetic Operations

ADD

Opcode add

DIV

Opcode div

MULT

Opcode mult

MVR

Opcode mvr

SQRT

Opcode sqrt

SUB

Opcode sub

XFOOT

Opcode xfoot

Z-ADD

Opcode z-add

Z-SUB

Opcode z-sub

Array Operations

LOOKUP

Opcode lookup

คำสั่ง LOOKUP ใช้ค้นหาค่าของ Factor 1 ใน Factor 2 โดยที่ Factor 2 เป็น Array หรือ Table

Indicators จะใช้กำหนดไว้เพื่อบอกผลการค้นหาและ เปิด Indicator ได้ไม่เกิน 2 คัว

Indicator High (HI): On ถ้าตัวค้นหา (Factor 1) มีใน Array และมีตัวที่มากกว่าอยู่ถัดไป

Ex. Data [A B C C C D E] หา B ดังนั้น C ตัวแรกคือตัวที่ทำให้ HI On

Data [E D C C C B A] หา B ดังนั้น C

Indicator Low (LO): On ถ้าตัวค้นหา (Factor 1) มีใน Array และมีตัวที่น้อยกว่าอยู่ถัดไป

Equal (EQ): เจอข้อมูล

MOVEA

Opcode Movea

SORTA

Opcode Sorta

XFOOT

Opcode Xfoot

Branching Operations

CABxx

Opcode Cabxx

GOTO

Opcode goto

Opcode goto2

ITER

Opcode iter

LEAVE

Opcode leave

TAG

Opcode tag

Call Operations

CALL

Opcode call

PARM

Opcode parm

PLIST

Opcode plist

RETURN

Opcode return

Compare Operations

ANDxx (And)

Opcode andxx

COMP (Compare)

Opcode comp

CABxx (Compare and Branch)

Opcode cabxx

CASxx (Conditionally Invoke Subroutine)

Opcode casxx

DOUxx (Do Until)

Opcode douxx

DOWxx (Do While)

Opcode dowxx

IFxx (If)

![Opcode ifxx]/assets/images/rpg/./assets/images/rpg/OP_CODE_IFXX.png)

ORxx (Or)

Opcode orxx

WHENxx (When true then select)

Opcode whenxx

โดยที่ xx หมายถึง

xx สามารถเป็นได้ดังนี้

อักษร ความหมาย
GT X1 > X2
LT X1 < X2
EQ X1 == X2
NE X1 != X2
GE X1 >= X2
LE X1 <= X2
Blanks การทำงานแบบไม่มีเงื่อนไข เช่น CAS, CAB

Data-Area Operations

IN (Retrieve a Data Area)

Opcode In

OUT (Write a Data Area)

Opcode Out

UNLOCK (Unlock a Data Area or Release a Record)

Opcode Unlock

Opcode Unlock2

Declarative Operations

DEFINE (Field Definition)

Opcode define

KFLD (Define Parts of a Key)

Opcode kfld

KLIST (Define a Composite Key)

Opcode klist

PARM (Identify Parameters)

Opcode parm

PLIST (Identify a Parameter List)

Opcode plist

TAG (tag)

Opcode tag

File Operations

CHAIN (Random Retrieval from a File)

Opcode chain

CLOSE (Close Files)

Opcode close

DELETE (Delete Record)

OPcode delete

EXCEPT (Calculation Time Output)

Opcode except

EXFMT (Write/Then Read Format)

Opcode exfmt

OPEN (Open File for Processing)

Opcode open

READ (Read a Record)

Opcode read

Opcode readc

READE (Read Equal Key)

Opcode reade

READP (Read Prior Record)

Opcode readp

READPE (Read Prior Equal)

Opcode readpe

SETGT (Set Greater Than)

Opcode setgt

SETLL (Set Lower Limit)

Opcode setll

UNLOCK (Unlock a Data Area or Release a Record)

Opcode unlock

Opcode unlock 2

UPDATE (Modify Existing Record)

Opcode update

WRITE (Create New Records)

Opcode write

Indicator-Setting Operations

SETOFF (Set Indicator Off)

Opcode setoff

SETON (Set Indicator On)

Opcodde seton

Initialization Operations

CLEAR (Clear)

Opcode clear

Message Operations

DSPLY (Display Function)

Opcode display

Move Operations

MOVE (Move)

Opcode move

MOVEA (Move Array)

Opcode movea

MOVEL (Move Left)

Opcode movel

String Operations

CAT (Concatenate Two String)

Opcode cat

Opcode cat2

CHECK (Check Caracters)

Opcode check

CHECKR (Check Reverse)

Opcode checkr

SCAN (Scan String)

Opcode scan

SUBST (Substring)

Opcode subst

Subroutine Operations

BEGSR (Beginning of Subroutine)

Opcode begsr

ENDSR (End of Subroutine)

Opcode endsr

EXSR (Invoke Subroutine)

Opcode exsr

CASxx (Conditionally Invoke Subroutine)

Opcode casxx

Other Operations

  • ELSE (Else)
  • ENDyy (End a Structure Group)
  • LEAVE (Leave a Do/For Group)
  • OTHER (Otherwise Select)

โดย yy สามารถเป็นได้ดังนี้

อักษร ความหมาย
CS End CASxx
DO End DO, DOU, DOW
FOR End FOR
IF End IF
SL End SELECT
Blanks End ให้กับ Structure operation ใด ๆ

Display screens

Display screen หรือหน้าจอใช้สำหรับเป็น UI ในการใช้งาน AS/400 โดย Screen เราจะสร้าง ไฟล์ไว้ใน [LIB]/QDDSSRC

วิธีสร้าง Screen สามารถทำได้โดย

  1. กด create (F6) บนหน้า WRKMBRPDM
  2. ใช้ CL command STRSDA

Data Files

Data files หรือไฟล์ใช้เก็บข้อมูล

ประเภทของ Data files

  1. Physical file

Physical file เป็นไฟล์ที่ใช้เก็บข้อมูล และตัวไฟล์ยังใช้กำกับรูปแบบของข้อมูลด้วยว่าเป็นประเภทอะไร มีขนาดเท่าไร

Physical file สามารถมี Record format ได้แค่ 1 ตัวต่อหนึ่ง Physical file เท่านั้

Physical file สามารถกำหนดคีย์ให้ได้

  1. Logical file

Logical file ไม่ได้เก็บข้อมูล

Logical file เป็น View คอยเลือกเอา Field ที่ต้องการจาก Physical file นำไปใช้ต่อไป

Logical file สามารถกำหนดคีย์ให้ได้

วิธีสร้าง Data files ทำได้โดยใช้คำสั่ง STRSEU หรือ กด F6 บน WRKMBRPDM

Practices

Practice #1 - Hello world

Op hello world

Practice #2 - Data structure

Op ds

Practice #3 - Loop

Op loop

Practice #4 - Data files

Practice #5 - Chain

Practice #6 - Cahin with keys

Practice #7 - Read

Practice #8 - Read with keys

Practice #9 - Program with parameters

Practice #10 - CRUD