CSVRecord stereotype

The CSVRecord stereotype customizes a Record for use with a comma-separated value (CSV) file.

Each record in a CSV file contains a row of data. The information from each column within the row is set off by a delimiter such as a comma (the default). CSV files provide a simple, standard, human-readable way to store data from a spreadsheet or database.

Example

The following example represents a spreadsheet with three rows and four columns. The columns contain, in order, an INT, a CHAR(3), a DATE?, and a STRING:
123,yes,3/9/2007,Rahima
-1,no ,9/9/1999,Jorge
92,yes,,Ludmilla
The following record is designed to hold this data in EGL:
record CsvRec type CSVRecord 
	{
	  fileName = "empfile", 
	  delimiter = ",",
	  textQualifier = "\"",
	  style = CsvStyle.quoted,
	  labels = [ "Job Code", "Permanent?", "Hire Date", "First Name" ]
	}
	jobCode int;
	permStatus char(3);
	hireDate date?;
	firstName string;
end

The text qualifier

The way that EGL uses the text qualifier depends on the value of the style property. EGL interprets the text qualifier character when it reads (with the get next statement), and adds the text qualifier character when it writes (with the add statement). The qualifiers are never stored as part of the CSVRecord.

Assume that instead of Ludmilla, the above CSVRecord contained the following value:
Steve "Sparky"
When EGL prepares to write this firstName variable, it finds the text qualifier character (a double quote) embedded in the string. Because the style property is set to CSVStyle.quoted, EGL places additional text qualifier characters at the beginning and end of the string. It also doubles the instances of the text qualifier within the string. It then writes the following line to the CSV file:
92,yes,,"Steve ""Sparky"""
With everything the same except the style property set to CSVStyle.escaped, EGL writes the following line:
92,yes,,Steve ""Sparky""
With the style property set to CSVStyle.escaped and the textQualifier property set to "\\" (a single backslash, which requires an additional backslash as an escape character), EGL writes the following line:
92,yes,,Steve \"Sparky\"
When EGL reads, the inverse process takes place. Returning to the original example, assume EGL reads the following line from a CSV file into the CSVRecord shown above:
92,yes,,"Steve ""Sparky"""
EGL recognizes the first and last quotation marks as text qualifiers and removes them. It also removes the extra quotation marks and saves the following into the firstName variable:
Steve "Sparky"

For more information about reading and writing to CSV files, see add considerations for file I/O and get considerations for file I/O.

Compatibility

Table 1. Compatibility considerations for CSV records
Platform Issue
Java™ generation and debug The resource association type for the CSVRecord should be seqws. CSVRecords do not use the formFeedOnClose and text fields in the resource association. The other fields have their usual meanings.
COBOL generation The CSVRecord stereotype is not supported.

Feedback