2.2 Accessing a table
This section describes the predicates to read data from a table.
2.2.1 Finding record locations in a table
Records are addressed by their offset in the table (file). As records have generally non-fixed length, searching is often required. The predicates below allow for finding records in the file.
- get_table_attribute(+Handle, +Attribute, -Value)
- Fetch attributes of the table. Defined attributes:
file
Unify value with the name of the file with which the table is associated. field(N)
Unify value with declaration of n-th (1-based) field. field_separator
Unify value with the field separator character. record_separator
Unify value with the record separator character. key_field
Unify value with the 1-based index of the field that is sorted or fails if the table contains no sorted fields. field_count
Unify value with the total number of columns in the table. size
Unify value with the number of characters in the table-file, not the number of records. window
Unify value with a term Start -
Size, indicating the properties of the current window. - table_window(+Handle, +Start, +Size)
- If only part of the file represents the table, this call may be used to define a window on the file. Start defines the start of the window relative to the start of the file. Size is the size in characters. Skipping a header is one of the possible purposes for this call.
- table_start_of_record(+Handle, +From, +To, -Start)
- Enumerates (on backtracking) the start of records in the table in the region [From, To). Together with read_table_record/4, this may be used to read the table's data.
- table_previous_record(+Handle, +Here, -Previous)
- If Here is the start of a record, find the start of the record before it. If Here points at an arbitrary location in a record, the start of this record will be returned.
2.2.2 Reading records
There are two predicates for reading records. The read_table_record/4 reads an entire record, while read_table_fields/4 reads one or more fields from a record.
- read_table_record(+Handle, +Start, -Next, -Record)
- Read a record from the table. Handle is a handle as returned
by new_table/4. Start
is the location of a record. If Start does not point to the
start of a record, this predicate searches backwards for the starting
position. Record is unified with a term constructed from the functor
associated with the table (default name
record
and arity the number of not-skipped columns), each of the arguments containing the converted data. An error is raised if the data could not be converted. Next is unified with the start position for the next record. - read_table_fields(+Handle, +Start, -Next, -Fields)
- As read_table_record/4, but Fields is a list of terms +Name(-Value), and the Values will be unified with the values of the specified field.
- read_table_record_data(+Handle, +Start, -Next, -Record)
- Similar to read_table_record/4,
but unifies record with a Prolog string containing the data of the
record unparsed. The returned record does not contain the
terminating record-separator.
2.2.3 Searching the table
- in_table(+Handle, ?Fields, -RecordPos)
- Searches the table for records matching Fields. If a match is
found, the variable (see below) fields in Fields are unified
with the corresponding field value, and RecordPos is unified
with the position of the record. The latter handle may be used in a
subsequent call to read_table_record/4
or
read_table_fields/4.
Fields is a list of field specifiers. Each specifier is of the format:
FieldName(Value [, Options])
Options is a list of options to specify the search. By default, the package will search for an exact match, possibly using the ordering table associated with the field (see
order
option in new_table/4). Options are:prefix
Uses prefix search with the default table. prefix(Table)
Uses prefix search with the specified ordering table. substring
Searches for a substring in the field. This requires linear search of the table. substring(Table)
Searches for a substring, using the table information for determining the equivalence of characters. =
Default equivalence. =(Table)
Equivalence using the given table. If Value is unbound (i.e. a variable), the record is considered not specified. The possible option list is ignored. If a match is found on the remaining fields, the variable is unified with the value found in the field.
First, the system checks whether there is an ordered field that is specified. In this case, binary search is employed to find the matching record(s). Otherwise, linear search is used.
If the match contains a specified field that has the property
unique
set (see new_table/4), in_table/3 succeeds deterministically. Otherwise it will create a backtrack-point and backtracking will yield further solutions to the query.in_table/3 may be comfortable used to bind the table transparently to a predicate. For example, we have a file with lines of the format.1This is the
disproot.dat
table from the AAT database used in GRASPC1C2,Full Name
C1C2 is a two-character identifier used in the other tables, and FullName is the description of the identifier. We want to have a predicate identifier_name(?Id, ?FullName) to reflect this table. The code below does the trick:
:- dynamic stored_idtable_handle/1. idtable(Handle) :- stored_idtable_handle(Handle). idtable(Handle) :- new_table('disproot.dat', [ id(atom, [downcase, sorted, unique]), name(atom) ], [ field_separator(0',) ], Handle), assert(stored_idtable_handle(Handle)). identifier_name(Id, Name) :- idtable(Handle), in_table(Handle, [id(Id), name(Name)], _).
2.2.4 Miscellaneous
- table_version(-Version, -CompileDate)
- Unify Version with an atom identifying the version of this package, and CompileDate with the date this package was compiled.