Reading and writing records

The acronym CRUD refers to the basic I/O functions that you typically perform on records within a file. The following table shows the CRUD functions and the EGL keywords that provide those functions:

Table 1. CRUD functions in EGL
CRUD function EGL keyword
Create add
Read get
Update replace
Delete delete

Furthermore, EGL tailors the way it executes each of these functions based on the particular type of file with which you are working. For example, if you write a get next statement for a SQL database, EGL generates a series of SQL statements to locate and retrieve the appropriate row from an SQL result set. The same EGL statement generated for a sequential file would simply read the next record in the file.

EGL accomplishes these tasks by means of stereotyping. In everyday use, a stereotype is a common pattern that you can use to characterize an individual. In much the same way, when you apply a stereotype to a record, you tell EGL how it should perform I/O functions that involve that record. For more on stereotypes, see Introduction to Record parts and topics that focus on the specific stereotypes in the EGL Language Reference.

The I/O processing cycle

Assuming that you have an existing database (typically created by a database administrator), regular processing generally involves the create, read, update, and delete (CRUD) functions. More specific examples are presented in the topics that deal with specific data access technologies. The following general scenario is possible only because EGL is so efficient at shielding you from implementation details.

Start by defining a Record, which you must do outside of a generatable part:
package com.companyb.customer

Record CustomerRecord type Stereotype
  customerNumber INT;
  customerName STRING;
  customerBalance MONEY;
end

The details of the record, and its properties, will vary depending on how you plan to use it.

Next, within the body of your program, declare a variable based on this Record part:
myCustomer CustomerRecord;
To find an existing customer record, you will need a unique piece of information about the record. Typically this means some kind of ID number, often used as a key to the file. A key is a piece of data used to index a file, so you don't have to look at every record to find the one that you want. Assume that you have separately written a function called getCustomer() that asks the user for a customer number and returns that number:
myCustomer.customerNumber = getCustomer();
To read the information that matches the requested number, simply code the following:
get myCustomer forUpdate;

EGL generates the appropriate code to read the data from the file or database and then places the information in your record variable. The forUpdate keyword tells EGL to place a hold on the data so you have the option to replace or delete it.

You can then offer the information to your user with the option to change it. Assume that you have separately written a function called showCustomer() that returns a –1 if the user wants to delete the record, 0 if the record is unchanged, and 1 if the user made any changes to the record:
case (showCustomer(myCustomer))
  when (-1)
    delete myCustomer;
  when (1)
    replace myCustomer;
end
If you handle errors (see Error handling), you can do a bit more with the number that getCustomer() returns. Assume that you have separately written a function called getReply() that returns TRUE or FALSE depending on whether the user answers Y or N to the specified question:
set myCustomer empty;            // all fields blank or 0
myCustomer.customerNumber = getCustomer();   // sets cust #
  
try
  get myCustomer forUpdate;
onException (ex AnyException)      // couldn't get the record
  if(myCustomer is noRecordFound)  // because customer # not on file
    if(getReply("Do you want to add this customer?  (y/n)") == TRUE)
      showCustomer(myCustomer);    // let user fill in remaining fields
      add myCustomer;
    end
  else
    myErrorHandler(ex);
end

With these few lines of code you have the heart of a customer file service program that you can use with an SQL database or your own VSAM indexed file. The only difference in your program between VSAM and SQL is the stereotype you apply to the CustomerRecord definition.


Feedback