Common Lisp Package: S-SQL

README:

FUNCTION

Public

ENABLE-S-SQL-SYNTAX (&OPTIONAL (CHAR Q))

Enable a syntactic shortcut #Q(...) for (sql (...)). Optionally takes a character to use instead of #\Q.

FROM-SQL-NAME (STR)

Convert a string to something that might have been its original lisp name (does not work if this name contained non-alphanumeric characters other than #-)

SQL-ESCAPE-STRING (STRING &OPTIONAL PREFIX)

Escape string data so it can be used in a query.

TO-SQL-NAME

Convert a symbol or string into a name that can be an sql table, column, or operation name. Add quotes when escape-p is true, or escape-p is :auto and the name contains reserved words.

Undocumented

SQL-COMPILE (FORM)

SQL-ERROR (CONTROL &REST ARGS)

SQL-TEMPLATE (FORM)

Private

DISSECT-TYPE (TYPE)

Return the type and whether it may be NULL.

ESCAPE-SQL-EXPRESSION (EXPR)

Try to escape an expression at compile-time, if not possible, delay to runtime. Used to create stored procedures.

EXPAND-JOINS (ARGS)

Helper for the select operator. Turns the part following :from into the proper SQL syntax for joining tables.

IMPLODE (SEP LIST)

Reduce a list of strings to a single string, inserting a separator between them.

MAKE-EXPANDER (ARITY NAME)

Generates an appropriate expander function for a given operator with a given arity.

REDUCE-STRINGS (LIST)

Join adjacent strings in a list, leave other values intact.

SPLIT-ON-KEYWORDS% (SHAPE LIST)

Helper function for split-on-keywords. Extracts the values associated with the keywords from an argument list, and checks for errors.

SQL-EXPAND (ARG)

Compile-time expansion of forms into lists of stuff that evaluates to strings (which will form an SQL query when concatenated).

SQL-EXPAND-LIST (ELTS &OPTIONAL (SEP , ))

Expand a list of elements, adding a separator in between them.

STRCAT (ARGS)

Concatenate a list of strings into a single one.

TO-TYPE-NAME (TYPE)

Turn a Lisp type expression into an SQL typename.

Undocumented

%BUILD-FOREIGN-REFERENCE (TARGET ON-DELETE ON-UPDATE)

DEQUOTE (VAL)

EXPAND-CREATE-INDEX (NAME ARGS)

EXPAND-FOREIGN-ON* (ACTION)

EXPAND-ROWS (ROWS LENGTH)

EXPAND-TABLE-COLUMN (COLUMN-NAME ARGS)

EXPAND-TABLE-CONSTRAINT (OPTION ARGS)

FOR-UPDATE/SHARE (SHARE-OR-UPDATE FORM &REST ARGS)

S-SQL-READER (STREAM CHAR MIN-ARGS)

SQL-EXPAND-NAMES (NAMES &OPTIONAL (SEP , ))

MACRO

Public

REGISTER-SQL-OPERATORS (ARITY &REST NAMES)

Define simple operators. Arity is one of :unary (like 'not'), :unary-postfix (the operator comes after the operand), :n-ary (like '+': the operator falls away when there is only one operand), :2+-ary (like '=', which is meaningless for one operand), or :n-or-unary (like '-', where the operator is kept in the unary case). After the arity follow any number of operators, either just a keyword, in which case the downcased symbol name is used as the operator, or a two-element list containing a keyword and a name string.

SQL (FORM)

Compile form to an sql expression as far as possible.

Private

DEF-SQL-OP (NAME ARGLIST &BODY BODY)

Macro to make defining syntax a bit more straightforward. Name should be the keyword identifying the operator, arglist a lambda list to apply to the arguments, and body something that produces a list of strings and forms that evaluate to strings.

SPLIT-ON-KEYWORDS (WORDS FORM &BODY BODY)

Used to handle arguments to some complex SQL operations. Arguments are divided by keywords, which are interned with the name of the non-keyword symbols in words, and bound to these symbols. After the naming symbols, a ? can be used to indicate this argument group is optional, an * to indicate it can consist of more than one element, and a - to indicate it does not take any elements.

Undocumented

DEF-DROP-OP (OP-NAME WORD)

GENERIC-FUNCTION

Public

SQL-ESCAPE (ARG)

Get the representation of a Lisp value so that it can be used in a query.

SQL-TYPE-NAME (LISP-TYPE &REST ARGS)

Transform a lisp type into a string containing something SQL understands. Default is to just use the type symbol's name.

Private

EXPAND-SQL-OP (OP ARGS)

For overriding expansion of operators. Default is to just place operator name in front, arguments between parentheses behind it.

VARIABLE

Public

*ESCAPE-SQL-NAMES-P*

Setting this to T will make S-SQL add double quotes around identifiers in queries. Setting it :auto will turn on this behaviour only for reserved words.

*STANDARD-SQL-STRINGS*

Indicate whether S-SQL will use standard SQL strings (just use '' for #'), or backslash-style escaping. Setting this to NIL is always safe, but when the server is configured to allow standard strings (parameter 'standard_conforming_strings' is 'on'), the noise in queries can be reduced by setting this to T.

Private

*POSTGRES-RESERVED-WORDS*

A set of all Postgres' reserved words, for automatic escaping.

Undocumented

*EXPAND-RUNTIME*

CLASS

Public

Undocumented

REAL

CONDITION

Public

Undocumented

SQL-ERROR (CONTROL &REST ARGS)