AS400RPG入门.docx
《AS400RPG入门.docx》由会员分享,可在线阅读,更多相关《AS400RPG入门.docx(7页珍藏版)》请在冰豆网上搜索。
AS400RPG入门
一:
RPG介绍
RPG程序的特点:
1.程序的编写对于每一个位置都有明确的定义,所以程序的编写必须在指定的位置开始。
通过使用ILE工具可以进行一些方便的输入。
2.RPG程序的设计针对每一部分都有严格的要求,有相应的规格说明,他们包括
a)配置规格说明(Control Specifications):
主要规定了程序的命名,日期格式等信息。
b)文件描述说明(File Description Specifications):
对程序使用到的文件的说明
c)定义说明(Definition Specifications):
主要对程序使用的数据进行说明。
d)输入说明(Input Specifications):
主要对文件输入数据进行说明。
e)计算说明(Calculation Specifications):
主要对数据计算和计算顺序的说明。
f)输出说明(Output Specifications):
主要针对数据的输出记录和字段的说明。
g)子程序说明:
包括有三种,对子程序的各种进行进行了定义说明。
3.指示器(Indicators)
通常是一个字节,被设置为‘0’或者‘1’,他主要作为程序操作的结果或者进行操作的条件。
好像程序的逻辑开关,决定着程序的流程。
指示器必须在定义规范中进行定义。
RPG VI也有一些系统的指示器,他们一般是两个字符的变量,如LR等。
指示器可以在程序的任何地方使用,也可以在程序中修改他的值。
4.操作代码
相当与汇编语言的命令一样,有RPG程序提供一套完整的操作代码,比如读一条数据使用READ ,RPG程序对操作代码进行了分类,比如字符串操作符,数组操作符等。
5.定义描述文件
a)文件服务是程序和IO设备的连接。
系统里的每一个文件都有相应的描述文件,对文件的特性和数据的组织形式进行描述。
如果在程序中要使用IO操作,必须要指定一个描述文件,对IO的设备进行描述。
对于文件的类型,系统支持以下几种:
1.数据库文件(database files):
对数据进行持久存储。
2.设备文件(Device files):
容许访问的扩展设备,包括显示文件,打印文件,磁盘文件等。
3.一般文件(save files):
保存在硬盘上的文件
4.远程文件(DDM files):
保存在远程系统上的文件。
b)每一个IO设备都对应有一个描述文件,在程序中如果要使用到IO设备,就要指定相应的文件。
这主要根据操作代码来决定,有的操作代码是设备依赖的,必须指定特定的设备才可以使用,而有些操作代码是和设备独立的,如WRITE等。
产用的设备类型有:
RPG Device Type ――――> iSeries File Type
DISK ――――>database, save, DDM files
PRINTER ――――>printer files
WORKSTN ――――> display, ICF files
SEQ ――――>tape, diskette, save, printer, database
SPECIAL ――――>N/A
c)描述文件的类型:
1、一般程序描述文件(program-described file):
在输入输出描述的文件中会做相应的处理。
2、扩展描述文件(externally described file):
主要是多个应用程序可以共享数据
a)通过针对描述文件的不同配置,RPG程序实现了程序和数据的分离,这样可以针对不同的保存数据的设备和形式,来对描述文件进行配置,这样就很容易的实现的程序的灵活性。
在设备进行升级以后,程序依然可以使用。
二、通过ILE建立RPG程序:
a)ILE(Intergrated Language Environment)的介绍
ILE是AS400中提供的一套增强编程的工具包,他针对不同的编程语言,都有相应的环境支持,ILE RPG,ILE C,ILE COBOL,ILE CL等。
b)ILE的优势
i.ILE 本身提供了许多程序模块,供程序员调用
ii.提供编译的环境,可以对代码进行调试
iii.提供优化的调用性能
iv.可以集成多种语言
v.对代码进行优化
vi.而且提供老版本的代码转换到新版本的代码,主要是针对RPG程序和CL程序。
c)使用ILE建立一个应用程序的例子
vii.建立一个库,取名PRTLIB
CRTLIB LIB(PRTLIB) TEXT(‘SAMPLE ILE APPLICATION’)
viii.将PRTLIB设置为当前库
CHGCURLIB CURLIB(PRTLIB)
ix.建立一个源码文件在PRTLIB中
CRTSRCPF FILE(SOURCE) RCDLEN(112) TEXT(‘SAMPLE ILE Appliaction’)
x.编辑文件,输入文件内容,分配源文件类型是CLP,描述信息是OPM,
WRKMBRPDM FILE(PRTLIB/SOURCE)
xi.按F3键,退出后,建立命令,系统将会产生CRTCLPGM命令。
三、SEU工具的使用
SEU(Source entry utility)源码输入工具。
如图:
这个界面的输入分为两部分:
上面的SEU:
在之后,可以输入SEU编辑的命令,包括查找等命令,都是辅助编辑使用,可以在输入代码的时候,随时输入命令。
代码输入在Beginning of data 和 End of data之间输入代码,如果要插入一行,必须在行首位置输入I,系统会自动增加一行,同时提示出代码行号。
在此界面按F1,有具体的帮助信息。
四:
源码示例
00.10 * PRTPGMR - Print program - OPM
00.20 *
00.30 * QADSPOBJ is the outfile from DSPOBJD - Override occurs in CL
00.40 FQADSPOBJIF E DISK
00.50 FQPRINT O F 132 OF PRINTER
00.60 ****************************************************************
00.70 * Parameter list
00.80 C *ENTRY PLIST Parm list
00.90 C PARM LIB 10 Library
01.00 C EXCPTHDG Prt heading
01.10 ****************************************************************
01.20 * Read a record
01.30 * QLIDOBJD is the format name of the QADSPOBJ file
01.40 C READ QLIDOBJD 20 Read
01.50 * Continue reading until EOF
01.60 C *IN20 DOWEQ'0' Not EOF
01.70 ****************************************************************
01.80 * Use a subroutine to convert the date from MMDDYY to YYMMDD
01.90 C MOVE ODUDAT MMDDYY 6 MMDDYY fmt
02.00 C EXSR CVTDAT Convert date
02.10 C MOVE YYMMDD LSTUSD 60 Last used dt
02.20 C EXCPTDETAIL Print detail
02.30 C OF EXCPTHDG Prt heading
02.40 C READ QLIDOBJD 20 Read
02.50 C ENDDO Loop Back
02.60 * End the program
02.70 C SETON LR Set LR
02.80 ****************************************************************
02.90 C CVTDAT BEGSR
03.00 * Convert date from MMDDYY to YYMMDD format
03.10 C MOVE MMDDYY WORK2 2 Move YY
03.20 C MOVELWORK2 YYMMDD 6 Move YY
03.30 C MOVELMMDDYY WORK4 4 Move MMDD
03.40 C MOVE WORK4 YYMMDD Move MMDD
03.50 C ENDSR
03.60 ****************************************************************
03.70 OQPRINT E 206 HDG
03.80 O 25 'Objects '
03.90 O 'in Library - '
04.00 O LIB
04.10 O E 2 HDG
04.20 O 6 'Object'
04.30 O 18 'Obj type'
04.40 O 30 'Attribute'
04.50 O 42 'Last used'
04.60 O E 1 DETAIL
04.70 O ODOBNM 10
04.80 O ODOBTP 19
04.90 O ODOBAT 33
05.00 O LSTUSDY 41
对于源码的解释如下:
由于还没有很清楚,所以暂不翻译
00.40Theexternallydescribed database file is namedQADSPOBJ.This is the
name of the file provided by the system that is used when an outfile is
created by the DSPOBJD command. The system essentially copies the
format of the model file to the outfile named on DSPOBJD. The RPG
program describes the file in QSYS because it wants to use the format
that exists in the file. An OVRDBF command in the CL program causes
the DSPOBJDP file in QTEMP to be read instead of the file that is compiled
into the program. Because both files use the same format, the compiled
RPG program knows what the layout of the format is.
00.50 The QPRINT file is described as a fixed-format file. Since no OVRPRTF
command was specified prior to the open operation of QPRINT, the
system provides access to the first QPRINT file found on the library list.
This is probably the QPRINT file supplied by IBM in the QGPL library.
This file causes spooled printed output to occur.
00.80 The PLIST and PARM operation codes provide the means to pass in the
variable library name from the CL program. This library name is printed
on the heading line.
01.00 The EXCPT statement prints the heading line as output.
01.40 The program reads a record using the QLIDOBJD format. This is the
name of the format used in the QADSPOBJD outfile.
01.60 If end of file is detected, the program sets the LR indicator on and returns.
01.80 A subroutine is used to convert the date-last-used field (which exists in the
outfile in a MMDDYY format) to a YYMMDD format. There are many solutions
to the problem of reformatting a date field. In this example, the subroutine
method is used because in a later chapter we change from an
RPG subroutine to a subprogram.
01.90 The program moves the ODUDAT field (date last used) to a common field
(MMDDYY) and then calls the subroutine. This type of coding creates a
general-purpose subroutine that can convert any MMDDYY date format.
When the subroutine returns, the YYMMDD field is moved to the field
named LSTUSD, which is a decimal field, so that editing can occur.
02.20 A detail line is printed.
02.30 If printer overflow occurs, an EXCPT operation is used to print the heading
on the next page.
02.40 The program loops back to read another record.
03.10 The subroutine converts the date to the correct format by using MOVE and
MOVEL operations. There are many other ways to alter the date format,
but this method allows the same code to move to a subprogram with ease
in a later chapter.
03.70 The heading output includes the library name that was passed into the
program.
04.60 The detail line prints several fields.