File Handling: Multiple record types


Multiple Record Types:

IN earlier examples we used files that had single record type. This means all the records in the file has same structure. Hence we defined only one structure in the FD entry.
However in real life this may not be the case. There can be multiple records types in a single File.
For example: A file that we received for processing may have all types of records .ie insert records, delete records , update records.
The size of these records can be different. For insertion we need all the fields. But for Deletion we may not need all the fields. Only the primary key values would be sufficient. For update rows only the primary key value and the columns to be updated are enough. Thus all the record types can be different. Also the fields in each of the record types may not be in same position. (If the field “XYZ” starts at position 10 in insert record, it may not be there in “DELETE” record and may be at position 2 in Update record)

To describe multiple record types we need to use more than one record description in the file’s FD entry.
As record descriptions begin with 01 level entry, we will have to provide 01 level for each of the record type.
Note that there will be only one FD written for a single File

Moral is “When a file contains multiple record types, a record declaration (starting with a 01 level number) must be created for each type of record.

Even though we have defined multiple record types there should a way from the program to know what types of record it has just read and how it needs to be processed. One way of doing this is to get this info by looking at the characteristics of the data. However that might not the easy always.
Hence to provide the type of the record the input file should have one more field, say ‘transtypecode” to indicate the type of record.
Even a single character would do.

After adding the transtypecode to the file layout following is how the FD entry would look like.

FD StudentFile.
01 InsertRec.
   02 TranstypeCode         PIC X.
   02 StudentId         PIC 9(7).
   02 StudentName.
      03 Surname        PIC X(8).
      03 Initials       PIC XX.
   02 DateOfBirth.
      03 YOBirth        PIC 9(4).
      03 MOBirth        PIC 99.
      03 DOBirth        PIC 99.
   02 CourseCode        PIC X(4).
   02 Gender            PIC X.

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

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

Generally the “transtypecode” column is the first column in the row. Also note that we can use any name and the field can be of any length. The column “transtypecode” used here is just an example.

It is very important to remember that for one file only 1 record buffer will be reserved irrespective of the number of record types the file has. Thus for all the 3 record types there would be same buffer (same memory space) used.
All the different record type declarations map to the same memory area and are all active at the same time.
As all record types use same memory space , fields from other record types are also accessible at a given time. But it may not contain relevant data.

Example: If the insert types record is read, the field NewCourseCode from update record is still accessible in the program, but it would not contain correct data.

If the file contains insert record of below type:
----------------------------------------
I0000001mahadik Su03031988AX2BM

1st character is “I” indicating that its an insert record. When this record is read into the buffer the fields from other record type description are still valid and accessible as they share the same memory space. However they may contain wrong data.
Here when insert type record is read the field NewCourseCode will contain “maha” which is not the correct course code. There are 4 letters from the surname of the student.

Moral is “ Only one record buffer area irrespective of the number of record types we use. And fields from all the record types are active and accessible at the same time. It’s the programmers responsibility to read correct data and then process it”

Also note that the column “TransTypeCode” occurs in all the three record descriptions. Some might wonder that this might be wrong. But that’s not the case . It is legal to use the same identifier in different records (but not in the same record); but in order to uniquely identify the one you want, you must qualify it with the record name.
To refer to the TransCOdeTYpe of the update record we write UpdateRec OF TransTypeCode . Thus we use here the qualifying technique to distinguish between the fields with same name from several record descriptions.
To move value “X” to the TRansTypeCode of UpdateRec then we have to use:
MOVE “X” to TransTypeCode OF UpdateRec.
                                               
However if we think logically it is not necessary to declare “TransTypeCode” in all the record descriptions . Since all the records map on to the same area of storage, it does not matter which TransTypeCode we refer to - they all access the same value in the record. The same logic applies to the StudentId. The StudentId is in the same place in all three record types, so it doesn't matter which one we use - they all access the same area of memory. This is only allowed if field starts at same position in the multiple record types.

In such cases we use FILLER. This is the name given to the storage area about which we don’t care what the name is .
Example:

FD StudentFile.
01 InsertRec.
   02 TranstypeCode         PIC X.
   02 StudentId         PIC 9(7).
   02 StudentName.
      03 Surname        PIC X(8).
      03 Initials       PIC XX.
   02 DateOfBirth.
      03 YOBirth        PIC 9(4).
      03 MOBirth        PIC 99.
      03 DOBirth        PIC 99.
   02 CourseCode        PIC X(4).
   02 Gender            PIC X.

01 DeleteRec.
   02 FILLER         PIC X.
   02 FILLER         PIC 9(7).

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

Here since the TransTYpeCode and StudentId had same description and had same location is all the 3 records , they were explicity declared only in the insert record and remaining two used it as filler.

Moral is “Multiple record descriptions can have same field name. If used we need to qualify the record name while referring to an individual field name. Ex: STudentID OF INSERTREC.
 However if the common fields in all record descriptions have the same position in the record then we can declare explicitly in only one record descriptions and in other we declare it as filler. Irrespective of the record type when we refer the explicitly defined name we will refer to the correct memory area”


Example: Create a program to read a fixed width file with multiple record types.
[pending]

No comments:

Post a Comment