Postmodern reference manual

This is the reference manual for the component named postmodern, which is part of a library of the same name.

Note that this package also exports the database-connection and database-error types from CL-postgres, and a few operators from S-SQL.

query, execute, and any other function that would logically need to communicate with the database will raise a condition of the type database-error when something goes wrong. As a special case, errors that break the connection (socket errors, database shutdowns) will be raised as subtypes of database-connection-error, providing a :reconnect restart to re-try the operation that encountered to the error.

Contents

  1. Connecting
  2. Querying
  3. Inspecting the database
  4. Database access objects
  5. Table definition and creation
  6. Symbol-index

Connecting

class database-connection

Objects of this type represent database connections.

function connect (database user password host &key (port 5432) pooled-p use-ssl)
→ database-connection

Create a new database connection for the given user and database. Port will default to 5432, which is where most PostgreSQL server are running. If pooled-p is true, a connection will be taken from a pool of connections of this type, if one is available there, and when the connection is disconnected it will be put back into this pool instead. use-ssl can be :no, :yes, or :try, as in open-database, and defaults to the value of *default-use-ssl*.

variable *default-use-ssl*

The default for connect's use-ssl argument. This starts at :no. If you set it to anything else, be sure to also load the CL+SSL library.

method disconnect (database-connection)

Disconnects a normal database connection, or moves a pooled connection into the pool.

function connected-p (database-connection)
→ boolean

Returns a boolean indicating whether the given connection is still connected to the server.

method reconnect (database-connection)

Reconnect a disconnected database connection. This is not allowed for pooled connections ― after they are disconnected they might be in use by some other process, and should no longer be used.

variable *database*

Special variable holding the current database. Most functions and macros operating on a database assume this contains a connected database.

macro with-connection (spec &body body)

Evaluates the body with *database* bound to a connection as specified by spec, which should be list that connect can be applied to.

macro call-with-connection (spec thunk)

The functional backend to with-connection. Binds *database* to a new connection as specified by spec, which should be a list that connect can be applied to, and runs the zero-argument function given as second argument in the new environment. When the function returns or throws, the new connection is disconnected.

function connect-toplevel (database user password host &key (port 5432))

Set *database* to a new connection. Use this if you only need one connection, or if you want a connection for debugging from the REPL.

function disconnect-toplevel ()

Disconnect *database*.

function clear-connection-pool ()

Disconnect and remove all connections in the connection pools.

variable *max-pool-size*

Set the maximum amount of connections kept in a single connection pool, where a pool consists of all the stored connections with the exact same connect arguments. Defaults to NIL, which means there is no maximum.

Querying

macro query (query &rest args/format)
→ result

Execute the given query, which can be either a string or an S-SQL form (list starting with a keyword). If the query contains placeholders ($1, $2, etc) their values can be given as extra arguments. If one of these arguments is a keyword occuring in the table below, it will not be used as a query argument, but will determine the format in which the results are returned instead. Any of the following formats can be used, with the default being :rows:

:noneIgnore the result values.
:lists, :rowsReturn a list of lists, each list containing the values for a row.
:list, :rowReturn a single row as a list.
:alistsReturn a list of alists which map column names to values,with the names represented as keywords.
:alistReturn a single row as an alist.
:str-alistsLike :alists, but use the original column names.
:str-alistReturn a single row as an alist, with strings for names.
:plistsReturn a list of plists which map column names to values,with the names represented as keywords.
:plistReturn a single row as a plist.
:columnReturn a single column as a list.
:singleReturn a single value.
:single!Like :single, but raise an error when the number of selected rows is not equal to 1.

If the database returns information about the amount rows that were affected, such as with updating or deleting queries, this is returned as a second value.

macro execute (query &rest args)

Like query, but called with format :none, and returning the amount of affected rows as its first returned value. (Also returns this amount as the second returned value, but use of this is deprecated.)

macro doquery (query (&rest names) &body body)

Execute the given query (a string or a list starting with a keyword), iterating over the rows in the result. The body will be executed with the values in the row bound to the symbols given in names. To iterate over a parameterised query, one can specify a list whose car is the query, and whose cdr contains the arguments. For example:

(doquery (:select 'name 'score :from 'scores) (n s)
  (incf (gethash n *scores*) s))

(doquery ((:select 'name :from 'scores :where (:> 'score '$1)) 100) (name)
  (print name))

macro prepare (query &optional (format :rows))
→ function

Creates a function that can be used as the interface to a prepared statement. The given query (either a string or an S-SQL form) may contain placeholders, which look like $1, $2, etc. The resulting function takes one argument for every placeholder in the query, executes the prepared query, and returns the result in the format specified (allowed formats are the same as for query).

For queries that have to be run very often, especially when they are complex, it may help performance if the server only has to plan them once. See the PostgreSQL manual for details.

In some cases, the server will complain about not being able to deduce the type of the arguments in a statement. In that case you should add type declarations (either with the :: syntax or with S-SQL's :type construct) to help it out.

macro defprepared (name query &optional (format :rows))

This is the defun-style variant of prepare. It will define a top-level function for the prepared statement.

macro with-transaction ((&optional name) &body body)

Execute the given body within a database transaction, committing it when the body exits normally, and aborting otherwise. An optional name can be given to the transaction, which can be used to force a commit or abort before the body unwinds.

function commit-transaction (transaction)

Commit the given database transaction.

function abort-transaction (transaction)

Roll back the given database transaction.

macro with-savepoint (name &body body)

Can only be used within a transaction. Establishes a savepoint with the given name at the start of body, and binds the same name to a handle for that savepoint. At the end of body, the savepoint is released, unless a condition is thrown, in which case it is rolled back.

function release-savepoint (savepoint)

Explicitly release the given savepoint.

function rollback-savepoint (transaction)

Roll back the given savepoint.

function sequence-next (sequence)
→ integer

Get the next value from a sequence. The sequence identifier can be either a string or a symbol, in the latter case it will be converted to a string with S-SQL rules.

function coalesce (&rest arguments)
→ value

Returns the first non-NIL, non-null (as in :null) argument, or NIL if none are present. Useful for providing a fall-back value for the result of a query, or, when given only one argument, for transforming :nulls to NIL.

Inspecting the database

function list-tables (&optional strings-p)
→ list

Returns a list of the tables in the current database. When strings-p is true, the names will be given as strings, otherwise as keywords.

function table-exists-p (name)
→ boolean

Tests whether a table with the given name exists. The name can be either a string or a symbol.

function table-description (name)
→ list

Returns a list of the fields in the named table. Each field is represented by a list of three elements: the field name, the type, and a boolean indicating whether the field may be null.

function list-sequences (&optional strings-p)
→ list

Returns a list of the sequences in the current database. When strings-p is true, the names will be given as strings, otherwise as keywords.

function sequence-exists-p (name)
→ boolean

Tests whether a sequence with the given name exists. The name can be either a string or a symbol.

function list-views (&optional strings-p)
→ list

Returns a list of the views in the current database. When strings-p is true, the names will be given as strings, otherwise as keywords.

function view-exists-p (name)
→ boolean

Tests whether a view with the given name exists. The name can be either a string or a symbol.

Database access objects

Postmodern contains a simple system for defining CLOS classes that represent rows in the database. This is not intended as as a full-fledged object-relational magic system ― while serious ORM systems have their place, they are notoriously hard to get right, and are outside of the scope of a humble SQL library like this.

metaclass dao-class

At the heart of Postmodern's DAO system is the dao-class metaclass. It allows you to define classes for your database-access objects as regular CLOS classes. Some of the slots in these classes will refer to columns in the database. To specify that a slot refers to a column, give it a :col-type option containing an S-SQL type expression (useful if you want to be able to derive a table definition from the class definition), or simply a :column option with value t. Such slots can also take a :col-default option, used to provide a database-side default value as an S-SQL expression.

DAO class definitions support two extra class options: :table-name to give the name of the table that the class refers to (defaults to the class name), and :keys to provide a set of primary keys for the table. When no primary keys are defined, operations such as update-dao and get-dao will not work.

Simple example:

(defclass user ()
  ((name :col-type string :initarg :name :accessor user-name)
   (creditcard :col-type (or db-null integer) :initarg :card :col-default :null)
   (score :col-type bigint :col-default 0 :accessor user-score))
  (:metaclass dao-class)
  (:keys name))

The (or db-null integer) form is used to indicate a column can have NULL values.

When inheriting from DAO classes, a subclass' set of columns also contains all the columns in its superclasses. The primary key for such a class is the union of its own keys and all the keys from its superclasses. Classes inheriting from DAO classes should probably always use the dao-class metaclass themselves.

When a DAO is created with make-instance, it can be passed a :fetch-defaults keyword parameter which, when true, will cause a query to be made to fetch the default values for all slots that have column default values and were not bound through initargs. In some cases, such as serial columns, which have an implicit default, this will not work. You can work around this by creating your own sequence and defining a (:nextval "my_sequence") default.

Finally, DAO class slots can have an option :ghost t to specify them as ghost slots. These are selected when retrieving instances, but not written when updating or inserting, or even included in the table definition. The only know use for this to date is to create your table with (oids=true), and specify a slot like this:

(oid :col-type integer :ghost t :accessor get-oid)

method dao-keys (class)
→ list

Returns list of slot names that are the primary key of DAO class CLASS.

method dao-keys (dao)
→ list

Returns list of values that are the primary key of DAO.

method dao-exists-p (dao)
→ boolean

Test whether a row with the same primary key as the given DAO exists in the database. Will also return NIL when any of the key slots in the object are unbound.

method get-dao (type &rest keys)
→ dao

Select the DAO object from the row that has the given primary key values, or NIL if no such row exists. Objects created by this function will have initialize-instance called on them (after loading in the values from the database) without any arguments ― even :default-initargs are skipped. The same goes for select-dao and query-dao.

macro select-dao (type &optional (test t) &rest sort)
→ list

Select DAO objects for the rows in the associated table for which the given test (either an S-SQL expression or a string) holds. When sorting arguments are given, which can also be S-SQL forms or strings, these are used to sort the result. (Note that, if you want to sort, you have to pass a test value.)

(select-dao 'user (:> 'score 10000) 'name)

function query-dao (type query)
→ list

Execute the given query (which can be either a string or an S-SQL expression) and return the result as DAOs of the given type. The names of the fields returned by the query must either match slots in the DAO class, or be bound through with-column-writers.

variable *ignore-unknown-columns*

Normally, when get-dao, select-dao, or query-dao finds a column in the database that's not in the DAO class, it will raise an error. Setting this variable to a truthy value will cause it to simply ignore the unknown column.

method insert-dao (dao)
→ dao

Insert the given DAO into the database. When any column slots in the object are unbound, these will be updated with the values they default to in the database. (If they have no defaults, it is an error to insert them.) Note: This feature only works on PostgreSQL 8.2 and up. On older versions, do not insert DAOs with unbound slots.

method update-dao (dao)
→ dao

Update the representation of the given DAO in the database with the values in the object. This is not defined for tables that do not have any non-primary-key columns. Raises an error when no row matching the DAO exists.

function save-dao (dao)
→ boolean

Tries to insert the given DAO using insert-dao. If this raises a unique key violation error, it tries to update it using update-dao instead. Be aware that there is a possible race condition here ― if some other process deletes the row at just the right moment, the update fails as well. Returns a boolean telling you whether a new row was inserted.

This function is unsafe to use inside of a transaction ― when a row with the given keys already exists, the transaction will be abandoned. Use save-dao/transaction instead in such a situation.

function save-dao/transaction (dao)
→ boolean

Acts exactly like save-dao, except that it protects its attempt to insert the object with a rollback point, so that a failure will not abort the transaction.

method delete-dao (dao)

Delete the given DAO from the database.

function dao-table-name (table)
→ string

Get the table name associated with the given DAO class (or symbol naming such a class).

function dao-table-definition (table)
→ string

Given a DAO class, or the name of one, this will produce an SQL query string with a definition of the table. This is just the bare simple definition, so if you need any extra indices or or constraints, you'll have to write your own queries to add them.

macro with-column-writers ((&rest writers) &body body)

Provides control over the way get-dao, select-dao, and query-dao read values from the database. This is not commonly needed, but can be used to reduce the amount of queries a system makes. writers should be a list of alternating column names (strings or symbols) and writers, where writers are either symbols referring to a slot in the objects, or functions taking two arguments ― an instance and a value ― which can be used to somehow store the value in the new instance. When any DAO-fetching function is called in the body, and columns matching the given names are encountered in the result, the writers are used instead of the default behaviour (try and store the value in the slot that matches the column name).

A common use for this is to add some non-column slots to a DAO class, and use query-dao within a with-column-writers form to pull in extra information about the objects, and imediately store it in the new instances.

Table definition and creation

It can be useful to have the SQL statements needed to build an application's tables available from the code, to do things like automatically initialising a database. The following macro and functions allow you to group sets of SQL statements under symbols, with some shortcuts for common elements in table definitions.

macro deftable (name &body definition)

Define a table. name can be either a symbol or a (symbol string) list. In the first case, the table name is derived from the symbol by S-SQL's rules, in the second case, the name is given explicitly. The body of definitions can contain anything that evaluates to a string, as well as S-SQL expressions. In this body, the variables *table-name* and *table-symbol* are bound to the relevant values. Note that the definitions are evaluated in order, so you'll generally want to first create your table and then start defining indices on it.

function create-table (symbol)

Creates the table identified by symbol by executing the result of all the forms in its definition.

function create-all-tables ()

Creates all defined tables.

function create-package-tables (package)

Creates all tables whose identifying symbol is interned in the given package.

variables *table-name*, *table-symbol*

These are bound to the relevant symbol and name while the clauses of a table definition are evaluated. Can be used to define shorthands like the ones below.

function !dao-def ()

Should only be used inside deftable forms. Adds the result of calling dao-table-definition on *table-symbol* to the definition.

function !index (&rest columns), !unique-index (&rest columns)

Define an index on the table being defined. The columns can be given as symbols or strings.

function !foreign (target-table columns &optional target-columns &key on-delete on-update)

Add a foreign key to the table being defined. target-table is the table the index refers to, columns is a list of column names or single name in this table, and, if the columns have different names in the table referred to, target-columns should be another list of names or single name for the target table.

The on-delete and on-update arguments can be used to specify ON DELETE and ON UPDATE actions, as per the keywords allowed in create-table. Note that they are not really &key arguments, but rather are picked out of a &rest arg at runtime, so that they can be specified even when target-columns is not given.

Symbol-index