Name

DO-QUERY — Iterate over all the tuples of a query

Macro

Syntax

do-query ((&rest args) query-expression &key database types) &body body => nil

Arguments and Values

args

A list of variable names.

query-expression

An sql expression that represents an SQL query which is expected to return a (possibly empty) result set, where each tuple has as many attributes as function takes arguments.

database

A database object. This will default to *default-database*.

types

A field type specififier. The default is NIL. See query for the semantics of this argument.

body

A body of Lisp code, like in a destructuring-bind form.

Description

Executes the body of code repeatedly with the variable names in args bound to the attributes of each tuple in the result set returned by executing the SQL query-expression on the database specified.

The body of code is executed in a block named nil which may be returned from prematurely via return or return-from. In this case the result of evaluating the do-query form will be the one supplied to return or return-from. Otherwise the result will be nil.

The body of code appears also is if wrapped in a destructuring-bind form, thus allowing declarations at the start of the body, especially those pertaining to the bindings of the variables named in args.

Examples

(do-query ((salary name) "select salary,name from simple")
  (format t "~30A gets $~2,5$~%" name (read-from-string salary)))
>> Mai, Pierre                    gets $10000.00
>> Hacker, Random J.              gets $08000.50
=> NIL

(do-query ((salary name) "select salary,name from simple")
  (return (cons salary name)))
=> ("10000.00" . "Mai, Pierre")
	

Side Effects

Whatever effects the execution of the SQL query has on the underlying database, if any.

Affected by

None.

Exceptional Situations

If the execution of the SQL query leads to any errors, an error of type clsql-sql-error is signalled.

If the number of variable names in args and the number of attributes in the tuples in the result set don't match up, an error is signalled.

See Also

query
map-query

Notes

None.