12 Auto-Generated Fields

Consider for example a list of articles in stock in a toy store:

%rec: Item
%key: Description

Description: 2cm metal soldier WWII
Amount: 2111

Description: Flying Helicopter Indoor Maxi
Amount: 8

...

It would be natural to identify the items by their descriptions, but it is also error prone: was it “Flying Helicopter Indoor Maxi” or “Flying Helicopter Maxi Indoor”? Was “Helicopter” in lower case or upper case?

Thus it is quite common in databases to use some kind of numeric “Id” to uniquely identify items like those ones, because numbers are easy to increment and manipulate. So we could add a new numeric Id field and use it as the primary key:

%rec: Item
%key: Id
%mandatory: Description

Id: 0
Description: 2cm metal soldier WWII
Amount: 2111

Id: 1
Description: Flying Helicopter Indoor Maxi
Amount: 8

...

A problem with this approach is that we must be careful to not assign already used ids when we introduce more articles in the database. Other than its uniqueness, it is not important which number is associated with which article.

To ease the management of those Ids database systems use to provide a facility called “auto-counters”. Auto-counters can be implemented in recfiles using the %auto directive in the record descriptor. Its usage is:

%auto: field1 field2 ... fieldN

The list of field names are separated by one or more blank characters. There can be several %auto fields in the same record descriptor, the effective list of auto-generated fields being the union of all the entries.

When recins inserts a new record in the recfile, it looks for any declared auto field. If any of these fields are not provided explicitly in the command line then recins generates them along with the user-provided fields. Such auto fields are generated at the beginning of the new records, in the same order they are found in the %auto directives.

For example, consider a items.rec database with an empty record set:

%rec: Item
%key: Id
%auto: Id
%mandatory: Description

If we insert a new record and we do not specify an Id then it will be generated automatically by recins:

$ recins -t Item -f Description -v 'recutils t-shirts' \
         -f Amount -v 200 \
         items.rec
$ cat items.rec
%rec: Item
%key: Id
%auto: Id
%mandatory: Description

Id: 0
Description: recutils t-shirts
Amount: 200

The concrete effect of the %auto directive depends on the type of the affected field. The following sections document how.