Name: DATABASE-QUERY
Section:
Database Commands
Synopsis:
<database-query DBVAR EXPR [FORMAT=FEXPR] [KEYS=VARNAME] [SORT=FIELD1[] [FIELD2...]] [SORTORDER=REVERSE]>
Description:
Select records from the database referenced by DBVAR, based on EXPR, perhaps sorting them based on the data in FIELDs, and optionally formatting them with the Meta-HTML expression in FEXPR.
Both EXPR and FEXPR are evaluated with the fields of the database as the current package, so references to variables which are not part of the record structure must be fully qualified (i.e., by prefixing them with a package name. See Packages for more information.) If the result of evaluating EXPR is not the empty string, then the record is selected for further processing by either FORMAT, KEYS, or to be returned in a plain text list of keys.
The order of evaluation is as follows:
First, the database is queried, and an internal list of selected records is created. Then, if SORT=FIELD1,FIELD2... is present, the records are sorted using the values of the specified fields. The order of the sort can be reversed by passed SORTORDER=REVERSE.
Next, if FORMAT=FEXPR is present, FEXPR is executed for each record, in an environment where the current package consists of bindings for each field's name to each field's value, and the special binding of key
to the key of that record. Finally, if KEYS=VARNAME is present, VARNAME is the name of a variable into which the matched keys are stored as an array.
The SORT parameters indicates which fields to sort the resultant keys on; the field names FIELD1,FIELD2... are separated by commas.
Examples:
This simple query stores the key of every record in the database whose name field contains "Fox" into the variable named dbkeys:
<with-open-database db "/www/data/employee.db" mode=read>
<database-query db <match <get-var name> "Fox"> keys=dbkeys>
</with-open-database>
This more complex query formats an alphabetical list of all the members of the database who are employed as carpenters, and are between the ages of 20 and 45, by creating links to a page which will (presumably) get a more detailed listing of the individual record:
<with-open-database db "employee.db" mode=read>
<database-query db sort=LastName
<and <match <get-var job> "carpent">
<gt age 20>
<lt age 45>>
format = <prog
<a href="detail-display.mhtml?<cgi-encode key>">
<get-var LastName>, <get-var FirstName>
</a>: <get-var Job>, <get-var Age>>>
</with-open-database>
Edit Pointer
Function Index
Variable Index
Variable Editor

The
META-HTML
Reference Manual V1.4
Copyright © 1995, 1996,
Brian J. Fox,
1996, 1997 Universal Access Inc.
Found a bug? Send mail to
bug-manual@metahtml.com