6 Queries
The sql form is used to enter the query language, and unquote is used to escape back into Scheme. Because sql statements are executed in the DBMS, the semantics of the query language are closer to SQL than Scheme.
Here is an example of a simple query:
; (listof person) |
; Find all people in the database: |
(find-all (sql (select #:from person))) |
The sql block describes the query to perform. The select form creates a SELECT statement that selects all person records in the database. The find-all procedure sends the query to the database, retrieves all the available query results, and turns them into a list.
Snooze automatically infers the type of query result. This example is selecting person records, so the results of the find-all is a (listof person).
The query language lets you customise the type of result in various ways. For example:
; (listof string) |
; Find the names of all people from the database. |
(find-all (sql (select #:what person.name |
#:from person))) |
; (listof (list string integer)) |
; Find the names and ages of all people from the database. |
(find-all (sql (select #:what (person.name person.age) |
#:from person))) |
In the query language, dotted operators in identifiers are treated like operators meaning "attribute of". For example, person.name means "the name attribute of the person entity". The part before the dot has to be an entity or entity alias (see below).
6.1 Query methods/procedures
snooze<%> supplies three query methods that retrieve data in different ways:
find-all retrieves a list of all matching results from the database.
find-one is like find-all but returns the first result found. If no results are found, returns #f instead.
(part ("(planet gen.ss (untyped unlib.plt 3 23))" "top"))
g:find is like find-all but returns a result generator.Generators, defined in Unlib, are a lightweight iteration mechanism for traversing and modifying large datasets where menory consumption is a concern. Unlib provides useful combinators for manipulating generated data, including memory-efficient equivalents of fold, map and for-each.
6.2 Query syntax
Formally, sql blocks have the syntax below. The parts of the syntax are described in subsequent sections: