Scheme-PG

David J. Neu
djneu at acm dot org

Version 0.4.0
March 24, 2005



{{note by corey: I stole the code and documentation from "scheme-pg". We section 9 - "SQL Statment Creation" of the scheme-pg manual for instructions on how to use. a example to start you off: type: (select all (table1 table2))

you should get: "SELECT * FROM \"table1\",\"table2\" "

so use (select all (table1 table2)) instead of: "SELECT * FROM table1,table2" when programing.


(i hacked out everything but sectoin 9 of the scheme-pg manual for you)}}


9  SQL Statement Creation

In order to create SQL statements, programmers frequently utilize error prone and often syntactically unappealing string formatting operations. Taking a cue from Scheme-QL, Scheme-PG provides a set of procedures and macros that allow programmers to create SQL statements using a little language implemented in Scheme. While this little language is still under development, it implements enough of SQL that it should prove useful for many applications.

Scheme-PG introduces a set of new forms that mirror a subset of the SQL language functions such as SELECT, INSERT, UPDATE and DELETE. These new forms return valid SQL statements as Scheme strings, are not dependent on other Scheme-PG functionality, and therefore can be used by themselves to support SQL statement creation for use with other Scheme database packages.1

In the new forms introduced by Scheme-PG

Scheme-PG requires that Scheme symbols appear in positions where SQL objects should appear (e.g. a column list in a SELECT statement) and it requires that Scheme strings or numbers appear in positions where SQL values should appear (e.g. a value in an INSERT statement). Scheme-PG handles proper formatting of values. Specifically, the escape-string procedure, (see §8) is called on strings and the result is surrounded by single quotes. Scheme-PG also formats SQL objects by surrounding them with double quotes so that table or column names can for example contain spaces or dashes. Positions in the forms where Scheme-PG expects an SQL object or SQL value are are quasiquoted, so unquoting (e.g. ,first-name ,(get-first-name)) can be utilized.

(where condition)      SYNTAX

where: condition --> string

where: and (listof conditions) --> string

where: or (listof conditions) --> string

where: not (listof conditions) --> string

The where macro supports the creation of WHERE clauses for use in SQL statements such as SELECT, UPDATE and DELETE. Fundamental to the where clause is a condition which is a length three proper list (operator column value). In a condition, a valid operator is a symbol, a valid column is either a symbol representing a column name or a length two proper list of symbols representing a table name and a column name, and a valid value is either a valid column or a string or number. The case of value being a valid column occurs in a condition in which the values of two database columns are compared. For example,

The SQL AS syntax is supported as shown in the following examples,

The case of value being a string or number occurs in a condition in which the value of a database column is compared to a constant. For example,

If value is a valid column it is formatted as described above. If value is not a valid column then, if it is a string it is formatted as described above, if it is a number it is returned unformatted and in all other cases an exception is raised. An example of an additional Scheme data type that could be supported as a value in future releases is a list, which could be used in an SQL IN, e.g. SELECT * FROM addr WHERE state IN ('NH', 'NJ', 'NY').

Scheme-PG tranforms a condition (operator column value) into the string ``column operator value'' formatting its components as described above. The following examples correspond to the four SELECT statements listed above

The where macro accepts either a single condition or the literals and, or and not and a list of conditions. Programmers can build where clauses that contain arbitrarily complex Boolean expression through use of and, or and not as shown by the following examples

(select acolumns atables aclause ...)      SYNTAX

select: (listof symbol/(table column)) (listof symbol/(schema table)) clause ... --> string

select: all (listof symbol/(table column)) clause ... --> string

The select macro supports the creation of SQL SELECT statements. It accepts a list of columns either as symbols or as length two proper lists consisting of a table name and a column name, a list of table names as either symbols or as length proper lists consisting of a schema name and a table name, and a arbitrary number of clauses as strings. In lieu of the list of column names the single literal all can be provided to generate the * in the common SELECT * [scheme-pg-Z-G-31.gif] statement. The select-distinct macro is the same as the select macro except that it creates a SELECT DISTINCT rather statement than a SELECT statement. The SQL AS syntax is also supported. The following examples demonstrate the use of the select marco.

(insert atable avalues)      SYNTAX
insert: symbol (listof strings and/or numbers) --> string

(insert atable acolumns avalues)      SYNTAX
insert: symbol (listof symbols) (listof strings and/or numbers) --> string

(insert atable acolumns-values)      SYNTAX
insert: symbol (listof (symbols . string and/or number)) --> string

(insert atable acolumns-values)      SYNTAX
insert: symbol (listof (symbols string and/or number)) --> string

The insert macro has four rules that support the creation of INSERT statements as shown in the examples below. The following form implements the values-only format of an INSERT statement.

The following forms all generate the same INSERT statement.

(delete atable)      SYNTAX
delete: symbol --> string

(delete atable awhere)      SYNTAX
delete: symbol where --> string

The delete macro has two rules that support the creation of DELETE statements as shown in the examples below. The where clause, in the second instance is a string.

(update atable acolumns-values)      SYNTAX
update: symbol (listof (symbols . string and/or number)) --> string

(update atable acolumns-values)      SYNTAX
update: symbol (listof (symbols string and/or number)) --> string

(update atable acolumns-values where)      SYNTAX
update: symbol (listof (symbols . string and/or number)) where --> string

(update atable acolumns-values where)      SYNTAX
update: symbol (listof (symbols string and/or number)) where --> string

The update macro has four rules that support the creation of UPDATE statements as shown in the examples below. The following two forms both construct the same UPDATE statement:

The following two forms both construct the same UPDATE statement. The where clause in these instances are strings.

(limit offset number)      SYNTAX
limit: non-negative-integer non-negative-integer --> string
The limit macro creates a LIMIT clause for use in a SELECT statement. The offset argument is the number of rows to be skipped, so an offset of 0 means that the first row returned will be the first row of the result. The number argument is an upper bound on the number of rows to return. Less than number rows can be returned depending on the number of rows in the result and the value of offset.

(order-by column asc/desc)      SYNTAX
order-by: (listof (symbol 'asc or 'desc))) --> string
The order-by macro creates an ORDER BY clause for use in a SELECT. It accepts a proper list of length two lists that consists of a symbol representing a column name and either the symbol asc (to indicate the result should be put in ascending order) or the symbol desc (to indicate the result should be put in descending order).

References

[1]   Harold Abelson and Gerald Jay Sussman with Julie Sussman, Structure and Interpretation of Computer Programs, second edition, The MIT Press, Cambridge, Massachusetts, 1996.

[2]   Philip L. Bewig, SRFI 40: A Library of Streams, http://srfi.schemers.org/srfi-40/, 2003.

[3]   Ryan Culpepper, spgsql: A PostgreSQL Database Library, http://schematics.sourceforge.net/spgsql.html.

[4]   R. Kent Dybvig, The Scheme Programming Language, third edition, The MIT Press, Cambridge, Massachusetts, 2003.

[5]   Daniel P. Friedman and Matthias Felleisen, The Little Schemer, fourth edition, The MIT Press, Cambridge, Massachusetts, 1996.

[6]   Daniel P. Friedman and Matthias Felleisen, The Seasoned Schemer, The MIT Press, Cambridge, Massachusetts, 1996.

[7]   Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, second edition, Prentice Hall, Englewood Cliffs, New Jersey, 1988.

[8]   Noel Welsh, Francisco Solsona and Ian Glover, SchemeUnit and SchemeQL: Two Little Languages, Scheme 2002 Workshop, Pittsburgh, Pennsylvania, October 3, 2002.

[9]   PLT Scheme, http://www.plt-scheme.org.

[10]   PostgreSQL, http://www.postgresql.org.


1 To use the little language feature by itself simply (require (lib "sql.ss" "scheme-pg")).

Last modified: Thursday, March 24th, 2005
HTML conversion by TeX2page 2004-09-11