RPG IV - Chapter 11 - Fox Valley Technical College

Download Report

Transcript RPG IV - Chapter 11 - Fox Valley Technical College

Advanced Data Definition
Chapter 11
Presentation © Copyright 2002, Bryan Meyers
Objectives
• Understand unique data types
• Perform date arithmetic
• Define, initialize, and use data structures
Programming in RPG IV
Third Edition
2
Data Types
Data Class
Data Type
RPG Code
Character
Character
A
Numeric
Zoned decimal
S
Packed decimal
P
Signed integer
I
Unsigned integer
U
Binary
B
Floating point
F
Date
D
Time
T
Timestamp
Z
Boolean
Indicator
N
DBCS
Graphic
G
Unicode
C
Object
Object/class
O
Pointer
Pointer
*
Date
Programming in RPG IV
Third Edition
3
Data Types and Time
• Three data types for dealing with time:
– Date (D)
– Time (T)
– Timestamp (Z)
• Date supports 8 distinct formats
– Default is *ISO
• Time supports 5 distinct formats
• Several separators are supported
Programming in RPG IV
Third Edition
4
Date Formats
DATFMT
*ISO
*USA
*EUR
*JIS
*YMD
*MDY
*DMY
*JUL
Programming in RPG IV
Third Edition
Representation
yyyy-mm-dd
mm/dd/yyyy
dd.mm.yyyy
yyyy-mm-dd
yy/mm/dd
mm/dd/yy
dd/mm/yy
yy/ddd
Example
9999-12-31
12/31/9999
31.12.9999
9999-12-31
99/12/31
12/3199
31/12/99
99/365
5
Duration Codes
• Represent a duration in time
– *YEARS (*Y)
– *MONTHS (*M)
– *DAYS (*D)
– *HOURS (*H)
– *MINUTES (*MN)
– *SECONDS (*S)
– *MSECONDS (*MS)
Programming in RPG IV
Third Edition
6
Duration Functions
• Convert a number to a duration
– %YEARS
– %MONTHS
– %DAYS
– %HOURS
– %MINUTES
– %SECONDS
– %MSECONDS
Programming in RPG IV
Third Edition
7
Date Arithmetic
• Can perform date arithmetic using
date/time/timestamp data in an
expression with duration functions
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
DueDate = BillDate + %DAYS(30);
EndTime = StartTime + %MINUTES(Min);
DueDate = DueDate + %YEARS(5);
StartDate = EndDate - %MONTHS(5);
StartDate = EndDate + %MONTHS(-5);
BillDate = DueDate - %DAYS(30);
DueDate = DueDate - %YEARS(5);
/END-FREE
Programming in RPG IV
Third Edition
8
%DIFF Function
• Calculates the difference between
dates/times/timestamps
• Specify duration code as third argument
• Result is numeric
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
Age = %DIFF(TodayDate:BirthDate:*YEARS);
CramTime = %DIFF(ExamDate:TodayDate:*DAYS);
/END-FREE
Programming in RPG IV
Third Edition
9
%SUBDT Function
• Extracts portion of a date/time/timestamp
– Date “substring”
• Specify portion with a duration code
• Result is numeric
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
BirthYear = %SUBDT(BirthDate:*YEARS);
LoanMonth = %SUBDT(LoanDate:*MONTHS);
/END-FREE
Programming in RPG IV
Third Edition
10
Typed Literals
• Precede date/time literals with D or T
– D’1899-12-31’
– T’08.00.00’
• Format of literal must match any format
specified in H-spec DATFMT/TIMFMT
keyword
– Use *ISO format as default
Programming in RPG IV
Third Edition
11
Pointer Data Types
• Two kinds of pointer data types
– Basing pointers
• Another field is BASED upon this pointer
– Procedure pointers
• Defined with PROCPTR keyword
• Used to access storage that is allocated
dynamically throughout the program
• Pointers store address of memory
locations rather than data values
Programming in RPG IV
Third Edition
12
LIKE (Field Definition)
• LIKE keyword defines a field based on
the attributes of another field
– Used in D-specs
• Field can be increased or decreased in
length from the reference field
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++
D Gross
S
9 2
D NetPay
S
LIKE(Gross)
D FedTax
S
-2
LIKE(Gross)
D TotalGross
S
+4
LIKE(Gross)
Programming in RPG IV
Third Edition
13
Data Structures
• Subdivide fields into subfields
• Restructure records with different field
layouts
• Change field data types
• Add a second dimension to arrays
Programming in RPG IV
Third Edition
14
Defining Data Structures
• Subdivide fields into subfields
• Change field data types
• Data structure has two parts
– Data structure header
– Definition for the subfields
• DS coded in 24-25 on D spec signals the
beginning of a data structure
• Enter name of data structure in 7-21
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D OptName
DS
D SubfieldA
3 0
D SubfieldB
5 2
Programming in RPG IV
Third Edition
15
OVERLAY
• OVERLAY keyword indicates that a subfield
overlays the storage of another subfield
• If the data item within parentheses is an array,
OVERLAY applies to each element of the array
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D
DS
D Phone
10 0
D AreaCode
3 0 OVERLAY(Phone)
D Exchange
3 0 OVERLAY(Phone:4)
D LocalNbr
4 0 OVERLAY(Phone:7)
//Using OVERLAY with *NEXT
D
DS
D BigArray
10
DIM(300)
D SubArray
2
OVERLAY(BigArray)
Programming in RPG IV
Third Edition
16
Externally Described Data Structures
• Externally described data structures are useful
when you want to standardize a data structure
– Subfield are described as fields in a file record
format
– Not need to code them in RPG program
• Code an E in position 22
• Name the external file using the EXTNAME
keyword
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D PgmSts
E DS
EXTNAME(RefPgmSts)
Programming in RPG IV
Third Edition
17
LIKEDS
• LIKEDS defines one data structure to be like another data structure
– Identical subfields
• Must qualify subfield name with data structure name
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D Contact
DS
D Name
35
D State
2
D ZipCode
10
D Customer
DS
LIKEDS(Contact)
D Vendor
DS
LIKEDS(Contact)
/FREE
Customer.Name = Vendor.Name;
/END-FREE
Programming in RPG IV
Third Edition
18
Multiple Occurrence Data Structures
• Repeated data structures
• Keyword OCCURS
– Number determines how many times the
structure is repeated in storage
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D DSState
DS
OCCURS(50)
D Code
2
D Name
15
D SalesTax
4 4
D Capital
15
D LowZip
5 0
D HiZip
5 0
Programming in RPG IV
Third Edition
19
%OCCUR Function
• Set occurrence
– %OCCUR function establishes which occurrence program uses
next
– Specify %OCCUR on left side of expression
• Get active occurrence
– %OCCUR retrieves active occurrence
– Specify %OCCUR on right side of expression
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
FOR I = 1 to 50;
%OCCUR(DSState) = I;
READ StatesFile;
ENDFOR;
S = %OCCUR(DSState);
/END-FREE
Programming in RPG IV
Third Edition
20
Variable Initialization
• INZ keyword will initialize to default value
of data type
– Definition specification
• *INZSR subroutine
– Invoked automatically at program start
• CLEAR and RESET operations will
reinitialize variables
– CLEAR generally zeros or blanks fields
– RESET reinitializes variables to INZ value
Programming in RPG IV
Third Edition
21
File Information Data Structure
• Optional data structure for each file in your program
• Contains predefined subfields
– Information about the file and I/O operations
• Requires an F-spec INFDS keyword to name data
structure
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
FFilename++IPEASFRlen+LKlen+AIDevice+.Keywords+++++++++++++++++++++++++++++
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
FSample
UF E K
DISK
INFDS(SampleDS)
…
D SampleDS
DS
D FileName
*FILE
D FileStatus
*STATUS
D OpCode
*OPCODE
D RelRecNbr
397
400B 0
D RecCnt
156
159B 0
Programming in RPG IV
Third Edition
22
Program Status Data Structure
• Provides information about program and
exceptions/errors that occur
• ‘S’ in position 23 identifies a program
status data structure
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D PSDS
SDS
D PrgStatus
*STATUS
D ErrType
40
42
D ErrCode
43
46
D JobName
244
253
D JobNumber
264
269
D UserName
354
363
Programming in RPG IV
Third Edition
23
Points to Remember
• Date/time/timestamp data types can
manipulate date-related data with special
functions
• Fields can be defined relative to other
field definitions using the keyword LIKE
• You can define data structures that
represent complex definitions of a given
area of memory
Programming in RPG IV
Third Edition
24
Points to Remember
• Data structures or subfields of data
structures can be initialized easily as part
of the data structure definition
• Two special data structures of predefined
subfields exist in RPG to provide
information about the status of files used
by the program or about the processing of
the program itself
Programming in RPG IV
Third Edition
25