This simple service program makes dBASE file access simple.
Written by Mike Faust
dBASE has been around since the late 1970s/early 1980s. It was the first widely used relational database for PCs. I recently had a need to import data into the System i from the U.S. Census Bureau's Tiger database, which stores address data for all of the United States, including state, county, and city. The Tiger database is made up of multiple dBASE format files, so I needed to create a means by which I could read the data directly from the System i. In this tip, we'll take a look at a simple service program that gives you the ability to do sequential data reads from a dBASE file.
dBASE Table File Format
The dBASE file format is actually relatively easy to read. Tables contain header information that describes the table itself, along with a field descriptor array that defines all of the fields in the file. The data immediately follows the header information and is broken apart based on the field descriptors. Information on the dBASE file layout can be found on the dBASE Web site.
The field descriptor array contains one element for each field in the table. These values describe the field names and the type of data contained in the field. You can determine the end of the field descriptor array by the line field character (hex 0D).
Since all of this information is stored sequentially within the file, it's easy enough to create a service program that reads the file from the IFS and returns the information that is stored in the table file.
The Service Program
To allow for easy access to dBASE table files, I've created a simple service program. The source for this service program can be found here. The source for the service program contains three members.
- dBASEf—This member contains the code for all of the subprocedures inside of this service program.
- dBASEfbnd—The binder language source for the service program is found in this source member.
- dBASEprc—The prototypes for each of the subprocedures are stored in this source member. This can then be used with a /copy in RPGLE programs that are bound to the service program.
The process of compiling the service program involves first creating the RPGLE module using the CRTRPGMOD command:
CRTRPGMOD MODULE(MYLIB/DBASEF)
SRCFILE(MYLIB/QRPGLESRC) SRCMBR(DBASEF)
Next, the service program is created using the CRTSRVPGM command:
CRTSRVPGM SRVPGM(DBASEF) EXPORT(*SRCFILE) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(DBASEFBND)
This service program contains five sub-procedures:
- opendBASEFile(dBASE_stream_file): This subprocedure performs several functions. First, it creates a pointer to space allocated in memory to store the data structure that holds the information on the dBASE table being opened. Next, it opens the actual dBASE stream file on the IFS. Then, it uses the previously defined header data structures to determine various details about the dBASE table, including details on the columns in the table. All of this data is stored in the data structure that is based on the pointer we allocated at the beginning of the subprocedure. This allows us to make this information available to the calling program without passing large volumes of data back and forth.
- readdBASEFile(file_header_pointer): This subprocedure loads data from the defined dBASE file into a structure that can then be used to retrieve the individual data fields. It accepts a single parameter that represents the pointer to the header for the dBASE file containing the record to be read. Then, it returns a pointer parameter that identifies the location in memory where the record data is stored. The subprocedure simply reads the next record sequentially and stores the entire record as a string in memory.
- getdBASEField(file_header_pointer: record_pointer: field_number): This subprocedure returns a data structure that contains data from a specified field in the requested record based on the field's ordinal position in the record.
- getdBASEFieldByName(file_header_pointer: record_pointer: field_name): This subprocedure returns a data structure that contains data from a specified field in the requested record based on the field name.
- closedBASEFile(file_header_pointer): This subprocedure closes the streamed file associated with the file header pointer and then sets the pointer to null.
These subprocedures use a set of data structures to store data associated with the file header and field data. Below are descriptions of these data structures.
Table 1: dbfHeader Data Structure |
||
Field Name |
Data Type/Length |
Description |
Version |
Numeric 1,0 |
The numeric value used to determine the version of the dBASE file |
LastUpdated |
Date |
The date the dBASE file was last updated |
RecordCount |
Integer |
The number of records in the dBASE file |
BytesPerRecord |
Integer |
The length in bytes of each data record |
FieldCount |
Integer |
The number of fields in the dBASE file |
Fields |
Array/Data Strucutre |
An array containing a field data structure defining each field in the dBASE file |
CurrentRecord |
Integer |
The relative record number for the current record based on the last readdBASEFile |
FileDescriptor |
Integer |
The return value from the IFS open API, used to access the streamed file |
Table 2: Field Description Data Structure |
||
Field Name |
Data Type/Length |
Description |
Name |
Character 11 |
The name of the field |
Type |
Character 1 |
The field type (based on the dBASE field types described earlier) |
Length |
Integer |
The total length of the field |
Decimals |
Integer |
The number of decimal places (for numeric fields only) |
Offset |
Integer |
The starting position for the field in the record |
Table 3: Data Structure to Return Field Data |
||
Field Name |
Data Type/Length |
Description |
Type |
Character 1 |
The name of the field |
String |
Character 32767 |
A string representation of the value |
Number |
Numeric 31,9 |
A numeric representation of the value (valid only for numeric fields) |
Date |
Date |
For date fields only, the date as a date type field |
The data structure in Table 1 is generated by the opendBASEFile subprocedure. Within that data structure, multiple copies of the field description data structure shown in Table 2 are generated. That structure contains all of the information for each field in the table. The data structure described in Table 3 shows the data returned by the getdBASEField and getdBASEFieldByName subprocedures.
Sample Program
To see how to use the dBASEf service program, take a look at the TESTDBF RPGLE program shown below:
//********************************************************************
// Purpose: Test Program
//********************************************************************
h dftactgrp(*no)
h option(*nodebugio:*srcstmt:*seclvl)
h bnddir('TESTDIR')
/copy qrpglesrc,DBASEFPRC
dtestdbf pr
d prmdBASEfile 500a
dtestdbf pi
d prmdBASEfile 500a
d wkDSHeader ds likeDs(dbfHeader) based(ptrHeader)
d wkRecordStr s 65535a based(ptrRecord)
d wkRecordCount s 5 0
d wkIndex s 3 0
d wkField ds likeds(fieldData)
d wkStr s 32766a
/free
ptrHeader = openDBaseFile(prmDBaseFile);
wkRecordCount = 0;
ptrRecord = readDBASEFile(ptrHeader);
dow ptrRecord <> *null and wkRecordCount < wkDSHeader.RecordCount;
wkRecordCount += 1;
wkField = getDBaseField(ptrHeader: ptrRecord: 1);
wkStr = wkField.String;
dsply %subst(wkStr: 1: 50);
ptrRecord = readDBASEFile(ptrHeader);
enddo;
closeDBaseFile(ptrHeader);
*INLR = *on;
return;
/end-free
This sample program accepts a single parameter that contains the path to the dBASE file to be opened. It also assumes that the DBASEF service program has already been added to a binding directory named TESTDIR. This program simply opens the specified dBASE file using the opendBASEFile subprocedure, reads each record, and then displays the contents of the first field in that record using the getdBASEField subprocedure. Note that we could just as easily use the getdBASEFieldByName subprocedure, using a field name to retrieve the same information. Finally, the program closes the dBASE file using the closedBASEFile subprocedure.
What It Won't Do
This simple service program does give powerful functionality, but what it won't allow you to do is update a dBASE file, nor will it allow you to read by keyed field or indexes. That being said, this service program does give you an easy way to access dBASE files inside of an ILE RPG program.
LATEST COMMENTS
MC Press Online