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.
Connecting
class database-connection
Representation of a database connection. Contains login information in order to be able to automatically re-establish a connection when it is somehow closed.
function connect (database user-name password host &key (port 5432) pooled-p use-ssl)
→ database-connection
Create a new database connection for the given user and the database. Port will default to 5432, which is where most PostgreSQL servers are running. If pooled-p is T, 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, :try, :require, :yes, or :full and defaults to the value of default-use-ssl
- :try means if the server supports it
- :require means use provided ssl certificate with no verification
- :yes means verify that the server cert is issued by a trusted CA, but does not verify the server hostname
- :full means expect a CA-signed cert for the supplied hostname and verify the server hostname
If you set it to anything other than :no be sure to also load the CL+SSL library.
variable default-use-ssl
The default for connect's use-ssl argument. Valid settings are :no, :try, :require, :yes, or :full :try means if the server supports it :require means use provided ssl certificate with no verification :yes means verify that the server cert is issued by a trusted CA, but does not verify the server hostname :full means expect a CA-signed cert for the supplied hostname and verify the server hostname If you set it to anything other than :no 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 connection information. Most functions and macros operating on a database assume this binds to 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-name password host &key (port 5432))
Bind the database to a new connection. Use this if you only need one connection, or if you want a connection for debugging from the REPL.
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 occurring 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:
:none | Ignore the result values. |
:lists, :rows | Return a list of lists, each list containing the values for a row. |
:list, :row | Return a single row as a list. |
:alists | Return a list of alists which map column names to values, with the names represented as keywords. |
:alist | Return a single row as an alist. |
:str-alists | Like :alists, but use the original column names. |
:str-alist | Return a single row as an alist, with strings for names. |
:plists | Return a list of plists which map column names to values,with the names represented as keywords. |
:plist | Return a single row as a plist. |
:column | Return a single column as a list. |
:single | Return a single value. Will raise an error if the query returns more than one field. If the query returns more than one row, it returns the first row. |
:single! | Like :single except that it will throw an error when the number of selected rows is not equal to 1. |
:array-hash | Return an array of hashtables which map column names to hash table keys |
:json-strs | Return a list of strings where each row is a json object expressed as a string |
:json-strs | Return a single string where the row returned is a json object expressed as a string |
:json-array-str | Return a string containing a json array, each element in the array is a selected row expressed as a json object |
(:dao type) | Return a list of DAOs of the given type. The names of the fields returned by the query must match slots in the DAO class the same way as with query-dao. |
(:dao type :single) | Return a single DAO of the given type. |
Some Examples:
Default
The default is :lists
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3))) ((1 2147483645 "text one") (2 0 "text two"))
Single
Returns a single field. Will throw an error if the queries returns more than one field or more than one row
(query (:select 'text :from 'short-data-type-tests :where (:= 'id 3)) :single) "text three"
List
Returns a list containing the selected fields. Will throw an error if the query returns more than one row
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:= 'id 3)) :list) (3 3 "text three")
Lists
This is the default
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :lists) ((1 2147483645 "text one") (2 0 "text two"))
Alist
Returns an alist containing the field name as a keyword and the selected fields. Will throw an error if the query returns more than one row.
(query (:select 'id 'int4 'text :from 'test-data :where (:= 'id 3)) :alist) ((:ID . 3) (:INT4 . 3) (:TEXT . "text three"))
Str-alist
Returns an alist containing the field name as a lower case string and the selected fields. Will throw an error if the query returns more than one row.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:= 'id 3)) :str-alist) (("id" . 3) ("int4" . 3) ("text" . "text three"))
Alists
Returns a list of alists containing the field name as a keyword and the selected fields.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :alists) (((:ID . 1) (:INT4 . 2147483645) (:TEXT . "text one")) ((:ID . 2) (:INT4 . 0) (:TEXT . "text two")))
Str-alists
Returns a list of alists containing the field name as a lower case string and the selected fields.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :str-alists) ((("id" . 1) ("int4" . 2147483645) ("text" . "text one")) (("id" . 2) ("int4" . 0) ("text" . "text two")))
Plist
Returns a plist containing the field name as a keyword and the selected fields. Will throw an error if the query returns more than one row.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:= 'id 3)) :plist) (:ID 3 :INT4 3 :TEXT "text three")
Plists
Returns a list of plists containing the field name as a keyword and the selected fields.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :plists) ((:ID 1 :INT4 2147483645 :TEXT "text one") (:ID 2 :INT4 0 :TEXT "text two"))
Array-hash
Returns a vector of hashtables where each hash table is a returned row from the query with field name as the key expressed as a lower case string.
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :array-hash) #(#<HASH-TABLE :TEST EQUAL :COUNT 3 {100D982B53}> #<HASH-TABLE :TEST EQUAL :COUNT 3 {100D982ED3}>) (alexandria:hash-table-alist (aref (query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :array-hash) 1)) (("text" . "text two") ("int4" . 0) ("id" . 2))
Dao
Returns a list of daos of the type specified
(query (:select '* :from 'country) (:dao country)) (#<COUNTRY {1010464023}> #<COUNTRY {1010465CB3}>) (query (:select '* :from 'country :where (:= 'name "Croatia")) (:dao country)) (#<COUNTRY {1010688943}>)
Column
Returns a list of field values of a single field. Will throw an error if more than one field is selected
(query (:select 'id :from 'short-data-type-tests :where (:< 'id 3)) :column) (1 2) (query (:select 'id :from 'short-data-type-tests :where (:= 'id 3)) :column) (3)
Json-strs
Return a list of strings where the row returned is a json object expressed as a string
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :json-strs) ("{\"id\":1,\"int4\":2147483645,\"text\":\"text one\"}" "{\"id\":2,\"int4\":0,\"text\":\"text two\"}")
This will also handle local-time timestamps and simple-date timestamps, time-of-day and date. E.g. (with a local-time timestamp)
(query (:select 'timestamp-with-time-zone :from 'test-data :where (:< 'id 3)) :json-strs) '("{\"timestampWithTimeZone\":\"{2019-12-30T13:30:54.000000-05:00}\"}" "{\"timestampWithTimeZone\":\"{1919-12-30T13:30:54.000000-05:00}\"}")
The following is an example with a simple-date timestamp.
(query (:select 'timestamp-with-time-zone :from 'test-data :where (:< 'id 3)) :json-strs) '("{\"timestampWithTimeZone\":\"2019-12-30 18:30:54:0\"}" "{\"timestampWithTimeZone\":\"1919-12-30 18:30:54:0\"}")
Json-str
Return a single string where the row returned is a json object expressed as a string
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:= 'id 3)) :json-str) "{\"id\":3,\"int4\":3,\"text\":\"text three\"}"
As with :json-strs, this will also work for either simple-date or local-time timestamps
Json-array-str
Return a string containing a json array, each element in the array is a selected row expressed as a json object
(query (:select 'id 'int4 'text :from 'short-data-type-tests :where (:< 'id 3)) :json-array-str) "[{\"id\":1,\"int4\":2147483645,\"text\":\"text one\"}, {\"id\":2,\"int4\":0,\"text\":\"text two\"}]"
As with :json-strs, this will also work for either simple-date or local-time timestamps
Second value returned
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)
Execute a query, ignore the results. So, in effect, Like a query called with format :none. Returns 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
Wraps a query into 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 since 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 PostgreSQL's CAST SQL-conforming syntax or historical :: syntax, or with S-SQL's :type construct) to help it out.
Note that it will attempt to automatically reconnect if database-connection-error, or admin-shutdown. It will reset prepared statements triggering an invalid-sql-statement-name error. It will overwrite old prepared statements triggering a duplicate-prepared-statement error.
macro defprepared (name query &optional (format :rows))
→ function
This is the macro-style variant of prepare. It is like prepare, but gives the function a name which now becomes a top-level function for the prepared statement. The name should not a string but may be quoted.
macro defprepared-with-names (name (&rest args) (query &rest query-args) &optional (format :rows))
Like defprepared, but allows to specify names of the function arguments in a lambda list as well as arguments supplied to the query.
(defprepared-with-names user-messages (user &key (limit 10)) ("select * from messages where user_id = $1 order by date desc limit $2" (user-id user) limit) :plists)
macro with-transaction ((&optional name isolation-level) &body body)
Execute the given body within a database transaction, committing it when the body exits normally, and aborting otherwise. An optional name and/or isolation-level can be given to the transaction. The name can be used to force a commit or abort before the body unwinds. The isolation-level will set the isolation-level used by the transaction.
You can specify the following isolation levels in postmodern transactions:
- :read-committed-rw (read committed with read and write)
- :read-committed-ro (read committed with read only)
- :repeatable-read-rw (repeatable read with read and write)
- :repeatable-read-ro (repeatable read with read only)
- :serializable (serializable with reand and write)
Sample usage where "george" is just the name given to the transaction (not quoted or a string) and … simply indicates other statements would be expected here:
(with-transaction () (execute (:insert-into 'test-data :set 'value 77)) ...) (with-transaction (george) (execute (:insert-into 'test-data :set 'value 22)) ...) (with-transaction (george :read-committed-rw) (execute (:insert-into 'test-data :set 'value 33)) (query (:select '* :from 'test-data)) ...) (with-transaction (:serializable) (execute (:insert-into 'test-data :set 'value 44)) ...)
Further discussion of transactions and isolation levels can found at isolation-notes.html in the doc directory.
function abort-transaction (transaction)
Roll back the given transaction, but the transaction block is still active. Thus calling abort-transaction in the middle of a transaction does not end the transaction. Any subsequent statements will still be executed. Per the Postgresql documentation: ABORT rolls back the current transaction and causes all the updates made by the transaction to be discarded. This command is identical in behavior to the standard SQL command ROLLBACK, and is present only for historical reasons..
function rollback-transaction (transaction)
Roll back the given transaction, but the transaction block is still active. Thus calling abort-transaction in the middle of a transaction does not end the transaction. Any subsequent statements will still be executed. Per the Postgresql documentation: this rolls back the current transaction and causes all the updates made by the transaction to be discarded.
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. The body is executed and, at the end of body, the savepoint is released, unless a condition is thrown, in which case it is rolled back. Execute the body within a savepoint, releasing savepoint when the body exits normally, and rolling back otherwise. NAME is both the variable that can be used to release or rolled back before the body unwinds, and the SQL name of the savepoint.
The following example demonstrates with-savepoint, rollback-savepoint and release-savepoint.
(execute (:create-table test-data ((value :type integer)))) (defun test12 (x &optional (y nil)) (with-logical-transaction (lt1 :read-committed-rw) (execute (:insert-into 'test-data :set 'value 0)) (with-savepoint sp1 (execute (:insert-into 'test-data :set 'value 1)) (format t "1-1. ~a Savepoint-name ~a~%" (query "select * from test_data") (pomo::savepoint-name sp1)) (if (< x 0) (rollback-savepoint sp1) (release-savepoint sp1)) (format t "1-2. ~a~%" (query "select * from test_data"))) (with-savepoint sp2 (execute (:insert-into 'test-data :set 'value 2)) (format t "2-1. ~a Savepoint-name ~a~%" (query "select * from test_data") (pomo::savepoint-name sp2)) (with-savepoint sp3 (execute (:insert-into 'test-data :set 'value 3)) (format t "3-1. ~a Savepoint-name ~a~%" (query "select * from test_data") (pomo::savepoint-name sp3)) (if (> x 0) (rollback-savepoint sp3) (release-savepoint sp3)) (format t "3-2. ~a~%" (query "select * from test_data")) (when y (rollback-savepoint sp2)) (format t "3-3. ~a~%" (query "select * from test_data"))) (if (= x 0) (rollback-savepoint sp2) (release-savepoint sp2)) (format t "2-2. ~a~%" (query "select * from test_data"))) (format t "4. ~a~%" (query "select * from test_data")) (when (string= y "abrt") (abort-transaction lt1)) (format t "5. ~a~%" (query "select * from test_data"))))
method commit-hooks (transaction-or-savepoint), setf (commit-hooks transaction-or-savepoint)
An accessor for the transaction or savepoint's list of commit hooks, each of which should be a function with no required arguments. These functions will be executed when a transaction is committed or a savepoint released.
function abort-hooks (transaction-or-savepoint), setf (abort-hooks transaction-or-savepoint)
An accessor for the transaction or savepoint's list of abort hooks, each of which should be a function with no required arguments. These functions will be executed when a transaction is aborted or a savepoint rolled back (whether via a non-local transfer of control or explicitly by either abort-transaction or rollback-savepoint).
variable isolation-level
The transaction isolation level currently in use. Defaults to :read-committed-rw
You can specify the following isolation levels in postmodern transactions:
- :read-committed-rw (read committed with read and write)
- :read-committed-ro (read committed with read only)
- :repeatable-read-rw (repeatable read with read and write)
- :repeatable-read-ro (repeatable read with read only)
- :serializable (serializable with reand and write)
macro with-logical-transaction ((&optional name isolation-level) &body body)
Executes body within a with-transaction form if no transaction is currently in progress, otherwise simulates a nested transaction by executing it within a with-savepoint form. The transaction or savepoint is bound to name if one is supplied. The isolation-level will set the isolation-level used by the transaction.
You can specify the following isolation levels in postmodern transactions:
- :read-committed-rw (read committed with read and write)
- :read-committed-ro (read committed with read only)
- :repeatable-read-rw (repeatable read with read and write)
- :repeatable-read-ro (repeatable read with read only)
- :serializable (serializable with reand and write)
For more information see isolation-notes
Sample usage where "george" is just the name given to the transaction (not quoted or a string) and … simply indicates other statements would be expected here:
(with-logical-transaction () (execute (:insert-into 'test-data :set 'value 77)) ...) (with-logical-transaction (george) (execute (:insert-into 'test-data :set 'value 22)) ...) (with-logical-transaction (george :read-committed-rw) (execute (:insert-into 'test-data :set 'value 33)) ...) (with-logical-transaction (:serializable) (execute (:insert-into 'test-data :set 'value 44)) ...)
function abort-logical-transaction (transaction-or-savepoint)
Roll back the given logical transaction, regardless of whether it is an actual transaction or a savepoint.
function commit-logical-transaction (transaction-or-savepoint)
Commit the given logical transaction, regardless of whether it is an actual transaction or a savepoint.
variable current-logical-transaction
This is bound to the current transaction-handle or savepoint-handle instance representing the innermost open logical transaction.
Helper functions for Prepared Statements
defparameter allow-overwriting-prepared-statements
When set to t, ensured-prepared will overwrite prepared statements having the same name if the query statement itself in the postmodern meta connection is different than the query statement provided to ensure-prepared.
function prepared-statement-exists-p (name)
→ boolean This returns t if the prepared statement exists in the current postgresql session, otherwise nil.
function list-prepared-statements (&optional (names-only nil))
→ list
This is syntactic sugar. It runs a query that lists the prepared statements in the session in which the function is run. If the names-only parameter is set to t, it will only return a list of the names of the prepared statements.
function drop-prepared-statement (statement-name &key (location :both) (database database))
The statement name can be a string or quoted symbol.
Prepared statements are stored both in the meta slot in the postmodern connection and in postgresql session information. In the case of prepared statements generated with defprepared, there is also a lisp function with the same name.
If you know the prepared statement name, you can delete the prepared statement from both locations (the default behavior), just from postmodern by passing :postmodern to the location key parameter or just from postgresql by passing :postgresql to the location key parameter.
If you pass the name 'All' as the statement name, it will delete all prepared statements.
The default behavior is to also remove any lisp function of the same name. This behavior is controlled by the remove-function key parameter.
function list-postmodern-prepared-statements (&optional (names-only nil))
→ list
List the prepared statements that postmodern has put in the meta slot in the connection. It will return a list of alists of form: ((:NAME . \"SNY24\") (:STATEMENT . \"(SELECT name, salary FROM employee WHERE (city = $1))\") (:PREPARE-TIME . #<TIMESTAMP 25-11-2018T15:36:43,385>) (:PARAMETER-TYPES . \"{text}\") (:FROM-SQL)
If the names-only parameter is set to t, it will only return a list of the names of the prepared statements.
function find-postgresql-prepared-statement (name)
→ string
Returns the specified named prepared statement (if any) that postgresql has for this session and placed in the meta slot in the connection.
function find-postmodern-prepared-statement (name)
→ string
Returns the specified named prepared statement (if any) that postmodern has put in the meta slot in the connection. Note that this is the statement itself, not the name.
function reset-prepared-statement (condition)
→ restart
If you have received an invalid-prepared-statement error but the prepared statement is still in the meta slot in the postmodern connection, this will try to regenerate the prepared statement at the database connection level and restart the connection.
function get-pid-from-postmodern ()
→ integer
Get the process id used by postgresql for this connection, but get it from the postmodern connection parameters.
Database Management
function create-database (database-name &key (encoding "UTF8") (connection-limit -1) owner limit-public-access comment collation template)
Creates a basic database. Besides the obvious database-name parameter, you can also use key parameters to set encoding (defaults to UTF8), owner, connection-limit (defaults to no limit)). If limit-public-access is set to t, then only superuser roles or roles with explicit access to this database will be able to access it. See Roles.
If collation is set, the assumption is that template0 needs to be used as the base of the database rather than template1 which may contain encoding specific or locale specific data.
(create-database 'testdb :limit-public-access t :comment "This database is for testing silly theories")
function drop-database (database)
Drop the specified database. The database parameter can be a string or a symbol. Note: Only the owner of a database (or superuser) can drop a database and there cannot be any current connections to the database. [[#database-information][See Database information below for information specific functions]
Database access objects
Postmodern contains a simple system for defining CLOS classes that represent rows in the database. This is not intended 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
You can work directly with the database or you can use a simple database-access-class (aka dao) which would cover all the fields in a row.
Postmodern allows you to have a relatively simple but straight forward matching of clos classes to a database table. 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. You can use the :col-name initarg (whose unevaluated value will be passed to to-sql-name) to specify the slot's column's name.
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 if they have not been specified in a single column. If more than one key is provided, this creates a multi-column primary key and all keys must be specified when using operations such as update-dao and get-dao. When no primary keys are defined, operations such as update-dao and get-dao will not work.
IMPORTANT: Class finalization for a dao class instance are wrapped with a thread lock. However, any time you are using threads and a class that inherits from other classes, you should ensure that classes are finalized before you start generating threads that create new instances of that class.
The (or db-null integer) form is used to indicate a column can have NULL values otherwise the column will be treated as NOT NULL.
Simple example:
(defclass users () ((name :col-type string :initarg :name :accessor name) (creditcard :col-type (or db-null integer) :initarg :card :col-default :null) (score :col-type bigint :col-default 0 :accessor score)) (:metaclass dao-class) (:keys name))
In this case the name of the users will be treated as the primary key and the database table is assumed to be users. (It might be worth noting that "user" is a reserved word for Postgresql and using reserved words, while possible using quotes, is generally not worth the additional trouble they cause.)
The name and score slots cannot be null, but the creditcard slot can be null and actually defaults to null. The :col-default :null specification ensures that the default in the database for this field is null, but it does not bound the slot to a default form. Thus, making an instance of the class without initializing this slot will leave it in an unbound state.
An example of a class where the keys are set as multiple column keys is here:
(defclass points () ((x :col-type integer :initarg :x :reader point-x) (y :col-type integer :initarg :y :reader point-y) (value :col-type integer :initarg :value :accessor value)) (:metaclass dao-class) (:keys x y))
In this case, retrieving a points record would look like the following where 12 and 34 would be the values you are looking to find in the x column and y column respectively.:
(get-dao 'points 12 34)
Now look at a slightly more complex example.
(defclass country () ((id :col-type integer :col-identity t :accessor id) (name :col-type string :col-unique t :check (:<> 'name "") :initarg :name :reader country-name) (inhabitants :col-type integer :initarg :inhabitants :accessor country-inhabitants) (sovereign :col-type (or db-null string) :initarg :sovereign :accessor country-sovereign) (region-id :col-type integer :col-references ((regions id)) :initarg :region-id :accessor region-id)) (:documentation "Dao class for a countries record.") (:metaclass dao-class) (:table-name countries))
In this example we have an id column which is specified to be an identity column. Postgresql will automatically generate a sequence of of integers and this will be the primary key.
We have a name column which is specified as unique and is not null.
We have a region-id column which references the id column in the regions table. This is a foreign key constraint and Postgresql will not accept inserting a country into the database unless there is an existing region table with an id that matches this number. Postgresql will also not allow deleting a region if there are countries that reference that region's id. If we wanted Postgresql to delete countries when regions are deleted, that column would be specified as:
(region-id :col-type integer :col-references ((regions id) :cascade) :initarg :region-id :accessor region-id)
Now you can see why the double parens.
We also specified that the table name is not "country" but "countries". (Some style guides recommend that table names be plural and references to rows be singular.)
When inheriting from DAO classes, a subclass' set of columns also contains all the columns of 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, the :fetch-defaults keyword argument can be passed, which, when T, will cause a query to fetch the default values for all slots that refers to columns with defaults and were not bound through initargs. In some cases, such as serial and identity columns, which have an implicit default, this will not work. You can work around this by creating your own sequence, e.g. "my_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 known use for this to date is for creating the table with (oids=true), and specify a slot like this:
(oid :col-type integer :ghost t :accessor get-oid)
Out of Sync Dao Objects
What Happens when dao classes are out of sync with the database table? Let's establish our baseline
(defclass test-data () ((id :col-type serial :initarg :id :accessor test-id) (a :col-type (or (varchar 100) db-null) :initarg :a :accessor test-a) (b :col-type boolean :col-default nil :initarg :b :accessor test-b) (c :col-type integer :col-default 0 :initarg :c :accessor test-c) (d :col-type numeric :col-default 0.0 :initarg :d :accessor test-d)) (:metaclass dao-class) (:table-name dao-test) (:keys id)) #<DAO-CLASS S-SQL-TESTS::TEST-DATA> (execute (dao-table-definition 'test-data))
Now we define a class that uses the same table, but does not have all the columns.
(defclass test-data-short () ((id :col-type serial :initarg :id :accessor test-id) (a :col-type (or (varchar 100) db-null) :initarg :a :accessor test-a)) (:metaclass dao-class) (:table-name dao-test) (:keys id))
We create an instance of the shortened class and try to save it, then check the results.
(let ((dao (make-instance 'test-data-short :a "first short"))) (save-dao dao)) (query (:select '* :from 'dao-test) :alists) (((:ID . 1) (:A . "first short") (:B) (:C . 0) (:D . 0)))
It was a successful save, and we see that the missing columns took their default values.
Now we define a shortened class, but the a slot is now numeric or null instead of a string and try to save it and check it.
(defclass test-data-short-wrong-1 () ((id :col-type serial :initarg :id :accessor test-id) (a :col-type (or numeric db-null) :initarg :a :accessor test-a)) (:metaclass dao-class) (:table-name dao-test) (:keys id)) (let ((dao (make-instance 'test-data-short-wrong-1 :a 12.75))) (save-dao dao)) (query (:select '* :from 'dao-test) :alists) (((:ID . 1) (:A . "first short") (:B) (:C . 0) (:D . 0)) ((:ID . 2) (:A . "12.75") (:B) (:C . 0) (:D . 0))
Notice that the 12.75 has been converted into a string when it was saved. Postgresql did this automatically. Anything going into a text or varchar column will be converted to a string.
Now we will go the other way and define a dao with the right number of columns, but col d is a string when the database expects a numeric and check that.
(defclass test-data-d-string () ((id :col-type serial :initarg :id :accessor test-id) (a :col-type (or (varchar 100) db-null) :initarg :a :accessor test-a) (b :col-type boolean :col-default nil :initarg :b :accessor test-b) (c :col-type integer :col-default 0 :initarg :c :accessor test-c) (d :col-type text :col-default "" :initarg :d :accessor test-d)) (:metaclass dao-class) (:table-name dao-test) (:keys id)) (let ((dao (make-instance 'test-data-d-string :a "D string" :b nil :c 14 :d "Trying string"))) (save-dao dao)) Database error 22P02: invalid input syntax for type numeric: "Trying string" QUERY: INSERT INTO dao_test (d, c, b, a) VALUES (E'Trying string', 14, false, E'D string') RETURNING id [Condition of type DATA-EXCEPTION]
Ok. That threw a data exception. What happens if we try to force a numeric into an integer column?
(let ((dao (make-instance 'test-data-d-string :a "D string" :b nil :c 14.37 :d 18.78))) (save-dao dao)) Database error 22P02: invalid input syntax for type integer: "14.37" [Condition of type CL-POSTGRES-ERROR:DATA-EXCEPTION]
Ok. Postgresql is enforcing the types.
(let ((dao (make-instance 'test-data-d-string :a "D string" :b nil :c 14 :d 18.78))) (save-dao dao)) (query (:select '* :from 'dao-test) :alists) (((:ID . 1) (:A . "first short") (:B) (:C . 0) (:D . 0)) ((:ID . 2) (:A . "12.75") (:B) (:C . 0) (:D . 0)) ((:ID . 3) (:A . "D string") (:B) (:C . 14) (:D . 939/50)))
Notice that postmodern returned a ratio 939/50 for the numeric 18.78.
We have looked at saving daos. Now look at returning a dao from the database where the dao definition is different than the table definition. First checking to see if we can get a correct dao back.
(get-dao 'test-data 3) #<TEST-DATA {100C82AA33}>
Ok. That worked as expected.
Second using a shortened dao that is correct in type of columns, but incorrect n the number of columns compared to the database table.
(get-dao 'test-data-short 3)
No slot named b in class TEST-DATA-SHORT. DAO out of sync with table, or
incorrect query used.
[Condition of type SIMPLE-ERROR]
Restarts:
0: [RETRY] Retry SLIME REPL evaluation request.
1: [*ABORT] Return to SLIME's top level.
2: [ABORT] abort thread (#<THREAD "new-repl-thread" RUNNING {100C205083}>)
Not only did it throw an exception, but I needed to actually use an interrupt from the repl to get back in operation. And then use (reconnect database). Very Bad result.
THIS ERROR IS CONTROLLABLE BY THE VARIABLE IGNORE-UNKNOWN-COLUMNS
Now if we setf the default global variable ignore-unknown-columns to t
(setf *ignore-unknown-columns* t) (get-dao 'test-data-short 3) #<TEST-DATA-SHORT {10054DFED3}> (describe (get-dao 'test-data-short 3)) #<TEST-DATA-SHORT {100B249783}> [standard-object] Slots with :INSTANCE allocation: ID = 3 A = "D string"
We now have a dao that has fewer slots than the database table it pulled from. Just to validate that:
(query (:select '* :from 'dao-test :where (:= 'id 3))) ((3 "D string" NIL 14 939/50))
Just to be thorough, let's use a dao that has more slots than the database table.
(defclass test-data-long () ((id :col-type serial :initarg :id :accessor test-id) (a :col-type (or (varchar 100) db-null) :initarg :a :accessor test-a) (b :col-type boolean :col-default nil :initarg :b :accessor test-b) (c :col-type integer :col-default 0 :initarg :c :accessor test-c) (d :col-type numeric :col-default 0.0 :initarg :d :accessor test-d) (e :col-type text :col-default "sell by date" :initarg :e :accessor test-e)) (:metaclass dao-class) (:table-name dao-test) (:keys id))
Now if we make an instance of this dao and try to save it in the dao-class table:
(let ((dao (make-instance 'test-data-long :a "first short" :d 37.3))) (save-dao dao)) Database error 42703: column "e" does not exist QUERY: INSERT INTO dao_test (d, a) VALUES ($1, $2) RETURNING e, c, b, id [Condition of type CL-POSTGRES-ERROR:UNDEFINED-COLUMN]
Postgresql rejected the attempted insert with an undefined column error.
method dao-keys (class)
→ list
Returns list of slot names that are the primary key of DAO class. This is likely interesting if you have primary keys which are composed of more than one slot. Pay careful attention to situations where the primary key not only has more than one column, but they are actually in a different order than they are in the database table itself. You can check this with the internal find-primary-key-info function. Obviously the table needs to have been defined. The class must be quoted.
(pomo:find-primary-key-info 'country1) (("name" "text") ("id" "integer"))
method dao-keys (dao)
→ list
Returns list of values that are the primary key of dao. Explicit keys takes priority over col-identity which takes priority over col-primary-key.
This is likely interesting if you have primary keys which are composed of more than one slot. Pay careful attention to situations where the primary key not only has more than one column, but they are actually in a different order than they are in the database table itself. Obviously the table needs to have been defined. You can provide a quoted class-name or an instance of a dao.
method find-primary-key-column
→ symbol
Loops through a class's column definitions and returns the first column name that has bound either col-identity or col-primary-key.
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 make-dao (type &rest args &key &allow-other-keys)
→ dao
Combines make-instance with insert-dao. Make the instance of the given class and insert it into the database, returning the created dao.
method fetch-defaults (dao)
→ dao if there were unbound slots with default values, otherwise nil
Used to fetch the default values of an object on creation. An example would be creating a dao object with unbounded slots. Fetch-defaults could then be used to fetch the default values from the database and bind the unbound slots which have default values. E.g.
(let ((dao (make-instance 'test-data :a 23))) (pomo:fetch-defaults dao))
method find-primary-key-column (class)
→ symbol
Loops through a class's column definitions and returns the first column name that has bound either col-identity or col-primary-key. Returns a symbol.
macro define-dao-finalization (((dao-name class) &rest keyword-args) &body body)
Create an :around-method for make-dao. The body is executed in a lexical environment where dao-name is bound to a freshly created and inserted DAO. The representation of the DAO in the database is then updated to reflect changes that body might have introduced. Useful for processing values of slots with the type serial, which are unknown before insert-dao.
method get-dao (type &rest keys)
→ dao
Get the single 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.
(get-dao 'country "The Netherlands")
#<COUNTRY {1010F0DCF3}>
From an sql perspective, the standard call to get-dao translates as:
select * from table
NOTE: if you have added fields to the database table without updating the class definition, get-dao and select-dao will throw errors. This may cause your application to appear to hang unless you have the necessary condition handling in your code. Usually this will only happen during development, so throwing an error is not a bad idea. If you want to ignore the errors, set ignore-unknown-columns to t.
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 the test argument.)
(select-dao 'country) (#<COUNTRY {101088F6F3}> #<COUNTRY {101088FAA3}>) 2 (select-dao 'country (:> 'inhabitants 50000000)) NIL 0 (select-dao 'country (:> 'inhabitants 5000000)) (#<COUNTRY {10108AD293}>) 1 (select-dao 'country (:> 'inhabitants 5000)) (#<COUNTRY {10108CA773}> #<COUNTRY {10108CAB23}>) 2 (select-dao 'country (:> 'inhabitants 5000) 'name) ;sorted by name (#<COUNTRY {10108EF423}> #<COUNTRY {10108EF643}>) (mapcar 'country-name (select-dao 'country (:> 'inhabitants 5000) 'name)) ("Croatia" "The Netherlands") (mapcar 'country-name (select-dao 'country (:> 'inhabitants 5000))) ("The Netherlands" "Croatia")
If for some reason, you wanted the list in reverse alphabetical order, then:
(select-dao 'country (:> 'id 0) (:desc 'name))
macro do-select-dao (((type type-var) &optional (test t) &rest sort) &body body)
Like select-dao, but iterates over the results rather than returning them. For each matching DAO, body is evaluated with type-var bound to the DAO instance.
Example:
(do-select-dao (('user user) (:> 'score 10000) 'name) (pushnew user high-scorers))
macro query-dao (type query &rest args)
→ 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. If the query contains placeholders ($1, $2, etc) their values can be given as extra arguments. The names of the fields returned by the query must either match slots in the DAO class, or be bound through with-column-writers.
function do-query-dao (((type type-var) query &rest args) &body body)
→ list
Like query-dao, but iterates over the results rather than returning them. For each matching DAO, body is evaluated with type-var bound to the instance.
Example:
(do-query-dao (('user user) (:order-by (:select '* :from 'user :where (:> 'score 10000)) 'name)) (pushnew user high-scorers))
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 non-NIL will cause it to simply ignore the unknown column.
method insert-dao (dao)
→ dao
Insert the given dao into the database. Column slots of the object which are unbound implies the database defaults. Hence, if these columns has no defaults defined in the database, the the insertion of the dao will be failed. (This feature only works on PostgreSQL 8.2 and up.)
method update-dao (dao)
→ dao
Update the representation of the given dao in the database to 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 the dao has unbound slots, those slots will be updated and bound by default data triggered by the database. If this raises a unique key violation error, it tries to update it by using update-dao instead. In this case, if the dao has unbound slots, updating will fail with an unbound slots error.
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 aborted. Use save-dao/transaction instead in such a situation.
See also: upsert-dao.
function save-dao/transaction (dao)
→ boolean
The transaction safe version of save-dao. Tries to insert the given dao using insert-dao. If this raises a unique key violation error, it tries to update it by using update-dao instead. If the dao has unbound slots, updating will fail with an unbound slots error. If the dao has unbound slots, those slots will be updated and bound by default data triggered by the database.
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.
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.
See also: upsert-dao.
method upsert-dao (dao)
→ dao
Like save-dao or save-dao/transaction but using a different method that doesn't involve a database exception. This is safe to use both in and outside a transaction, though it's advisable to always do it in a transaction to prevent a race condition. The way it works is:
If the object contains unbound slots, we call insert-dao directly, thus the behavior is like save-dao.
Otherwise we try to update a record with the same primary key. If the PostgreSQL returns a non-zero number of rows updated it treated as the record is already exists in the database, and we stop here.
If the PostgreSQL returns a zero number of rows updated, it treated as the record does not exist and we call insert-dao.
The race condition might occur at step 3 if there's no transaction: if UPDATE returns zero number of rows updated and another thread inserts the record at that moment, the insertion implied by step 3 will fail.
Note, that triggers and rules may affect the number of inserted or updated rows returned by PostgreSQL, so zero or non-zero number of affected rows may not actually indicate the existence of record in the database.
This method returns two values: the DAO object and a boolean (T if the object was inserted, NIL if it was updated).
IMPORTANT: This is not the same as insert on conflict (sometimes called an upsert) in Postgresq. An upsert in Postgresql terms is an insert with a fallback of updating the row if the insert key conflicts with an already existing row. An upsert-dao in Postmodern terms is the reverse. First you try updating an existing object. If there is no existing object to oupdate, then you insert a new object.
function dao-table-name (class)
→ string
Get the name of the table associated with the given DAO class (or symbol naming such a class).
function dao-table-definition (class)
→ 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, in which case look to s-sql's create-table function.
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).
An example of using 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 immediately 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 source code, to do things like automatically deploying 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's name 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. The variables table-name and table-symbol are bound to the relevant values in the body. Note that the evaluation of the definition is ordered, so you'll generally want to create your table first and then define indices on it.
function !dao-def ()
Should only be used inside a deftable form. Define this table using the corresponding DAO class' slots. Adds the result of calling dao-table-definition on table-symbol to the definition.
function !index (&rest columns), !unique-index (&rest columns)
Used inside a deftable form. Define an index on the table being defined. The columns can be given as symbols or strings.
function !foreign (target fields &rest target-fields/on-delete/on-update/deferrable/initially-deferred)
Used insde a deftable form. Add a foreign key to the table being defined. target-table is the referenced table. columns is a list of column names or single name in this table, and, if the columns have different names in the referenced table, target-columns must be another list of column names or single column name of the target-table, or :primary-key to denote the column(s) of the target-table's primary key as referenced column(s).
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. In addition, the deferrable and initially-deferred arguments can be used to indicate whether constraint checking can be deferred until the current transaction completed, and whether this should be done by default. Note that none of these are 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.
function !unique (target-fields &key deferrable initially-deferred)
Constrains one or more columns to only contain unique (combinations of) values, with deferrable and initially-deferred defined as in !foreign
function create-table (symbol)
Takes the name of a dao-class and creates the table identified by symbol by executing all forms in its definition as found in the tables list.
function create-package-tables (package)
Creates all tables identified by symbols interned in the given package.
variables table-name, table-symbol
Used inside deftable to find the name of the table being defined.
Used inside deftable to find the symbol naming the table being defined.
function drop-table (table-name &key if-exists cascade)
If a table exists, drop a table. Available additional key parameters are :if-exists and :cascade.
Introduction to Multi-table dao class objects
Postmodern's dao-class objects are not required to be tied down to a specific table. They can be used simply as classes to hold data for whatever purpose your application may use.
For this introduction, we will use two sets of tables: (1) country-d and region-d and (2) country-n and region-n. In each case the country table will have a foreign key tied to a region.
A foreign key is a "constraint" referencing a primary key in another table. The table containing the foreign key is the referencing or child table and the table referenced by the foreign key is the referenced or parent table. The foreign key enforces a requirement that the child table column refering to another table must refer to a row that exists in the other table. In other words, you cannot create a row in table country-d that references a region-d name "Transylvania" if the region-d name "Transylvania" does not yet exist in the region-d table. At the same time, you could not later delete the region-d row with "Transylvania" if the country-d row referencing it still exists.
Do you remember the slightly more complicated version of country from earlier on the page?
(defclass country () ((id :col-type integer :col-identity t :accessor id) (name :col-type string :col-unique t :check (:<> 'name "") :initarg :name :reader country-name) (inhabitants :col-type integer :initarg :inhabitants :accessor country-inhabitants) (sovereign :col-type (or db-null string) :initarg :sovereign :accessor country-sovereign) (region-id :col-type integer :col-references ((regions id)) :initarg :region-id :accessor region-id)) (:documentation "Dao class for a countries record.") (:metaclass dao-class) (:table-name countries))
That one specified a foreign key reference in the region-id column, so we cannot insert the data from a country dao unless there is already a region table with an id column equal to the region-id in the country dao.
Lets look at two slightly different ways of handling countries and regions.
In our first set of tables, country-d will have a region column that references the name column in a region-d table (so the name column in region-d must be the primary key for region-d).
This looks relatively straight forward and it is in this simple case. Things start getting more complicated if you start having to reference a table where there are many items with the same name. An example would be tracking library books. There may be multiple copies of a book title, but you need to know which book was checked out to which library patron. In these types of situations, the primary key cannot be the name of the region, it needs to reference some particular id.
In our second set of tables, country-n will have a region-id column that references an id column in a region-d table (so the id column in region-d must be the primary key for region-d).
Simple Version
Lets start by declaring our classes and we will use the deftable make to create a definition for our tables that gets stored in the tables special variable. We can then use the (create-table 'class-name) function to create the table in the database.
Just to be slightly different, we are going to declare the classes without the :col-reference and :col-unique modifiers and put those into the (deftable) macro call. We will set the id as a serial in the -d version because we want to use name as the primary key and seting id as an identity would cause it to be the primary key.
(defclass region-d () ((id :col-type serial :initarg :id :reader region-id) (name :col-type string :initarg :name :accessor region-name)) (:metaclass pomo:dao-class) (:keys name)) (deftable region-d (!dao-def) (!unique 'name)) (create-table 'region-d) (defclass country-d () ((id :col-type serial :initarg :id :reader country-id) (name :col-type string :initarg :name :reader country-name) (region-name :col-type string :initarg :region-name :accessor region-name)) (:metaclass pomo:dao-class) (:keys name)) (deftable country-d (!dao-def) (!unique 'name) (!foreign 'region-d 'region-name 'name)) (create-table 'country-d)
The new function !foreign insde the deftable adds a foreign key which requires that a region with that id already exist before you can insert a country. By the way, because of the foreign key constraint, postgresql will require that the region-d table be created before the country-d table.
Look at tables for a moment:
*tables* ((REGION-D . #<FUNCTION (LAMBDA ()) {534D126B}>) (COUNTRY-D . #<FUNCTION (LAMBDA ()) {52A1484B}>))
The region-d lambda looks like this:
(LAMBDA () (LET ((*TABLE-NAME* "region_d") (*TABLE-SYMBOL* 'REGION-D)) (DOLIST (STAT (LIST (!DAO-DEF) (!UNIQUE 'NAME))) (EXECUTE STAT))))
The country-d lambda looks like this:
(LAMBDA () (LET ((*TABLE-NAME* "country_d") (*TABLE-SYMBOL* 'COUNTRY-D)) (DOLIST (STAT (LIST (!DAO-DEF) (!UNIQUE 'NAME) (!FOREIGN 'REGION-D 'REGION-NAME 'NAME))) (EXECUTE STAT))))
Less Simple Version
In the -n version, we are going to use the id columns as the primary key. We will not need to tell deftable t
(defclass region-n () ((id :col-type integer :col-identity t :initarg :id :reader region-id) (name :col-type string :initarg :name :accessor region-name)) (:metaclass pomo:dao-class)) (deftable region-n (!dao-def) (!unique 'name)) (create-table 'region-n) (defclass country-n () ((id :col-type integer :col-identity t :initarg :id :reader country-id) (name :col-type string :initarg :name :reader country-name) (region-id :col-type integer :initarg :region-id :accessor region-id)) (:metaclass dao-class)) (deftable country-n (!dao-def) (!unique 'name) (!foreign 'region-n 'region-id 'id)) (create-table 'country-n)
How do you find the region-id? While we set the primary key as name for both country and region in the simple version, it will be a little more work in the less simple version. Lets start by inserting a couple of regions and we will stick with the dao method for the moment:
(insert-dao (make-instance 'region-d :name "Western Europe")) (insert-dao (make-instance 'region-n :name "Western Europe")) (insert-dao (make-instance 'region-d :name "Southern Europe")) (insert-dao (make-instance 'region-n :name "Southern Europe"))
Now we can add a few countries to country-d:
(insert-dao (make-instance 'country-d :name "The Netherlands" :region-name "Western Europe")) (insert-dao (make-instance 'country-d :name "Croatia" :region-name "Southern Europe"))
Now we can add a few countries to country-n, remembering that for this version, name is not the primary key so how to get the region dao with the name "Western Europe"? For region-d it is easy because the name is the primary key. So
(get-dao 'region-d "Western Europe")
#<REGION-D {100A322D43}>
For region-n it is a little more complicated because the primary key is the id column, not the name column. So there are a couple of ways to do it. First is select-dao which will return a list of daos meeting a test criteria, in a sorted order if that third parameter is also provided. Eg.
(select-dao 'region-n (:= 'id 1)) (#<REGION-N {100AAC6E13}>) (select-dao 'region-n (:= 'name "Western Europe")) (#<REGION-N {100A813CF3}>) (select-dao 'region-n t 'name) (#<REGION-N {100AC90FA3}> #<REGION-N {100AC911B3}>)
Another method with is query-dao, which takes a row and inserts it into a dao. That gets us a list of daos meeting the select criteria.
(query-dao 'region-n "select * from region_n where name = 'Western Europe'")
(#<REGION {1009E75E63}>)
or, using s-sql expression
(query-dao 'region-n (:select '* :from 'region-n :where (:= 'name "Western Europe"))) (#<REGION-D {100A50DA13}>)
Here are two different ways of generating the region-id so we can insert a new dao into country-n:
(insert-dao (make-instance 'country-n :name "The Netherlands" :region-id (region-id (first (select-dao 'region-n (:= 'name "Western Europe")))))) #<COUNTRY-N {1002AD79B3}> (insert-dao (make-instance 'country-n :name "Croatia" :region-id (query (:select 'id :from 'region-n :where (:= 'name "Southern Europe")) :single))) #<COUNTRY-N {1002ADE2B3}>
But the returned row need not be the result from a single table. Suppose we create a third table that has population by year and inserted a couple of rows. This time we will do it with s-sql.
(query (:create-table 'country-population ((id :type bigserial) (country-id :type integer) (year :type integer) (population :type integer)))) (let ((country-id (query (:select 'id :from 'country-d :where (:= 'name "The Netherlands")) :single))) (query (:insert-rows-into 'country-population :columns 'country-id 'year 'population :values `((,country-id 2014 16830000) (,country-id 2015 16900000) (,country-id 2016 16980000) (,country-id 2017 17080000))))) (let ((country-id (query (:select 'id :from 'country-d :where (:= 'name "Croatia")) :single))) (query (:insert-rows-into 'country-population :columns 'country-id 'year 'population :values `((,country-id 2014 4255518) (,country-id 2015 4232873) (,country-id 2016 4208611) (,country-id 2017 4182846)))))
Now we create a class that pulls from all three tables (country, region and country-population).
(defclass country-with-population () ((country-name :col-type string :initarg :country-name :reader country-name) (region-name :col-type string :initarg :region-name :accessor region-name) (year :col-type integer :initarg :year :reader year) (population :col-type integer :initarg :population :reader population)) (:metaclass dao-class) (:keys country-name))
Can we use query-dao to get a list of country-with-population daos with the most recent population data? The answer is yes. That would give us a class that maybe our application can use without having to worry about constantly going back to the database to look for the region's name or whatever.
Of course you still need to get the data into the class instances. You could write the following one time as a function to generate your list of countries with the most recent population data. Note that you need to rename the columns to the appropriate initarg name (e.g. 'country-n.name is selected as 'country-name). You do not need to worry about the order of the selected rows. So long as the selections are renamed properly, the slots will be populated properly.
In the data that we have in the system, we happen to know that the years available are the same for every country. In that case, we just want the information for the maximum year. One way to do that would be:
(query-dao 'country-with-population (:select (:as 'country-n.name 'country-name) 'year (:as 'region-n.name 'region-name) 'population :from 'country-n :inner-join 'region-n :on (:= 'country-n.region-id 'region-n.id) :inner-join 'country-population :on (:= 'country-n.id 'country-population.country-id) :where (:= 'year (:select (:max 'year) :from 'country-population))))
But what happens if the data is not the same for both countries? Lets drop the 2017 population data row for Croatia and make sure it still returns the most current year that we have for both countries.
(query (:delete-from 'country-population :where (:and (:= 'country-id 2) (:= 'year 2017))))
If we run the same query from above, we only get an instance for The Netherlands because that was the only data available for the maximum year (2017). We need to approach the data slightly differently.Because this is postmodern and we only care about the Postgresql database, we can use its DISTINCT ON extension to the SQL standard.
See https://www.postgresql.org/docs/current/sql-select.html#SQL-DISTINCT for more documentation.
The following query will pull the most recent year for both countries. How did that happen? We limited the select clause to distinct country names so we would only pull one of each country, then ordered the result by country-name, but most importantly by year descending.
(query-dao 'country-with-population (:order-by (:select (:as 'country-n.name 'country-name) 'year (:as 'region-n.name 'region-name) 'population :distinct-on 'country-n.name :from 'country-n :inner-join 'region-n :on (:= 'country-n.region-id 'region-n.id) :inner-join 'country-population :on (:= 'country-n.id 'country-population.country-id)) 'country-name (:desc 'year))) (#<COUNTRY-WITH-POPULATION {1009AFAEC3}> #<COUNTRY-WITH-POPULATION {1009AFC963}>)
At this point you could write a function that gets a country-with-population dao pulling the most recent population year from the database:
(defun get-country-with-most-recent-population (country) (car (query-dao 'country-with-population (:order-by (:select (:as 'country-n.name 'country-name) 'year (:as 'region-n.name 'region-name) 'population :distinct-on 'country-n.name :from 'country-n :inner-join 'region-n :on (:= 'country-n.region-id 'region-n.id) :inner-join 'country-population :on (:= 'country-n.id 'country-population.country-id) :where (:= 'country-n.name '$1)) 'country-name (:desc 'year)) country)))
Obviously it is not get-dao, which is simpler but just pulls everything in a single row from a table and this pulls just the data you want from three different tables and it is bespoken for that class. Because get-dao is a generic function, with the normal method being applied when passing a symbol, you could write a new method for get-dao that would apply if you passed it an actual country-with-population class instance.
If you want to display fields in a record which matches a dao class that you have set up, you can call get-dao with the name of table and the primary key. In this example, the table is "countries and the primary key happens to be the field "id" with a value of 1.
For example, assume we pull a dao object out of our country-n table for Croatia:
(describe (get-dao 'country-n 2)) #<COUNTRY-N {1005BF7273}> [standard-object] Slots with :INSTANCE allocation: ID = 2 NAME = "Croatia" REGION-ID = 2
Notice that the region-id field has an integer value. This works. But assume it has a slot of region-id, which refers to an id in the table "regions" and you want the name of the region displayed rather than the region-id. There is a hack using with-column-writers which essentially writes the name into the link slot. Now, we write a function that uses the with-column-writers macro and pull in the actual region name from the regions table.
(defun get-country2 (country-name ) (first (with-column-writers ('region-n 'region-id) (query-dao 'country-n (:select 'country-n.* (:as 'region-n.name 'region-n) :from 'country-n :left-join 'region-n :on (:= 'country-n.region-id 'region-n.id) :where (:= 'country-n.name country-name)))))) (describe (get-country2 "Croatia")) #<COUNTRIES {1003AD23D1}> [standard-object] (describe (get-country2 "Croatia")) #<COUNTRY-N {100593DF03}> [standard-object] Slots with :INSTANCE allocation: ID = 2 NAME = "Croatia" REGION-ID = "Southern Europe" (region-id (get-country2 "Croatia")) "Southern Europe"
Normally calling the accessor region-id would return an integer, but now it is returning the name of the region. if you are using the dao as a simple way to get the relevant data out of the database and you are just going to display this value, this saves you from having to make additional database calls. Otherwise, you would have to make an additional call to get the information from all the foreign tables.
Roles
Every connection is specific to a particular database. However, creating roles or users is global to the entire cluster (the running postgresql server). You can create policies for any individual database, schema or table, but you need to ensure that those policies also apply to any subsequently created database, schema or table. Note that each user is automatically a member of the public group, so you need to change those policies for public as well.
Per the Postgresql Documentation, CREATE ROLE adds a new role to a PostgreSQL database cluster. A role is an entity that can own database objects and have database privileges; a role can be considered a “user”, a “group”, or both depending on how it is used. https://www.postgresql.org/docs/current/sql-createrole.html. The only real difference between "create role" and "create user" is that create user defaults to having a login attribute and create role defaults to not having a login attribute.
Often applications will have their own concept of users and the application will itself have one or more types of roles to which the application user is assigned. So, for example, the application may have two roles - reader and editor with which it interacts with postgresql and then there are many application users registered with the application and probably listed in some type of user table in postgresql that the application manages. When users 1,2 or 3 log in to the application, the application might connect to the postgresql cluster using a role that only has read (select) permissions. When users 4 or 5 log in to the application, the applicatin might connect to the postgresql cluster using a role that has read, insert, update and delete permission. Postmodern provides a simplified create-role system allowing easy creation of roles that have readonly, editor or superuser type permissions. Further, those permissions can be limited to individual databases, schemas or tables.
We suggest that you separate application users from roles. Make it easy to drop application users. Dropping roles requires going through every database, reassigning ownership of any objects that role might own or have privileges on, then dropping ownership of objects, then dropping the role itself.
function role-exists-p (role-name)
→ boolean
Does the named role exist in this database cluster? Returns t or nil.
function create-role
(name password &key (base-role :readonly) (schema :public) (tables :all) (databases :current) (allow-whitespace nil) (allow-utf8 nil) (allow-disallowed-names nil) (comment nil))
Keyword parameters: Base-role. Base-role should be one of :readonly, :editor, :admin, :standard or :superuser. A readonly user can only select existing data in the specified tables or databases. An editor has the ability to insert, update, delete or select data. An admin has all privileges on a database, but cannot create new databases, roles, or replicate the system. A standard user has no particular privileges other than connecting to databases.
:schema defaults to :public but can be a list of schemas. User will not have access to any schemas not in the list.
:tables defaults to :all but can be a list of tables. User will not have access to any tables not in the list.
:databases defaults to :current but can be a list of databases. User will not have access to any databases not in the list.
:allow-whitespace - Whitespace in either the name or password is not allowed by default.
:allow-utf8 defaults to nil. If t, the name and password will be normalized. If nil, the name and password are limited to printable ascii characters. For fun reading on utf8 user names see https://labs.spotify.com/2013/06/18/creative-usernames. Also interesting reading is https://github.com/flurdy/bad_usernames and https://github.com/dsignr/disallowed-usernames/blob/master/disallowed%20usernames.csv, and https://www.b-list.org/weblog/2018/feb/11/usernames/
:allow-disallowed-names defaults to nil. If nil, the user name will be checked against disallowed-role-names.
As an aside, if allowing utf8 in names, you might want to think about whether you should second copy of the username in the original casing and normalized as NFC for display purposes as opposed to normalizing to NFKC. It might be viewed as culturally insensitive to change the display of the name.
function drop-role (role-name &optional (new-owner "postgres") (database :all))
→ boolean
The role-name and optional new-owner name should be strings. If they are symbols, they will be converted to string and hyphens will be converted to underscores.
Before dropping the role, you must drop all the objects it owns (or reassign their ownership) and revoke any privileges the role has been granted on other objects. If database is :all, drop-role will loop through all databases in the cluster ensuring that the role has no privileges or owned objects in every database. Otherwise drop-role will drop objects owned by a role in the current database.
We will reassign ownership of the objects to the postgres role unless otherwise specified in the optional second parameter. Returns t if successful. Will not drop the postgres role.
function alter-role-search-path (role search-path)
Changes the priority of where a role looks for tables (which schema first, second, etc. Role should be a string or symbol. Search-path could be a list of schema names either as strings or symbols.
function change-password (role password &optional expiration-date)
Alters a role's password. If the optional expiration-date parameter is provided, the password will expire at the stated date. A sample expiration date would be 'December 31, 2020'. If the expiration date is 'infinity', it will never expire. The password will be encrypted in the system catalogs. This is automatic with postgresql versions 10 and above.
function grant-role-permissions (role-type name &key (schema :public) (tables :all) (databases :all))
Grant-role-permissions assumes that a role has already been created, but permissions need to be granted or revoked on a particular database.
A :superuser can create databases, roles, replication, etc. Returns nil. A :standard user has no particular privileges or restrictions. Returns nil. An :admin user can edit existing data, insert new data and create new tables in the specified databases/schemas/tables. An :editor user can update fields or insert new records but cannot create new tables in the specified tables or databases. A :readonly role can only read existing data in the specified schemas, tables or databases. Schema, tables or databases can be :all or a list of schemas, tables or databases to be granted permission.
Granting :all provides access to all future items of that type as well.
Note that the schema and table rights and revocations granted are limited to the connected database at the time of execution of this function.
function grant-readonly-permissions (schema-name role-name &optional (table-name nil))
Grants select privileges to a role for the named schema. If the optional table-name parameter is provided, the privileges are only granted with respect to that table. Note that we are giving some function execute permissions if table-name is nil, but if the table-name is specified, those are not provided. Your mileage may vary on how many privileges you want to provide to a read-only role with access to only a limited number of tables.
function grant-editor-permissions (schema-name role-name &optional (table-name nil))
Grants select, insert, update and delete privileges to a role for the named schema. If the optional table-name parameter is provided, the privileges are only granted with respect to that table. Note that we are giving some function execute permissions if table-name is nil, but if the table-name is specified, those are not provided. Your mileage may vary on how many privileges you want to provide to a editor role with access to only a limited number of tables.
function grant-admin-permissions (schema-name role-name &optional (table-name nil))
Grants all privileges to a role for the named schema. If the optional table-name parameter is provided, the privileges are only granted with respect to that table.
function revoke-all-on-table (table-name role-name)
Takes a table-name which could be a string, symbol or list of strings or symbols of tables names, a role name and revokes all privileges that role-name may have with that/those tables. This is limited to the currently connected database and can only revoke the privileges granted by the caller of the function.
function list-role-accessible-databases (role-name)
→ list
Returns a list of the databases to which the specified role can connect.
function list-roles (&optional (lt nil))
→ list
Returns a list of alists of rolenames, role attributes and membership in roles. See https://www.postgresql.org/docs/current/role-membership.html for an explanation. Optionally passing :alists or :plists can be used to set the return list types to :alists or :plists. This is the same as the psql function \du.
function list-role-permissions (&optional role)
→ list
This returns a list of sublists of the permissions granted within the currently connected database. If an optional role is provided, the result is limited to that role. The sublist returned will be in the form of role-name, schema-name, table-name and then a string containing all the rights of that role on that table in that schema.
Database Information
function add-comment (type name comment &optional (second-name ""))
Attempts to add a comment to a particular database object. The first parameter is a keyword for the type of database object. The second parameter is the name of the object. The third parameter is the comment itself. Some objects require an additional identifier. The names can be strings or symbols.
Example usage would be:
(add-comment :database 'my-database-name "Does anyone actually use this database?") (add-comment :column 'country-locations.name "Is what it looks like - the name of a country") (add-comment :column "country_locations.name" "Is what it looks like - the name of a country")
Example usage where two identifiers are required would be constraints:
(add-comment :constraint 'constraint1 "Some kind of constraint descriptions here" 'country-locations)
find-comments (type identifier)
Returns the comments attached to a particular database object. The allowed types are :database :schema :table :columns (all the columns in a table) :column (for a single column).
An example would be (find-comments :table 's2.employees) where the table employees is in the s2 schema.
function get-database-comment (database-name)
→ string
Returns the comment, if any, attached to a database. See also get-schema-comment, get-column-comments and get-database-comment.
function postgresql-version ()
→ string
Returns the version string provided by postgresql of the current postgresql server. E.g. "PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (Arch Linux 9.3.0-1) 9.3.0, 64-bit". If you want just the postgresql version number, use (cl-postgres:get-postgresql-version).
function database-version ()
→ string
DEPRECATED. This returns the postgresql server version number, not a version number from the currently connected database. The format of the return string is determined by the current postgresql server. E.g. "PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (Arch Linux 9.3.0-1) 9.3.0, 64-bit".
If you want just the postgresql version number, use (cl-postgres:get-postgresql-version).
function database-exists-p (database-name)
→ boolean
Checks to see if a particular database exists. Returns T if true, nil if not.
function database-size (&optional database-name)
→ list
Given the name of a database, will return the name, a pretty-print string of the size of the database and the size in bytes. If a database name is not provided, it will return the result for the currently connected database.
function num-records-in-database ()
→ list
Returns a list of lists with schema, table name and approximate number of records in the currently connected database.
function list-databases (&key (order-by-size nil) (size t))
→ list
Returns a list of lists where each sub-list contains the name of the database, a pretty-print string of the size of that database and the size in bytes. The default order is by database name. Pass t as a parameter to :order-by-size for order by size. Setting size to nil will return just the database names in a single list ordered by name. This function excludes the template databases
function list-database-functions ()
→ list
Returns a list of the functions in the database from the information_schema.
DEPRECATED FOR DESCRIBE-TRIGGERS. List detailed information on the triggers from the information_schema table.
function list-database-users ()
→ list
List database users (actually 'roles' in Postgresql terminology).
function list-database-access-rights (&optional database-name)
→ list
If the database parameter is specifed, this returns an list of lists where each sublist is a role name and whether they have access rights (t or nil) to that particular database. If the database-name is not provided, the sublist is a database name, a role name and whether they have access rights (t or nil). This excludes the template databases.
function list-available-types ()
→ list
List the available data types in the connected postgresql version, It returns a list of lists, each sublist containing the oid (object identifier number) and the name of the data types. E.g. (21 "smallint")
function list-available-collations ()
→ list
Get a list of the collations available from the current database cluster. Collations are a mess as different operating systems provide different collations. We might get some sanity if Postgresql can use ICU as the default. See https://wiki.postgresql.org/wiki/Collations.
function list-available-extensions ()
→ list
List the postgresql extensions which are available in the system to the currently connected database. The extensions may or may not be installed.
function list-installed-extensions ()
→ list
List the postgresql extensions which are installed in the currently connected database.
function change-toplevel-database (new-database user password host)
→ string
Just changes the database assuming you are using a toplevel connection. Recommended only for development work. Returns the name of the newly connected database as a string.
function cache-hit-ratio ()
→ list
The cache hit ratio shows data on serving the data from memory compared to how often you have to go to disk. This function returns a list of heapblocks read from disk, heapblocks hit from memory and the ratio of heapblocks hit from memory / total heapblocks hit. Borrowed from: https://www.citusdata.com/blog/2019/03/29/health-checks-for-your-postgres-database/
function bloat-measurement ()
→ list
Bloat measurement of unvacuumed dead tuples. Borrowed from: https://www.citusdata.com/blog/2019/03/29/health-checks-for-your-postgres-database/ who borrowed it from https://github.com/heroku/heroku-pg-extras/tree/master/commands.
function unused-indexes ()
→ list
Returns a list of lists showing schema.table, indexname, index_size and number of scans. The code was borrowed from: https://www.citusdata.com/blog/2019/03/29/health-checks-for-your-postgres-database/
function check-query-performance (&optional (ob nil) (num-calls 100) (limit 20))
→ list
This function requires that postgresql extension pg_stat_statements must be loaded via shared_preload_libraries. It is borrowed from https://www.citusdata.com/blog/2019/03/29/health-checks-for-your-postgres-database/. Optional parameters:
OB allow order-by to be 'calls', 'total-time', 'rows-per' or 'time-per', defaulting to time-per.
num-calls to require that the number of calls exceeds a certain threshold, and limit to limit the number of rows returned. It returns a list of lists, each row containing the query, number of calls, total_time, total_time/calls, stddev_time, rows, rows/calls and the cache hit percentage.
Constraints
function list-unique-or-primary-constraints (table-name)
→ list
List constraints on a table. Table-name can be either a string or quoted. Turns constraints into keywords if strings-p is not true.
function list-all-constraints (table-name)
→ list
Users information_schema to list all the constraints in a table. Table-name can be either a string or quoted. Turns constraints into keywords if strings-p is not true.
Indexes/Indices
function create-index (name &key unique if-not-exists concurrently on using fields)
Create an index. Slightly less sophisticated than the query version because it does not have a where clause capability.
function drop-index (name &key concurrently if-exists cascade)
Drop an index. Available keys are :concurrently, :if-exists, and :cascade.
function list-indices (&optional strings-p)
→ list
Return a list of the indexs in a database. Turn them into keywords if strings-p is not true.
function list-table-indices (table-name &optional strings-p)
→ list
List the index names and the related columns in a single table. Each index will be in a separate sublist.
function index-exists-p (name)
→ boolean
Tests whether an index with the given name exists. The name can be either a string or a symbol.
Keys
function find-primary-key-info (table-name &optional (just-key nil))
→ list
Returns a list of sublists where the sublist contains two strings. If a table primary key consists of only one column, such as 'id' there will be a single sublist where the first string is the name of the column and the second string is the string name for the datatype for that column. If the primary key for the table consists of more than one column, there will be a sublist for each column subpart of the key. The sublists will be in the order they are used in the key, not in the order they appear in the table. If just-key is set to t, the list being returned will contain just the column names in the primary key as string names with no sublists. If the table is not in the public schema, provide the fully qualified table name e.g. schema-name.table-name.
Schema/Schemata
Schema allow you to separate tables into differnet name spaces. In different schemata two tables with the same name are allowed to exists. The tables can be referred by fully qualified names or with the macro with-schema. You could also set the search path with set-search-path. For listing end checking there are also the functions list-schemata and schema-exist-p. The following functions allow you to create, drop schemata and to set the search path.
macro with-schema ((namespace &key :strict t :if-not-exist :create :drop-after) &body body)
A macro to set the schema search path (namespace) of the postgresql database to include as first entry a specified schema and then executes the body. Before executing body the PostgreSQL's session variable search_path is set to the given namespace. After executing body the search_path variable is restored to the original value.
Calling with :strict 't only the specified schema is set as current search path. All other schema are then not searched any more. If strict is nil, the namespace is just first schema on the search path upon the the body execution.
Calling with :if-not-exist set to :create the schema is created if this schema did not exist.
Calling with :if-not-exist set to nil, an error is signaled.
calling with drop-after set to 't the schema is removed after the execution of the body form.
example : (with-schema (:schema-name :strict nil :drop-after nil :if-not-exist :error) (foo 1) (foo 2))
example : (with-schema ('uniq :if-not-exist :create) ;; changing the search path (schema-exists-p 'uniq))
function list-schemata ()
→ list
List all existing user defined schemata.
Note: The query uses the portable information_schema relations instead of pg_tables relations.
select schema_name from information_schema.schemata where schema_name !~ '(pg_*)|information_schema' order by schema_name ;
function list-schemas ()
→ list
List schemas in the current database, excluding the pg_* system schemas.
function schema-exists-p (schema)
→ boolean
Tests the existence of a given schema. Returns T if the schema exists or NIL otherwise. The name provided can be either a string or quoted symbol.
function create-schema (schema)
Creates a new schema. Raises an error if the schema is already exists.
function drop-schema (schema &key (if-exists nil) (cascade nil))
Drops an existing database schema. Accepts :if-exists and/or :cascade arguments like :drop-table. A notice instead of an error is raised with the is-exists parameter.
function set-search-path (path)
This changes the postgresql runtime parameter controlling what order schemas are searched. You can always use fully qualified names [schema.table]. By default, this function only changes the search path for the current session. This function is used by with-schema.
function split-fully-qualified-tablename (name)
→ list Take a tablename of the form database.schema.table or schema.table or table and return the tablename and the schema name. The name can be a symbol or a string. Returns a list of form '(table schema database. If the tablename is not fully qualified, it will assume that the schema should be \"public\".
function get-schema-comment (schema-name)
→ string
Retrieves the comment, if any attached to the schema. See also get-schema-comment, get-column-comments and get-database-comment.
Sequences
function create-sequence (name &key temp if-not-exists increment min-value max-value start cache)
Create a sequence. Available additional key parameters are :temp :if-not-exists :increment :min-value :max-value :start and :cache. See https://www.postgresql.org/docs/current/static/sql-createsequence.html for details on usage.
function sequence-next (sequence)
→ integer
Shortcut for getting 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 according to S-SQL rules.
function drop-sequence (name &key if-exists cascade)
→ list
Drop a sequence. Name should be quoted. Available key parameters are :if-exists and :cascade.
Tables
function list-tables (&optional strings-p)
→ list
DEPRECATED FOR LIST-ALL-TABLES. Return a list of the tables in the public schema of a database. By default the table names are returned as keywords. They will be returned as lowercase strings if strings-p is true.
function list-all-tables (&optional (fully-qualified-names-only nil))
:ID: bd228cd6-3651-48ca-a9c5-a27737fbaacc
→ list
If fully-qualified-names-only is set to t, returns a flattened list of all schema.table names other than pg_catalog or the information_schema.
Otherwise returns the following info:
schema-name, table-name, table-owner, tablespace, hasindexes, hasrules, hastriggers and rowsecurity(&optional strings-p).
function list-tables-in-schema (&optional (schema-name "public") (strings-p nil))
→ list
Returns a list of tables in a particular schema, defaulting to public. If schema-name is :all, it will return all the non-system tables in the database in fully qualified form: e.g. 'public.test_table'. If string-p is t, the names will be returned as strings with underscores converted to hyphens.
function list-table-sizes (&key (schema "public") (order-by-size nil) (size t))
→ list
Returns a list of lists (table-name, size in 8k pages) of tables in the current database. Providing a name to the schema parameter will return just the information for tables in that schema. It defaults to just the tables in the public schema. Setting schema to nil will return all tables, indexes etc in the database in descending order of size. This would include system tables, so there are a lot more than you would expect. If :size is set to nil, it returns only a flat list of table names. Setting order-by-size to t will return the result in order of size instead of by table name.
function table-exists-p (name)
→ boolean
Check whether a table exists in a particular schema. Defaults to the search path. Takes either a string or a symbol for the table name. The table-name can be fully qualified in the form of schema.table-name or database.schema.table-name. If the schema is specified either in a qualified table-name or in the optional schema-name parameter, we look directly to the information schema tables. Otherwise we use the search path which can be controlled by being within a with-schema form.
function table-size (table-name)
→ list
Return the size of a given postgresql table in k or m. Table-name can be either a string or quoted.
function table-description (name &optional schema-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.
Table can be either a string or quoted. Table-names can be fully qualified with the schema or not. If the table-name is not fully qualified and a schema name is not provided, the table will be assumed to be in the public schema.
function table-description-plus (table-name)
→ list
Returns more table info than table-description. Specifically returns ordinal-position, column-name, data-type, character-maximum-length, modifier, whether it is not-null and the default value.
Table can be either a string or quoted. Table-names can be fully qualified with the schema or not. If the table-name is not fully qualified and a schema name is not provided, the table will be assumed to be in the public schema.
function table-description-menu (table-name &key char-max-length data-type-length
has-default default-value not-null numeric-precision numeric-scale storage primary primary-key-name unique unique-key-name fkey fkey-name fkey-col-id fkey-table fkey-local-col-id identity generated collation col-comments locally-defined inheritance-count stat-collection) → list string list
Takes a fully qualified table name which can be either a string or a symbol.
Returns three values.
- A list of plists of each row's parameters. This will always
include :column-name and :data-type-name but all other parameters can be set or unset and are set by default (set to t).
- The comment string attached to the table itself (if any).
- A list of the check constraints applied to the rows in the table. See documentation for
list-check-constraints for an example.
The available keyword parameters are:
- data-type-length (For a fixed-size type, typlen is the number of bytes in the internal representation of the type. But for a variable-length type, typlen is negative. -1 indicates a “varlena” type (one that has a length word), -2 indicates a null-terminated C string.)
- char-max-length (Typically used for something like a varchar and shows the maximum length)
- has-default (value T if this column has a default value and :NULL if not)
- default-value (value is the default value as string. A default of 9.99 will still be a string)
- not-null (value is T if the column must have a value or :NULL otherwise)
- numeric-precision (value is the total number of digits for a numeric type if that precision was specified)
- numeric-scale (value is the number of digits in the fraction part of a numeric type if that scale was specified)
- storage (value is the storage setting for a column. Result can be plain, extended, main or external)
- primary (value is T if the column is the primary key for the table, :NULL otherwise)
- primary-key-name (value is the name of the primary-key itself, not the column, if the column is the primary key for the table, :NULL otherwise)
- unique (value is T if the column is subject to a unique key, :NULL otherwise)
- unique-key-name (value is the name of the unique-key itself, not the column, applied to the column, :NULL otherwise)
- fkey (value is T if the column is a foreign key, :NULL otherwise)
- fkey-name (value is the name of the foreign key, :NULL otherwise)
- fkey-col-id (value is the column id of the foreign table used as the foreign key. Probably easier to use the Postmodern function list-foreign-keys if you are looking for the name of the columns)
- fkey-table (value is the name of the foreign table, :NULL otherwise)
- fkey-local-col-id (value is the column id of this column. Probably easier to use the Postmodern function list-foreign-keys if you are looking for the name of the columns involved in the foreign key)
- identity (if the column is an identity column, the values can be 'generated always' or 'generated by default'. Otherwise :NULL)
- generated (columns can be generated, if this column is generated and stored on disk, the value will be 'stored', otherwise :NULL)
- collation (columns with collations which are not the default collation for the database will show that collation here, otherwise :NULL)
- col-comments (value is any comment that has been applied to the column, :NULL otherwise)
- locally-defined (value is T if locally defined. It might be both locally defined and inherited)
- inheritance-count (the number of direct ancestors this column has inherited)
- stat-collection (stat-collection returns the value of attstattarget which controls the level of detail of statistics accumulated for this column by ANALYZE. A zero value indicates that no statistics should be collected. A negative value says to use the system default statistics target. The exact meaning of positive values is data type-dependent. For scalar data types, attstattarget is both the target number of most common values to collect, and the target number of histogram bins to create. Attstorage is normally a copy of pg_type.typstorage of this column's type. For TOAST-able data types, this can be altered after column creation to control storage policy.)
function list-check-constraints (table-name)
→ list
Takes a fully qualified table name and returns a list of lists of check constraints where each sublist has the form of (check-constraint-name check)
Example:
(query (:create-table 'employees2 ((did :type (or integer db-null) :primary-key "generated by default as identity") (name :type (varchar 40) :check (:<> 'name "")) (birth-date :type date :check (:> 'birth-date "1900-01-01")) (start-date :type date :check (:> 'start-date 'birth-date)) (salary :type numeric :check (:> 'salary 0))))) (list-check-constraints 'employees2) (("employees2_birth_date_check" "CHECK (birth_date > '1900-01-01'::date)") ("employees2_check" "CHECK (start_date > birth_date)") ("employees2_name_check" "CHECK (name::text <> ''::text)") ("employees2_salary_check" "CHECK (salary > 0::numeric)"))
function list-columns (table-name)
→ list
Returns a list of strings of just the column names in a table. Pulls info from the postmodern table-description function rather than directly. The table-name can be a string or quoted. Any table-name that is not fully qualified with the schema will be assumed to be in the public schema.
function list-columns-with-types (table-name)
→ list
Returns a list of (name type) lists for the fields of a table. Returns a list of strings of just the column names and their sql data types in a table. Pulls info from the postmodern table-description function rather than directly. The table-name can be a string or quoted. Any table-name that is not fully qualified with the schema will be assumed to be in the public schema.
function column-exists-p (table-name column-name &optional schema-name)
→ boolean
Determine if a particular column exists. Table name and column-name can be either strings or symbols. If the optional schema name is not given or the table-name is not fully qualified with a schema name, the schema will be assumed to be the public schema.
function get-table-oid (table-name &optional schema-name)
→ integer
Retrieves the oid identifier for a particular table from postgresql. Works for tables in all schemas.
function get-table-comment (table-name &optional schema-name)
→ string
Retrieves the comment, if any attached to the table. See also get-schema-comment, get-column-comments and get-database-comment
function get-column-comments (database schema table)
→ string
Retrieves a list of lists of column names and the comments, if any, attached to the columns of a table.
function rename-table (old-name new-name)
→ boolean
Renames a table. Parameters can be strings or symbols. If you are renaming a table using a fully qualified schema.table-name, you do not need to specify the schema in the new-name. You cannot use this function to move tables from one schema to another. Returns t if successful
function rename-column (table-name old-name new-name)
→ boolean
Rename a column in a table. Parameters can be strings or symbols. If the table is not in the public schema, it needs to be fully qualified - e.g. schema.table. Returns t if successful
Tablespaces
function list-tablespaces ()
→ list
Lists the tablespaces in the currently connected database. What are tablespace you ask? Per the Postgresql documentation https://www.postgresql.org/docs/current/manage-ag-tablespaces.html: Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.
Triggers
function describe-triggers ()
→ list
List detailed information on the triggers from the information_schema table.
function list-triggers (&optional table-name)
→ list
List distinct trigger names from the information_schema table. Table-name can be either quoted or string. (A trigger is a specification that the database should automatically execute a particular function whenever a certain type of operation is performed. Triggers can be attached to tables (partitioned or not), views, and foreign tables. See https://www.postgresql.org/docs/current/trigger-definition.html)
Views
function list-views (&optional strings-p)
→ list
Returns list of the user defined views in the current database. When strings-p is T, the names will be returned as strings, otherwise as keywords.
Miscellaneous Utility Functions
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.
function execute-file (filename &optional (print nil))
This function will execute sql queries stored in a file. Each sql statement in the file will be run independently, but if one statement fails, subsequent query statements will not be run, but any statement prior to the failing statement will have been commited.
If you want the standard transction treatment such that all statements succeed or no statement succeeds, then ensure that the file starts with a "begin transaction" statement and finishes with an "end transaction" statement. See the test file test-execute-file-broken-transaction.sql as an example.
For debugging purposes, if the optional print parameter is set to t, format will print the count of the query and the query to the REPL.
IMPORTANT NOTE: This utility function assumes that the file containing the sql queries can be trusted and bypasses the normal postmodern parameterization of queries.
function num-records-in-database ()
→ list
Returns a list of lists with schema, table name and approximate number of records in the currently connected database.
Imported From s-sql
macro sql (form)
→ string
Convert the given form (a list starting with a keyword) to an SQL query string at compile time, according to the rules described here. For example:
(sql (:select '* :from 'country :where (:= 'a 1))) "(SELECT * FROM country WHERE (a = 1))"
but
(sql '(:select '* :from 'country :where (:= 'a 1)))
would throw an error. For the later case you need to use sql-compile.
function sql-compile (form)
→ string
This is the run-time variant of the sql macro. It converts the given list to an SQL query, with the same rules except that symbols in this list do not have to be quoted to be interpreted as identifiers. For example:
(sql-compile '(:select '* :from 'country :where (:= 'a 1))) \"(SELECT * FROM country WHERE (a = 1))\"
but
(sql (:select '* :from 'country :where (:= 'a 1)))
would throw an error. For the later case you need to use sql.
deftype db-null ()
Type for representing NULL values. Use like (or integer db-null) for declaring a type to be an integer that may be null." '(eql :null)
function from-sql-name (str)
Convert a string to a symbol, upcasing and replacing underscores with hyphens.
function parse-queries (file-content)
→ list
Read SQL queries in given string and split them, returns a list.
function sql-escape-string (string)
→ string
Escapes a string for inclusion in a PostgreSQL query. Example:
(sql-escape-string \"Puss in 'Boots'\") \"E'Puss in ''Boots'''\"
method sql-escape (arg)
A generalisation of sql-escape-string looks at the type of the value passed, and properly writes it out it for inclusion in an SQL query. Symbols will be converted to SQL names. Examples:
(sql-escape "tr'-x") "E'tr''-x'" (sql-escape (/ 1 13)) "0.0769230769230769230769230769230769230" (sql-escape #("Baden-Wurttemberg" "Bavaria" "Berlin" "Brandenburg")) "ARRAY[E'Baden-Wurttemberg', E'Bavaria', E'Berlin', E'Brandenburg']"
macro 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.
variable escape-sql-names-p
Determines whether double quotes are added around column, table, and ** function names in queries. Valid values:
- T, in which case every name is escaped,
- NIL, in which case no name is escape,
- :auto, which causes only reserved words to be escaped, or.
- :literal which is the same as :auto except it has added consequence in
to-sql-name (see below).
The default value is :auto.
Be careful when binding this with let and such ― since a lot of SQL compilation tends to happen at compile-time, the result might not be what you expect. Mixed case sensitivity is not currently well supported. Postgresql itself will downcase unquoted identifiers. This will be revisited in the future if requested.
function to-sql-name (name &optional (escape-p escape-sql-names-p) (ignore-reserved-words nil))
Convert a symbol or string into a name that can be a sql table, column, or operation name. Add quotes when escape-p is true, or escape-p is :auto and the name contains reserved words. Quoted or delimited identifiers can be used by passing :literal as the value of escape-p. If escape-p is :literal, and the name is a string then the string is still escaped but the symbol or string is not downcased, regardless of the setting for downcase-symbols and the hyphen and forward slash characters are not replaced with underscores.
Ignore-reserved-words is only used internally for column names which are allowed to be reserved words, but it is not recommended.
Conditions Imported From cl-postgres
condition database-connection-error
Conditions of this type are signalled when an error occurs that breaks the connection socket. They offer a :reconnect restart.
condition database-error
This is the condition type that will be used to signal virtually all database-related errors (though in some cases socket errors may be raised when a connection fails on the IP level).
reader database-error-code
Code: the Postgresql SQLSTATE code for the error (see the Postgresql Manual Appendix A for their meaning). Not localizable. Always present.
accessor database-error-message
Message: the primary human-readable error message. This should be accurate but terse (typically one line). Always present.