4 Database Manipulation

The DBFIND Statement

DBFIND is a dual-purpose statement: DBFIND is used with detail data sets, and locates the head of a chain in a master data set whose search-item value is identified by the argument parameter.

DBFIND is used with indexed access and establishes current index and locates matching value in index table.

DBFIND (base name, data set, mode, status, item, argument)

The parameters are:

base name
The string variable used when opening the database.
data set
Either a string variable containing a left-justified data set name or a numeric variable containing a data set number corresponding to the data set's position in the schema definition.
mode
A numeric expression equal to 1, 2, 3, 4, 5.
status
An integer array variable that returns status information after DBFIND is executed. The array must contain at least ten elements in its right-most dimension.
item
A string expression containing a left-justified search item or index item name, or a numeric expression containing a search item or index item number. The search item number represents the relative position in the (index item part) schema definition starting with the number of defined items plus 1; search item or index item numbers are integers ranging from 1 to 1024.
argument
Contains a value for the search item or index item to be used.

DBFIND Modes

DBFIND is used to establish current chain or current index. The mode and item parameters determine the type of access requested - chained or indexed.

Mode 1: Find chain head. If the item refers to a search item (an item linked to a master set in schema definition), DBFIND will set up the current chain and locate the chain head. The specified search item defines the path to which the chain belongs. The data set parameter must reference a detail data set. DBFIND uses the argument value to locate the desired chain head in the master data set (using calculated access). The argument and the search item data types must match. DBFIND converts numeric arguments to the search item numeric data type during execution.

First matching record. If an item refers to an index item, DBFIND will set up the current index and locate the first matching value in the index. If the argument is an empty string, the first record (in index order) will be located. (See discussion below on comparison between modes 1 and 2.)

Mode 2: First matching record. The item parameter must refer to an index item. DBFIND will set up the current index and locate the first matching value in the index. If the argument is an empty string, the first record (in index order) will be located.

Mode 3: Last matching record. The item parameter must refer to an index item. DBFIND will set up the current index and locate the last matching value in the index. If the argument is an empty string, the last record (in index order) will be located.

Mode 4: Find first matching record with matching regular expression. The item parameter must refer to an index item. DBFIND will set up the current index and locate the first matching value in the index. The index item must contain at least one leading string segment.

The given expression must describe the leading string segments exactly. There is no implicit '*' at the end (as in DBFIND Modes 2/3). If you store "AAA " (trailing space), in an entry, you won't find it using a value of "AAA", but you will find it if you use "AAA*" or "AAA?".

The entries will be retrieved using DBGET Mode 5/6 in index order.

Mode 5: Find last record with matching regular expression. Same as Mode 4, but locates the last entry.

NOTE: Access time depends on the regular expression given. We do not recommend specifying a character class or a wildcard character at the beginning of the regular expression, as this would result in a serial access to specified data set.

NOTE: Status may return to 0 in the first status array element, although there is no matching entry in the dataset. A subsequent DBGET will return 15 (end-of-chain) in the first status array element.

Chained Access

DBFIND verifies that the item parameter references a search item for the specified detail data set. It then locates the appropriate master data set entry whose search item value (or key) matches the value of the argument parameter. The internal status information relative to the data set parameter is adjusted in anticipation of subsequent chained references to that same data set (DBGET, modes 5, 6.). Note that DBFIND does not retrieve data entries; it simply establishes the current record pointer.

Indexed Access

DBFIND verifies that the item parameter references an index item for the specified data set. It then locates the argument value in the appropriate index. If the argument parameter is an empty string, this is simply the first or last record (in index order). If a matching value cannot be found, the record pointer will be located at the position in the index where the requested value would be inserted.

The numeric argument may be given as a string value independent of index item definition. String value must be set up by PACK USING statement. It is valid to give a shorter search value for a string item. DBFIND will locate the first (or last) entry with matching value. If numeric items are truncated they will be ignored in locating index position.

If the first part of the index item referenced by the item parameter is numeric then the argument parameter may be numeric. DBFIND converts numeric arguments to the item numeric data type during execution. Only the first item is significant in locating index position.

DBFIND/DBGET modes with indexed access

 
DBFIND mode DBGET mode Relation Comment
25equalfirst record with matching index value
26 end-of-chain
215equal/greater 
216lesslast record before matching index volume
35 end-of-chain
36equallast record with matching index value
315greaterfirst record after matching index value
316less/equal 

Mode 1 v. Mode 2

DBFIND mode 1 and mode 2 both locate the first matching record. DBFIND mode 1 will return status array elements 6, 8, 10, while DBFIND mode 2 will not (they are zero). DBFIND mode 1 using index item is a convenient way to extend a master/detail database design without changing your programs. (Note that DBGET in index order will not return status elements 6 or 8.) But this has a great disadvantage: it results in the data set entries being read twice, the first time to locate first/last record address and number of records, the second time if you retrieve the data using DBGET.

NOTE: Do not use DBFIND mode 1 on index items if you are expecting a large number of data set entries.

DBFIND Status Array

A DBFIND error assigns a non-zero conditional word (CW) to the first element of the status array. A list of all CW values and their meanings appears in page 197 . The following table describes the status array contents after a successful DBFIND.

 
Array Element Value Description
10CW.
20 
30 
40 
50 
6Chain LengthInteger count of entries in the current chain.
70 
8End of Chain AddressInteger address of the last record in the chain.
90 
10Chain Head AddressInteger address of the first record in the chain.

Status array elements 6, 8, 10 are zero in DBFIND modes 2 and 3.

Regular Expressions

Elements:

[
starting delimiter of character class expression
]
ending delimiter of character class expression
!
negation expression (only as 1st character of character class)
-
range expression (only inside a character class)
?
any character
*
any string (including the empty string)
#
numeric character (same as [0-9])
The backslash character (\) loses its special meaning within the delimiters, except in the following combinations:

\b - becomes backspace

\t - becomes tab

\r - becomes cr

\n - becomes lf

\f - becomes ff

\s - becomes space

The above combinations conform to the HP-UX standard, and are extremely practical.

Evaluation An evaluation is only possible with index items, and then only for leading string segments. Index items without leading string segments cannot be accessed.

A regular expression must exactly describe the contents of the leading string segments. There is no implicit "*" at the end (as in DBFIND 2/3). For example, the value "AAA "(trailing space) does not match the search expression "AAA".

Examples of regular expressions:

A[BCD]
Index value starts with A, followed by either a B, C or D.
BOB?*
Index value starts with BOB, followed by at least one character.

Eloquence Database Manual - 19 DEC 2002