25
Advanced Advanced Sequenti Sequenti al al Files 1. Files 1.

Advanced Sequential Files 1.. Single Record Type Files In a file which contains only one record type (the kind we have examined so far) the record structure

Embed Size (px)

Citation preview

Page 1: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

AdvancedAdvancedSequentialSequential

Files 1.Files 1.

Page 2: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Single Record Type FilesSingle Record Type Files

In a file which contains only one record type (the In a file which contains only one record type (the kind we have examined so far) the record structure kind we have examined so far) the record structure is described as part of the file FD using an 01 level is described as part of the file FD using an 01 level number.number.

The record description creates a ‘buffer’ capable of The record description creates a ‘buffer’ capable of storing one record instance at a time.storing one record instance at a time.

Each time a record is read from the file it overwrites Each time a record is read from the file it overwrites the previous contents of the buffer.the previous contents of the buffer.

The record buffer is the only connection between The record buffer is the only connection between the file and the program.the file and the program.

Page 3: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Multiple Record Type FilesMultiple Record Type Files

Quite often a single file will contain Quite often a single file will contain more than one more than one type of record.type of record.

For instance, some of the terminal exercises For instance, some of the terminal exercises required that your program apply a file of required that your program apply a file of transaction records to the StudentsFile.transaction records to the StudentsFile.

For simplicity, the Transaction file in these For simplicity, the Transaction file in these exercises contained one record type only; either exercises contained one record type only; either Insertion or Update or Deletion.Insertion or Update or Deletion.

In a real environment, transactions of this sort In a real environment, transactions of this sort would normally be collected together into would normally be collected together into oneone single transaction file.single transaction file.

Page 4: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Implications of a multiple record transaction file.Implications of a multiple record transaction file.

Gathering all the transactions into a single file Gathering all the transactions into a single file implies that the file will contain implies that the file will contain different record different record types types (i.e. records with different structures).(i.e. records with different structures).

The different record structures may give rise to The different record structures may give rise to records which are also of records which are also of different lengthsdifferent lengths..

For exampleFor example

an insertion transaction will contain all the fields that an insertion transaction will contain all the fields that appear in the StudentFile record (appear in the StudentFile record (32 characters).).

a deletion transaction will contain only the StudentId a deletion transaction will contain only the StudentId ((7 characters).).

an update transaction used to record course changes an update transaction used to record course changes might contain the StudentId, the OldCourseCode and might contain the StudentId, the OldCourseCode and the NewCourseCode (the NewCourseCode (15 characters).).

Page 5: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Describing multiple record filesDescribing multiple record files

To describe these different record types we have To describe these different record types we have to use to use more than one more than one record description in the record description in the file's FD.file's FD.

Because record descriptions always begin with Because record descriptions always begin with level 01 we provide a 01 level for level 01 we provide a 01 level for eacheach record type record type in the file.in the file.

Page 6: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

What is not What is not obvious from this obvious from this description is that description is that COBOLCOBOL continues to continues to create just a create just a single single ‘record buffer’ ‘record buffer’ for for the file!the file!

And this ‘record And this ‘record buffer’ is only able buffer’ is only able to store a to store a single single record record at a time!at a time!

DATA DIVISION.FILE SECTION.FD TransactionFile.01 InsertionRec. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

01 DeleteRec. 02 StudentId PIC 9(7).

01 UpdateRec. 02 StudentId PIC 9(7). 02 OldCourseCode PIC X(4). 02 NewCourseCode PIC X(4).

Multiple record descriptions - One record bufferMultiple record descriptions - One record buffer

Page 7: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Multiple record descriptions in a file are Multiple record descriptions in a file are IMPLICITLYIMPLICITLY redefinitionsredefinitions of the single record buffer. of the single record buffer.

9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 8: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Multiple record descriptions in a file are Multiple record descriptions in a file are IMPLICITLYIMPLICITLY redefinitionsredefinitions of the single record buffer. of the single record buffer.

InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender

9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 9: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Multiple record descriptions in a file are Multiple record descriptions in a file are IMPLICITLYIMPLICITLY redefinitionsredefinitions of the single record buffer. of the single record buffer.

InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender

StudentId

DeletionRecDeletionRec StudentId

DeletionRecDeletionRec

9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 10: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Multiple record descriptions in a file are Multiple record descriptions in a file are IMPLICITLYIMPLICITLY redefinitionsredefinitions of the single record buffer. of the single record buffer.

InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender

StudentId

DeletionRecDeletionRec StudentId

DeletionRecDeletionRec

StudentId OldCourseCode NewCourseCode

UpdateRecUpdateRec StudentId OldCourseCode NewCourseCode

UpdateRecUpdateRec

9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 11: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

All these record descriptions are valid at the All these record descriptions are valid at the same same timetime. .

But only one description makes sense for the values But only one description makes sense for the values in the buffer.in the buffer.

How can we tell which description to use?How can we tell which description to use?

InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecStudentId StudentName DateOfBirth CourseCode Grant Gender

StudentId

DeletionRecDeletionRec StudentId

DeletionRecDeletionRec

StudentId OldCourseCode NewCourseCode

UpdateRecUpdateRec StudentId OldCourseCode NewCourseCode

UpdateRecUpdateRec

9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 12: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

The Transaction Type CodeThe Transaction Type Code

Generally we cannot reliably establish the type of Generally we cannot reliably establish the type of record record READREAD into the buffer by examining its into the buffer by examining its contents.contents.

To allow us to distinguish between the record To allow us to distinguish between the record types, a special data item is inserted into each types, a special data item is inserted into each transaction which identifies the transaction type.transaction which identifies the transaction type.

This data item is usually the first data item in the This data item is usually the first data item in the transaction record and one character in size, but it transaction record and one character in size, but it does not have to be.does not have to be.

Transaction types can be identified using a Transaction types can be identified using a number, a letter or other character.number, a letter or other character.

Page 13: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

The Revised FD.The Revised FD. TransCode occurs in all TransCode occurs in all

the record descriptions.the record descriptions. How can we refer to the How can we refer to the

one in DeleteRec?one in DeleteRec? MOVE TransCode OF MOVE TransCode OF

DeleteRec TO TCode.DeleteRec TO TCode. But TransCode really But TransCode really

only needs to be defined only needs to be defined in one record.in one record.

Since all the records Since all the records map on to the map on to the same area same area of storage the of storage the TransCode defined for TransCode defined for the InsertionRec can be the InsertionRec can be used no matter which used no matter which record type is actually in record type is actually in the buffer . the buffer .

DATA DIVISION.FILE SECTION.FD TransactionFile.01 InsertionRec. 02 TransCode PIC X. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

01 DeleteRec. 02 TransCode PIC X. 02 StudentId PIC 9(7).

01 UpdateRec. 02 TransCode PIC X. 02 StudentId PIC 9(7). 02 OldCourseCode PIC X(4). 02 NewCourseCode PIC X(4).

Page 14: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

The Final FD.The Final FD.DATA DIVISION.FILE SECTION.FD TransactionFile.01 InsertionRec. 88 EndOfTransFile VALUE HIGH-VALUES. 02 TransCode PIC X. 88 Insertion VALUE "I". 88 Deletion VALUE "D". 88 Update VALUE "U". 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

01 DeleteRec. 02 FILLER PIC X(8).

01 UpdateRec. 02 FILLER PIC X(8). 02 OldCourseCode PIC X(4). 02 NewCourseCode PIC X(4).

TransCode and TransCode and StudentId have StudentId have the the same same description description and and are in the are in the same same location location in all in all three records.three records.

So they are So they are defined only in defined only in the InsertionRec.the InsertionRec.

In the other In the other records the area records the area occupied by occupied by these two items these two items is defined using is defined using FILLERFILLER..

Page 15: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

What happens when we display the OldCourseCode?What happens when we display the OldCourseCode? What happens if we now read an Update record into What happens if we now read an Update record into

the buffer? the buffer?

InsertionRecInsertionRecTransCode StudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecTransCode StudentId StudentName DateOfBirth CourseCode Grant Gender

FILLER

DeletionRecDeletionRec FILLER

DeletionRecDeletionRec

FILLER OldCourseCode NewCourseCode

UpdateRecUpdateRecFILLER OldCourseCode NewCourseCode

UpdateRecUpdateRec

I 9 2 3 0 1 6 5 H E N N E S S Y R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 16: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

When a record smaller than the size of the largest When a record smaller than the size of the largest record is read into the buffer any data that is not record is read into the buffer any data that is not explicitly overwritten is left intact.explicitly overwritten is left intact.

What happens when we display StudentName and What happens when we display StudentName and DateOfBirth?DateOfBirth?

InsertionRecInsertionRecTransCode StudentId StudentName DateOfBirth CourseCode Grant Gender InsertionRecInsertionRecTransCode StudentId StudentName DateOfBirth CourseCode Grant Gender

FILLER

DeletionRecDeletionRec FILLER

DeletionRecDeletionRec

FILLER OldCourseCode NewCourseCode

UpdateRecUpdateRec FILLER OldCourseCode NewCourseCode

UpdateRecUpdateRec

U 9 3 1 5 6 8 2 L M 6 1 L M 5 1 R M 7 1 0 9 1 5 L M 5 1 0 5 5 0 F

TransactionFile BufferTransactionFile Buffer

Page 17: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Printing a Report.Printing a Report.

A report is made up of groups of printed lines of A report is made up of groups of printed lines of different types.different types.

What types of line are required for the Student What types of line are required for the Student Details Report?Details Report?

A program is required which will print a report.A program is required which will print a report.

The report, called the Student Details Report, will The report, called the Student Details Report, will be based on the file Students.Dat.be based on the file Students.Dat.

The report will show the Name, StudentId, Gender The report will show the Name, StudentId, Gender and CourseCode of each student in the file.and CourseCode of each student in the file.

Page 18: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Report Print Lines.Report Print Lines.

Page Heading. Page Heading.

UL Student Details Report UL Student Details Report

Page Footing.Page Footing.

Page : Page : PageNumPageNum

Column Headings.Column Headings.

Student Id. Student Name Gender CourseStudent Id. Student Name Gender Course

Student Detail Line.Student Detail Line.

StudentId. StudentName Gender CourseCodeStudentId. StudentName Gender CourseCode

Report Footing.Report Footing.

**** End of Student Details Report ***** End of Student Details Report ***

Page 19: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

Describing Print Lines.Describing Print Lines.

01 PageHeading.01 PageHeading. 02 FILLER PIC X(7) VALUE SPACES. 02 FILLER PIC X(7) VALUE SPACES. 02 FILLER PIC X(25) 02 FILLER PIC X(25) VALUE "UL Student Details Report". VALUE "UL Student Details Report".

01 PageFooting.01 PageFooting. 02 FILLER PIC X(19) VALUE SPACES. 02 FILLER PIC X(19) VALUE SPACES. 02 FILLER PIC X(7) VALUE "Page : ". 02 FILLER PIC X(7) VALUE "Page : ". 02 FILLER PIC 99. 02 FILLER PIC 99.

01 ColumnHeadings PIC X(36)01 ColumnHeadings PIC X(36) VALUE " StudentId StudentName Gender Course". VALUE " StudentId StudentName Gender Course".

01 StudentDetailLine.01 StudentDetailLine. 02 PrnStudId PIC BB9(7). 02 PrnStudId PIC BB9(7). 02 PrnStudName PIC BBX(10). 02 PrnStudName PIC BBX(10). 02 PrnGender PIC BBBBX. 02 PrnGender PIC BBBBX. 02 PrnCourse PIC BBBBX(4). 02 PrnCourse PIC BBBBX(4).

01 ReportFooting PIC X(38)01 ReportFooting PIC X(38) VALUE "*** End of Student Details Report ***". VALUE "*** End of Student Details Report ***".

The Print Lines are all different The Print Lines are all different record record types!types!

Page 20: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

The File BufferThe File Buffer All data coming from, or going to, the peripherals All data coming from, or going to, the peripherals

must pass through a file buffer declared in the File must pass through a file buffer declared in the File Section.Section.

The file buffer is associated The file buffer is associated with the physical device by with the physical device by means of a Select and means of a Select and Assign clause.Assign clause.

In previous In previous lectures we saw lectures we saw that the file bufferthat the file bufferis represented by a record description (01 level).is represented by a record description (01 level).

But the different types of line that must appear on But the different types of line that must appear on our report are declared as different record types.our report are declared as different record types.

How can we declare these different record types in How can we declare these different record types in the File Section?the File Section?

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT Printer ASSIGN TO “LPT1”.

DATA DIVISION.FILE SECTION.FD Printer.01 PrintLine. ????????????????

Page 21: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

No No VALUEVALUE clause in the clause in the FILE SECTIONFILE SECTION..

Defining a file buffer which is used by different Defining a file buffer which is used by different record types is easy (as we have seen).record types is easy (as we have seen).

But !!But !! These record types all map on to the same area of These record types all map on to the same area of

storage and print line records storage and print line records cannot cannot share the share the same area of storage. same area of storage.

Why? Because most of the print line record values Why? Because most of the print line record values are assigned using the are assigned using the VALUEVALUE clause and these clause and these values are assigned as soon as the program starts.values are assigned as soon as the program starts.

To prevent us trying to use the To prevent us trying to use the VALUEVALUE clause to clause to assign values to a File buffer assign values to a File buffer COBOL COBOL has a rule has a rule which states that;which states that;

In the In the FILE SECTIONFILE SECTION, the , the VALUEVALUE clause must be clause must be used used in condition-name entries only (i.e. it cannot be in condition-name entries only (i.e. it cannot be used used to give an initial value to an item).to give an initial value to an item).

Page 22: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

A SolutionA Solution

We define the print records in the We define the print records in the WORKING-STORAGE WORKING-STORAGE SECTIONSECTION..

We create a file buffer in the We create a file buffer in the FILE SECTION FILE SECTION which is the which is the size of the largest print record.size of the largest print record.

We print a line by moving the appropriate print record We print a line by moving the appropriate print record to the file buffer and then to the file buffer and then WRITEWRITEing the contents of the ing the contents of the file buffer to the device.file buffer to the device.

We get round the problem as follows;We get round the problem as follows;

Page 23: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT ReportFile ASSIGN TO “STUDENTS.RPT”.

DATA DIVISION.FILE SECTION.FD ReportFile.01 PrintLine PIC X(38).

WORKING-STORAGE SECTION.01 PageHeading. 02 FILLER PIC X(7) VALUE SPACES. 02 FILLER PIC X(25) VALUE "UL Student Details Report".

01 PageFooting. 02 FILLER PIC X(19) VALUE SPACES. 02 FILLER PIC X(7) VALUE "Page : ". 02 FILLER PIC 99.

01 ColumnHeadings PIC X(36) VALUE " StudentId StudentName Gender Course".

01 StudentDetailLine. 02 PrnStudId PIC BB9(7). 02 PrnStudName PIC BBX(10). 02 PrnGender PIC BBBBX. 02 PrnCourse PIC BBBBX(4).

01 ReportFooting PIC X(38) VALUE "*** End of Student Details Report ***".

STUDENTS.RPT

DISK

Page 24: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

WRITE Syntax revisited.WRITE Syntax revisited.

When we are writing to a When we are writing to a printerprinter or a or a print file print file we we use a form of the WRITE command different from use a form of the WRITE command different from that we use when writing to a sequential file.that we use when writing to a sequential file.

WRITE

ADVANCING

AdvanceNum

MnemonicName

PAGE

RecordName FROM Identifier

BEFORE

AFTER

LINE

LINES

Page 25: Advanced Sequential Files 1.. Single Record Type Files  In a file which contains only one record type (the kind we have examined so far) the record structure

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ReportFile ASSIGN TO "STUDENTS.RPT" ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION. FILE SECTION. FD ReportFile. 01 PrintLine PIC X(40).

WORKING-STORAGE SECTION. 01 HeadingLine PIC X(21) VALUE " Record Count Report".

01 StudentTotalLine. 02 FILLER PIC X(17) VALUE "Total Students = ". 02 PrnStudentCount PIC Z,ZZ9.

01 MaleTotalLine. 02 FILLER PIC X(17) VALUE "Total Males = ". 02 PrnMaleCount PIC Z,ZZ9.

01 FemaleTotalLine. 02 FILLER PIC X(17) VALUE "Total Females = ". 02 PrnFemaleCount PIC Z,ZZ9.

MOVE StudentCount TO PrnStudentCountMOVE MaleCount TO PrnMaleCountMOVE FemaleCount TO PrnFemaleCountWRITE PrintLine FROM HeadingLine AFTER ADVANCING PAGEWRITE PrintLine FROM StudentTotalLine AFTER ADVANCING 2 LINESWRITE PrintLine FROM MaleTotalLine AFTER ADVANCING 2 LINESWRITE PrintLine FROM FemaleTotalLine AFTER ADVANCING 2 LINES.

STUDENTS.RPT

DISK