replace numeric constants with oid symbols
[postmodern.git] / doc / postmodern.html
blob1e8300c86fcf7a08a5717f2b64ae22400d079c7e
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Postmodern reference manual</title>
6 <link rel="stylesheet" type="text/css" href="style.css"/>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
8 </head>
10 <body>
12 <h1>Postmodern reference manual</h1>
14 <p>This is the reference manual for the component named
15 <code>postmodern</code>, which is part of a <a
16 href="index.html">library</a> of the same name.</p>
18 <p>Note that this package also exports the <a
19 href="cl-postgres.html#database-connection"><code>database-connection</code></a>
20 and <a
21 href="cl-postgres.html#database-error"><code>database-error</code></a>
22 types from <a href="cl-postgres.html">CL-postgres</a> and a few
23 operators from <a href="s-sql.html">S-SQL</a>.</p>
25 <p><a href="#query"><code>query</code></a>, <a
26 href="#execute"><code>execute</code></a>, and any other function
27 that would logically need to communicate with the database will
28 raise a condition of the type <a
29 href="cl-postgres.html#conditions"><code>database-error</code></a>
30 when something goes wrong. As a special case, errors that break
31 the connection (socket errors, database shutdowns) will be raised
32 as subtypes of <a
33 href="cl-postgres.html#database-connection-error"><code>database-connection-error</code></a>,
34 providing a <code>:reconnect</code> restart to re-try the
35 operation that encountered to the error.</p>
37 <h2>Contents</h2>
39 <ol>
40 <li><a href="#connecting">Connecting</a></li>
41 <li><a href="#querying">Querying</a></li>
42 <li><a href="#inspect">Inspecting the database</a></li>
43 <li><a href="#daos">Database access objects</a></li>
44 <li><a href="#tabledef">Table definition and creation</a></li>
45 <li><a href="#schemata">Schemata</a></li>
46 <li><a href="#index">Symbol-index</a></li>
47 </ol>
49 <h2><a name="connecting"></a>Connecting</h2>
51 <p class="def">
52 <span>class</span>
53 <a name="database-connection"></a>
54 database-connection
55 </p>
57 <p class="desc">Objects of this type represent database connections.</p>
59 <p class="def">
60 <span>function</span>
61 <a name="connect"></a>
62 connect (database user password host &amp;key (port 5432) pooled-p use-ssl)
63 <br/>&#8594; database-connection
64 </p>
66 <p class="desc">Create a new database connection for the given
67 <code>user</code> and the <code>database</code>. Port will default
68 to 5432, which is where most PostgreSQL servers are running. If
69 <code>pooled-p</code> is <code>T</code>, a connection will be taken
70 from a pool of connections of this type, if one is available there,
71 and when the connection is disconnected it will be put back into this
72 pool instead. <code>use-ssl</code> can be <code>:no</code>,
73 <code>:yes</code>, or <code>:try</code>, as in <a
74 href="cl-postgres.html#open-database"><code>open-database</code></a>,
75 and defaults to the value of <a
76 href="#*default-use-ssl*"><code>*default-use-ssl*</code></a>.</p>
78 <p class="def">
79 <span>variable</span>
80 <a name="*default-use-ssl*"></a>
81 *default-use-ssl*
82 </p>
84 <p class="desc">The default for <a
85 href="#connect"><code>connect</code></a>'s <code>use-ssl</code>
86 argument. This starts at <code>:no</code>. If you set it to
87 anything else, be sure to also load the <a
88 href="http://common-lisp.net/project/cl-plus-ssl/">CL+SSL</a>
89 library.</p>
91 <p class="def">
92 <span>method</span>
93 <a name="disconnect"></a>
94 disconnect (database-connection)
95 </p>
97 <p class="desc">Disconnects a normal database connection, or moves
98 a pooled connection into the pool.</p>
100 <p class="def">
101 <span>function</span>
102 <a name="connected-p"></a>
103 connected-p (database-connection)
104 <br/>&#8594; boolean
105 </p>
107 <p class="desc">Returns a boolean indicating whether the given
108 connection is still connected to the server.</p>
110 <p class="def">
111 <span>method</span>
112 <a name="reconnect"></a>
113 reconnect (database-connection)
114 </p>
116 <p class="desc">Reconnect a disconnected database connection. This
117 is not allowed for pooled connections &#x2015; after they are
118 disconnected they might be in use by some other process, and
119 should no longer be used.</p>
121 <p class="def">
122 <span>variable</span>
123 <a name="*database*"></a>
124 *database*
125 </p>
127 <p class="desc">Special variable holding the current database.
128 Most functions and macros operating on a database assume this
129 binds to a connected database.</p>
131 <p class="def">
132 <span>macro</span>
133 <a name="with-connection"></a>
134 with-connection (spec &amp;body body)
135 </p>
137 <p class="desc">Evaluates the <code>body</code> with <a
138 href="#*database*"><code>*database*</code></a> bound to a
139 connection as specified by <code>spec</code>, which should be list
140 that <a href="#connect"><code>connect</code></a> can be applied
141 to.</p>
143 <p class="def">
144 <span>macro</span>
145 <a name="call-with-connection"></a>
146 call-with-connection (spec thunk)
147 </p>
149 <p class="desc">The functional backend to <a
150 href="#with-connection"><code>with-connection</code></a>. Binds <a
151 href="#*database*"><code>*database*</code></a> to a new connection
152 as specified by <code>spec</code>, which should be a list that <a
153 href="#connect"><code>connect</code></a> can be applied to, and
154 runs the zero-argument function given as second argument in the
155 new environment. When the function returns or throws, the new
156 connection is disconnected.</p>
158 <p class="def">
159 <span>function</span>
160 <a name="connect-toplevel"></a>
161 connect-toplevel (database user password host &amp;key (port 5432))
162 </p>
164 <p class="desc">Bind the <a
165 href="#*database*"><code>*database*</code></a> to a new
166 connection. Use this if you only need one connection, or if you
167 want a connection for debugging from the REPL.</p>
169 <p class="def">
170 <span>function</span>
171 <a name="disconnect-toplevel"></a>
172 disconnect-toplevel ()
173 </p>
175 <p class="desc">Disconnect the <a
176 href="#*database*"><code>*database*</code></a>.</p>
178 <p class="def">
179 <span>function</span>
180 <a name="clear-connection-pool"></a>
181 clear-connection-pool ()
182 </p>
184 <p class="desc">Disconnect and remove all connections from the
185 connection pools.</p>
187 <p class="def">
188 <span>variable</span>
189 <a name="*max-pool-size*"></a>
190 *max-pool-size*
191 </p>
193 <p class="desc">Set the maximum amount of connections kept in a
194 <em>single</em> connection pool, where a pool consists of all the
195 stored connections with the exact same connect arguments. Defaults
196 to <code>NIL</code>, which means there is no maximum.</p>
198 <h2><a name="querying"></a>Querying</h2>
200 <p class="def">
201 <span>macro</span>
202 <a name="query"></a>
203 query (query &amp;rest args/format)
204 <br/>&#8594; result
205 </p>
207 <p class="desc">Execute the given <code>query</code>, which can be
208 either a string or an <a href="s-sql.html">S-SQL</a> form (list starting
209 with a keyword). If the query contains placeholders
210 (<code>$1</code>, <code>$2</code>, etc) their values can be given
211 as extra arguments. If one of these arguments is a keyword
212 occurring in the table below, it will not be used as a <code>query</code>
213 argument, but will determine the <code>format</code> in which the results
214 are returned instead. Any of the following formats can be used, with
215 the default being <code>:rows</code>:</p>
217 <table class="desc">
218 <tr><td><code>:none</code></td><td>Ignore the result values.</td></tr>
219 <tr><td><code>:lists</code>, <code>:rows</code></td><td>Return a
220 list of lists, each list containing the values for a
221 row.</td></tr>
222 <tr><td><code>:list</code>, <code>:row</code></td><td>Return a
223 single row as a list.</td></tr>
224 <tr><td><code>:alists</code></td><td>Return a list of alists which map column
225 names to values, with the names represented as
226 keywords.</td></tr>
227 <tr><td><code>:alist</code></td><td>Return a single row as an alist.</td></tr>
228 <tr><td><code>:str-alists</code></td><td>Like
229 <code>:alists</code>, but use the original column
230 names.</td></tr>
231 <tr><td><code>:str-alist</code></td><td>Return a single row as an alist, with
232 strings for names.</td></tr>
233 <tr><td><code>:plists</code></td><td>Return a list of plists which map column
234 names to values,with the names represented as keywords.</td></tr>
235 <tr><td><code>:plist</code></td><td>Return a single row as a plist.</td></tr>
236 <tr><td><code>:column</code></td><td>Return a single column as a list.</td></tr>
237 <tr><td><code>:single</code></td><td>Return a single value.</td></tr>
238 <tr><td><code>:single!</code></td><td>Like <code>:single</code>,
239 but raise an error when the number of selected rows is not equal
240 to 1.</td></tr>
241 <tr><td><code>(:dao type)</code></td><td>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 <a href="#query-dao"><code>query-dao</code></a>.</td></tr>
242 <tr><td><code>(:dao type :single)</code></td><td>Return a single DAO of the given type.</td></tr>
243 </table>
245 <p class="desc">If the database returns information about the
246 amount rows that were affected, such as with updating or deleting
247 queries, this is returned as a second value.</p>
249 <p class="def">
250 <span>macro</span>
251 <a name="execute"></a>
252 execute (query &amp;rest args)
253 </p>
255 <p class="desc">Like <a href="#query"><code>query</code></a>
256 called with <code>format :none</code>. Returns the amount of
257 affected rows as its first returned value. (Also returns this
258 amount as the second returned value, but use of this is
259 deprecated.)</p>
261 <p class="def">
262 <span>macro</span>
263 <a name="doquery"></a>
264 doquery (query (&amp;rest names) &amp;body body)
265 </p>
267 <p class="desc">Execute the given <code>query</code> (a string or a list
268 starting with a keyword), iterating over the rows in the result.
269 The <code>body</code> will be executed with the values in the row bound to the
270 symbols given in <code>names</code>. To iterate over a
271 parameterised query, one can specify a list whose <em>car</em> is the
272 query, and whose <em>cdr</em> contains the arguments. For example:</p>
274 <pre class="code">
275 (doquery (:select 'name 'score :from 'scores) (n s)
276 (incf (gethash n *scores*) s))
278 (doquery ((:select 'name :from 'scores :where (:> 'score '$1)) 100) (name)
279 (print name))</pre>
281 <p class="def">
282 <span>macro</span>
283 <a name="prepare"></a>
284 prepare (query &amp;optional (format :rows))
285 <br/>&#8594; function
286 </p>
288 <p class="desc">Creates a function that can be used as the
289 interface to a prepared statement. The given <code>query</code>
290 (either a string or an <a href="s-sql.html">S-SQL</a> form) may contain
291 placeholders, which look like <code>$1</code>, <code>$2</code>,
292 etc. The resulting function takes one argument for every
293 placeholder in the <code>query</code>, executes the prepared query,
294 and returns the result in the <code>format</code> specified. (Allowed
295 formats are the same as for <a href="#query"><code>query</code></a>.)</p>
297 <p class="desc">For queries that have to be run very often,
298 especially when they are complex, it may help performance since the
299 server only has to plan them once. See <a
300 href="http://www.postgresql.org/docs/current/static/sql-prepare.html">the
301 PostgreSQL manual</a> for details.</p>
303 <p class="desc">In some cases, the server will complain about not
304 being able to deduce the type of the arguments in a statement. In
305 that case you should add type declarations (either with the PostgreSQL's
306 <code>CAST</code> SQL-conforming syntax or historical <code>::</code>
307 syntax, or with S-SQL's <a href="s-sql.html#type"><code>:type</code></a>
308 construct) to help it out.</p>
310 <p class="def">
311 <span>macro</span>
312 <a name="defprepared"></a>
313 defprepared (name query &amp;optional (format :rows))
314 </p>
316 <p class="desc">This is the <code>defun</code>-style variant of <a
317 href="#prepare"><code>prepare</code></a>. It will define a
318 top-level function for the prepared statement.</p>
320 <p class="def">
321 <span>macro</span>
322 <a name="defprepared-with-names"></a>
323 defprepared-with-names (name (&amp;rest args) (query &amp;rest query-args) &amp;optional (format :rows))
324 </p>
326 <p class="desc">Like <a href="#defprepared"><code>defprepared</code></a>,
327 but allows to specify names of the function arguments as well as arguments
328 supplied to the <code>query</code>.</p>
330 <pre class="code">
331 (defprepared-with-names user-messages (user &amp;key (limit 10))
332 ("select * from messages
333 where user_id = $1
334 order by date desc
335 limit $2" (user-id user) limit)
336 :plists)
337 </pre>
339 <p class="def">
340 <span>macro</span>
341 <a name="with-transaction"></a>
342 with-transaction ((&amp;optional name) &amp;body body)
343 </p>
345 <p class="desc">Execute the given <code>body</code> within a database
346 transaction, committing it when the <code>body</code> exits normally, and
347 aborting otherwise. An optional <code>name</code> can be given to the
348 transaction, which can be used to force a commit or abort before
349 the <code>body</code> unwinds.</p>
351 <p class="def">
352 <span>function</span>
353 <a name="commit-transaction"></a>
354 commit-transaction (transaction)
355 </p>
357 <p class="desc">Commit the given <code>transaction</code>.</p>
359 <p class="def">
360 <span>function</span>
361 <a name="abort-transaction"></a>
362 abort-transaction (transaction)
363 </p>
365 <p class="desc">Roll back the given <code>transaction</code>.</p>
367 <p class="def">
368 <span>macro</span>
369 <a name="with-savepoint"></a>
370 with-savepoint (name &amp;body body)
371 </p>
373 <p class="desc">Can only be used within a transaction. Establishes
374 a savepoint with the given <code>name</code> at the start of
375 <code>body</code>, and binds the same <code>name</code> to a handle
376 for that savepoint. At the end of <code>body</code>, the savepoint
377 is released, unless a condition is thrown, in which case it is rolled
378 back.</p>
380 <p class="def">
381 <span>function</span>
382 <a name="release-savepoint"></a>
383 release-savepoint (savepoint)
384 </p>
386 <p class="desc">Release the given <code>savepoint</code>.</p>
388 <p class="def">
389 <span>function</span>
390 <a name="rollback-savepoint"></a>
391 rollback-savepoint (savepoint)
392 </p>
394 <p class="desc">Roll back the given <code>savepoint</code>.</p>
396 <p class="def">
397 <span>function</span>
398 <a name="commit-hooks"></a>
399 commit-hooks (transaction-or-savepoint),
400 setf (commit-hooks transaction-or-savepoint)
401 </p>
403 <p class="desc">An accessor for the transaction or savepoint's list
404 of commit hooks, each of which should be a function with no required
405 arguments. These functions will be executed when a transaction is
406 committed or a savepoint released.</p>
408 <p class="def">
409 <span>function</span>
410 <a name="abort-hooks"></a>
411 abort-hooks (transaction-or-savepoint),
412 setf (abort-hooks transaction-or-savepoint)
413 </p>
415 <p class="desc">An accessor for the transaction or savepoint's list
416 of abort hooks, each of which should be a function with no required
417 arguments. These functions will be executed when a transaction is
418 aborted or a savepoint rolled back (whether via a non-local transfer
419 of control or explicitly by either <a href="#abort-transaction">
420 <code>abort-transaction</code></a> or <a href="#rollback-savepoint">
421 <code>rollback-savepoint</code></a>).
423 <p class="def">
424 <span>macro</span>
425 <a name="with-logical-transaction"></a>
426 with-logical-transaction ((&amp;optional name) &amp;body body)
427 </p>
429 <p class="desc">Executes <code>body</code> within
430 a <a href="#with-transaction"><code>with-transaction</code></a>
431 form if no transaction is currently in progress, otherwise simulates
432 a nested transaction by executing it within a <code>with-savepoint</code>
433 form. The transaction or savepoint is bound to <code>name</code> if one is
434 supplied.</p>
436 <p class="def">
437 <span>function</span>
438 <a name="abort-logical-transaction"></a>
439 abort-logical-transaction (transaction-or-savepoint)
440 </p>
442 <p class="desc">Roll back the given logical transaction, regardless of
443 whether it is an actual transaction or a savepoint.</p>
445 <p class="def">
446 <span>function</span>
447 <a name="commit-logical-transaction"></a>
448 commit-logical-transaction (transaction-or-savepoint)
449 </p>
451 <p class="desc">Commit the given logical transaction, regardless of
452 whether it is an actual transaction or a savepoint.</p>
454 <p class="def">
455 <a name="*current-logical-transaction*"></a>
456 <span>variable</span>
457 *current-logical-transaction*
458 </p>
460 <p class="desc">This is bound to the
461 current <code>transaction-handle</code>
462 or <code>savepoint-handle</code> instance representing the
463 innermost open logical transaction.</p>
466 <p class="def">
467 <span>macro</span>
468 <a name="ensure-transaction"></a>
469 ensure-transaction (&amp;body body)
470 </p>
472 <p class="desc">Ensures that <code>body</code> is executed within
473 a transaction, but does not begin a new transaction if one is
474 already in progress.</p>
476 <p class="def">
477 <span>macro</span>
478 <a name="with-schema"></a>
479 with-schema ((namespace &amp;key :strict t :if-not-exist
480 :create :drop-after) &amp;body body)
481 </p>
483 <p class="desc">Sets the current schema to <code>namespace</code> and
484 executes the <code>body</code>. Before executing <code>body</code> the
485 PostgreSQL's session variable <code>search_path</code> is set to
486 the given <code>namespace</code>. After executing <code>body</code> the
487 <code>search_path</code> variable is restored to the original value.
489 If the keyword <code>:strict</code> is set to <code>T</code> then
490 the <code>namespace</code> is only the scheme on the search path upon
491 the <code>body</code> execution. Otherwise the <code>namespace</code> is
492 just first schema on the search path upon the the <code>body</code>
493 execution. If <code>:if-not-exist</code> is <code>NIL</code>,
494 an error is signaled. If <code>:drop-after</code> is <code>T</code>
495 the <code>namespace</code> is dropped from the database after the
496 <code>body</code> execution.
497 </p>
499 <p class="def">
500 <span>function</span>
501 <a name="sequence-next"></a>
502 sequence-next (sequence)
503 <br/>&#8594; integer
504 </p>
506 <p class="desc">Get the next value from a <code>sequence</code>.
507 The sequence identifier can be either a string or a symbol, in the latter
508 case it will be converted to a string according to <a
509 href="s-sql.html">S-SQL</a> rules.</p>
511 <p class="def">
512 <span>function</span>
513 <a name="coalesce"></a>
514 coalesce (&amp;rest arguments)
515 <br/>&#8594; value
516 </p>
518 <p class="desc">Returns the first non-<code>NIL</code>, non-NULL
519 (as in <code>:null</code>) argument, or <code>NIL</code> if none
520 are present. Useful for providing a fall-back value for the result
521 of a query, or, when given only one argument, for transforming
522 <code>:null</code>s to <code>NIL</code>.</p>
524 <h2><a name="inspect"></a>Inspecting the database</h2>
526 <p class="def">
527 <span>function</span>
528 <a name="list-tables"></a>
529 list-tables (&amp;optional strings-p)
530 <br/>&#8594; list
531 </p>
533 <p class="desc">Returns a list of the tables in the current
534 database. When <code>strings-p</code> is <code>T</code>, the
535 names will be given as strings, otherwise as keywords.</p>
537 <p class="def">
538 <span>function</span>
539 <a name="table-exists-p"></a>
540 table-exists-p (name)
541 <br/>&#8594; boolean
542 </p>
544 <p class="desc">Tests whether a table with the given <code>name</code>
545 exists. The <code>name</code> can be either a string or a symbol.</p>
547 <p class="def">
548 <span>function</span>
549 <a name="table-description"></a>
550 table-description (name &amp;optional schema-name)
551 <br/>&#8594; list
552 </p>
554 <p class="desc">Returns a list of the fields in the named table.
555 Each field is represented by a list of three elements: the field
556 name, the type, and a boolean indicating whether the field may be
557 NULL. Optionally, <code>schema-name</code> can be specified to
558 restrict the result to fields from the named schema. Without it,
559 all fields in the table are returned, regardless of their schema.</p>
561 <p class="def">
562 <span>function</span>
563 <a name="list-sequences"></a>
564 list-sequences (&amp;optional strings-p)
565 <br/>&#8594; list
566 </p>
568 <p class="desc">Returns a list of the sequences in the current
569 database. When <code>strings-p</code> is <code>T</code>, the names
570 will be given as strings, otherwise as keywords.</p>
572 <p class="def">
573 <span>function</span>
574 <a name="sequence-exists-p"></a>
575 sequence-exists-p (name)
576 <br/>&#8594; boolean
577 </p>
579 <p class="desc">Tests whether a sequence with the given <code>name</code>
580 exists. The <code>name</code> can be either a string or a symbol.</p>
582 <p class="def">
583 <span>function</span>
584 <a name="list-views"></a>
585 list-views (&amp;optional strings-p)
586 <br/>&#8594; list
587 </p>
589 <p class="desc">Returns list of the user defined views in the current
590 database. When <code>strings-p</code> is <code>T</code>, the names will
591 be returned as strings, otherwise as keywords.</p>
593 <p class="def">
594 <span>function</span>
595 <a name="view-exists-p"></a>
596 view-exists-p (name)
597 <br/>&#8594; boolean
598 </p>
600 <p class="desc">Tests whether a view with the given <code>name</code>
601 exists. The <code>name</code> can be either a string or a symbol.</p>
603 <p class="def">
604 <span>function</span>
605 <a name="list-schemata"></a>
606 list-schemata ()
607 <br/>&#8594; list
608 </p>
610 <p class="desc">Returns list of the user defined schemata (as strings)
611 and the quantity of existing schemata.</p>
613 <p class="def">
614 <span>function</span>
615 <a name="schema-exist-p"></a>
616 schema-exist-p (schema)
617 <br/>&#8594; boolean
618 </p>
620 <p class="desc">Tests the existence of a given <code>schema</code>.
621 Returns <code>T</code> if the schema exists or <code>NIL</code>
622 otherwise.</p>
624 <h2><a name="daos"></a>Database access objects</h2>
626 <p>Postmodern contains a simple system for defining CLOS classes
627 that represent rows in the database. This is not intended as a
628 full-fledged object-relational magic system &#x2015; while serious
629 ORM systems have their place, they are notoriously hard to get
630 right, and are outside of the scope of a humble SQL library like
631 this.</p>
633 <p class="def">
634 <span>metaclass</span>
635 <a name="dao-class"></a>
636 dao-class
637 </p>
639 <p class="desc">At the heart of Postmodern's DAO system is the
640 <code>dao-class</code> metaclass. It allows you to define classes
641 for your database-access objects as regular CLOS classes. Some of
642 the slots in these classes will refer to columns in the database.
643 To specify that a slot refers to a column, give it a
644 <code>:col-type</code> option containing
645 an <a href="s-sql.html">S-SQL</a> type expression (useful if you
646 want to be able to derive a table definition from the class
647 definition), or simply a <code>:column</code> option with
648 value <code>T</code>. Such slots can also take
649 a <code>:col-default</code> option, used to provide a
650 database-side default value as an S-SQL expression. You can use
651 the <code>:col-name</code> initarg (whose unevaluated value will
652 be passed to <code>to-sql-name</code>) to specify the slot's column's
653 name.</p>
655 <p class="desc">DAO class definitions support two extra class
656 options: <code>:table-name</code> to give the name of the table
657 that the class refers to (defaults to the class name), and
658 <code>:keys</code> to provide a set of primary keys for the table.
659 When no primary keys are defined, operations such as <a
660 href="#update-dao"><code>update-dao</code></a> and <a
661 href="#get-dao"><code>get-dao</code></a> will not work.</p>
663 <p class="desc">Simple example:</p>
665 <pre class="code">
666 (defclass user ()
667 ((name :col-type string :initarg :name :accessor user-name)
668 (creditcard :col-type (or db-null integer) :initarg :card :col-default :null)
669 (score :col-type bigint :col-default 0 :accessor user-score))
670 (:metaclass dao-class)
671 (:keys name))</pre>
673 <p class="desc">The <code>(or db-null integer)</code> form is used
674 to indicate a column can have NULL values.</p>
676 <p class="desc">When inheriting from DAO classes, a subclass' set
677 of columns also contains all the columns of its superclasses. The
678 primary key for such a class is the union of its own keys and all
679 the keys from its superclasses. Classes inheriting from DAO
680 classes should probably always use the <code>dao-class</code>
681 metaclass themselves.</p>
683 <p class="desc">When a DAO is created with
684 <code>make-instance</code>, the <code>:fetch-defaults</code> keyword
685 argument can be passed, which, when <code>T</code>, will cause a query
686 to fetch the default values for all slots that refers to columns with
687 defaults and were not bound through initargs. In some cases, such as
688 <code>serial</code> columns, which have an implicit default, this will
689 not work. You can work around this by creating your own sequence, e.g.
690 <code>"my_sequence"</code>, and defining a
691 <code>(:nextval "my_sequence")</code> default.</p>
693 <p class="desc">Finally, DAO class slots can have an option
694 <code>:ghost t</code> to specify them as ghost slots. These are
695 selected when retrieving instances, but not written when updating
696 or inserting, or even included in the table definition. The only
697 known use for this to date is for creating the table with
698 <code>(oids=true)</code>, and specify a slot like this:</p>
700 <pre class="code">
701 (oid :col-type integer :ghost t :accessor get-oid)</pre>
703 <p class="def">
704 <span>method</span>
705 <a name="dao-keys"></a>
706 dao-keys (class)
707 <br/>&#8594; list
708 </p>
710 <p class="desc">
711 Returns list of slot names that are the primary key of DAO
712 <code>class</code>.
713 </p>
715 <p class="def">
716 <span>method</span>
717 dao-keys (dao)
718 <br/>&#8594; list
719 </p>
721 <p class="desc">
722 Returns list of values that are the primary key of <code>dao</code>.
723 </p>
725 <p class="def">
726 <span>method</span>
727 <a name="dao-exists-p"></a>
728 dao-exists-p (dao)
729 <br/>&#8594; boolean
730 </p>
732 <p class="desc">Test whether a row with the same primary key as
733 the given <code>dao</code> exists in the database. Will also return
734 <code>NIL</code> when any of the key slots in the object are
735 unbound.</p>
737 <p class="def">
738 <span>method</span>
739 <a name="make-dao"></a>
740 make-dao (type &amp;rest args &amp;key &amp;allow-other-keys)
741 <br/>&#8594; dao
742 </p>
744 <p class="desc">Combines <code>make-instance</code> with
745 <a href="#insert-dao"><code>insert-dao</code></a>. Return the
746 created dao.</p>
748 <p class="def">
749 <span>macro</span>
750 <a name="define-dao-finalization"></a>
751 define-dao-finalization (((dao-name class) &amp;rest keyword-args) &amp;body body)
752 </p>
754 <p class="desc">Create an <code>:around</code>-method for <a href="#make-dao">
755 <code>make-dao</code></a>. The <code>body</code> is executed in
756 a lexical environment where <code>dao-name</code> is bound
757 to a freshly created and inserted DAO. The representation of the DAO in the
758 database is then updated to reflect changes that <code>body</code> might
759 have introduced. Useful for processing values of slots with the type
760 <code>serial</code>, which are unknown before <a href="#insert-dao">
761 <code>insert-dao</code></a>.</p>
763 <p class="def">
764 <span>method</span>
765 <a name="get-dao"></a>
766 get-dao (type &amp;rest keys)
767 <br/>&#8594; dao
768 </p>
770 <p class="desc">Select the DAO object from the row that has the
771 given primary key values, or <code>NIL</code> if no such row
772 exists. Objects created by this function will have
773 <code>initialize-instance</code> called on them (after loading in
774 the values from the database) without any arguments &#x2015; even
775 <code>:default-initargs</code> are skipped. The same goes for <a
776 href="#select-dao"><code>select-dao</code></a> and <a
777 href="#query-dao"><code>query-dao</code></a>.</p>
779 <p class="def">
780 <span>macro</span>
781 <a name="select-dao"></a>
782 select-dao (type &amp;optional (test t) &amp;rest sort)
783 <br/>&#8594; list
784 </p>
786 <p class="desc">Select DAO objects for the rows in the associated
787 table for which the given <code>test</code> (either an <a
788 href="s-sql.html">S-SQL</a> expression or a string) holds. When
789 sorting arguments are given, which can also be S-SQL forms or
790 strings, these are used to sort the result. (Note that, if you
791 want to sort, you <em>have</em> to pass the <code>test</code>
792 argument.)</p>
794 <pre class="code">(select-dao 'user (:> 'score 10000) 'name)</pre>
796 <p class="def">
797 <span>macro</span>
798 <a name="do-select-dao"></a>
799 do-select-dao (((type type-var) &amp;optional (test t) &amp;rest sort) &amp;body body)
800 </p>
802 <p class="desc">Like <a href="#select-dao"><code>select-dao</code></a>,
803 but iterates over the results rather than returning them. For each matching
804 DAO, <code>body</code> is evaluated with <code>type-var</code> bound to the
805 DAO instance.</p>
807 <pre class="code">(do-select-dao (('user user) (:> 'score 10000) 'name)
808 (pushnew user high-scorers))</pre>
810 <p class="def">
811 <span>function</span>
812 <a name="query-dao"></a>
813 query-dao (type query &amp;rest args)
814 <br/>&#8594; list
815 </p>
817 <p class="desc">Execute the given <code>query</code> (which can be either
818 a string or an <a href="s-sql.html">S-SQL</a> expression) and return
819 the result as DAOs of the given <code>type</code>. If the <code>query</code>
820 contains placeholders ($1, $2, etc) their values can be given as extra
821 arguments. The names of the fields returned by the <code>query</code> must
822 either match slots in the DAO class, or be bound through <a
823 href="#with-column-writers"><code>with-column-writers</code></a>.</p>
825 <p class="def">
826 <span>function</span>
827 <a name="do-query-dao"></a>
828 do-query-dao (((type type-var) query &amp;rest args) &body body)
829 <br/>&#8594; list
830 </p>
832 <p class="desc">Like <a href="#query-dao"><code>query-dao</code></a>, but
833 iterates over the results rather than returning them. For each matching DAO,
834 <code>body</code> is evaluated with <code>type-var</code> bound to the
835 instance.</p>
837 <pre class="code">(do-query-dao (('user user) (:order-by (:select '* :from 'user :where (:> 'score 10000)) 'name))
838 (pushnew user high-scorers))</pre>
840 <p class="def">
841 <span>variable</span>
842 <a name="*ignore-unknown-columns*"></a>
843 *ignore-unknown-columns*
844 </p>
846 <p class="desc">Normally,
847 when <code><a href="#get-dao">get-dao</a></code>,
848 <code><a href="#select-dao">select-dao</a></code>,
849 or <code><a href="#query-dao">query-dao</a></code> finds a column
850 in the database that's not in the DAO class, it will raise an
851 error. Setting this variable to a non-<code>NIL</code> will cause it to
852 simply ignore the unknown column.</p>
854 <p class="def">
855 <span>method</span>
856 <a name="insert-dao"></a>
857 insert-dao (dao)
858 <br/>&#8594; dao
859 </p>
861 <p class="desc">Insert the given <code>dao</code> into the database.
862 Column slots of the object which are unbound implies the database defaults.
863 Hence, if these columns has no defaults defined in the database, the
864 the insertion of the <code>dao</code> will be failed.
865 (This feature only works on PostgreSQL 8.2 and up.)</p>
867 <p class="def">
868 <span>method</span>
869 <a name="update-dao"></a>
870 update-dao (dao)
871 <br/>&#8594; dao
872 </p>
874 <p class="desc">Update the representation of the given <code>dao</code>
875 in the database to the values in the object. This is not defined for
876 tables that do not have any non-primary-key columns. Raises an
877 error when no row matching the <code>dao</code> exists.</p>
879 <p class="def">
880 <span>function</span>
881 <a name="save-dao"></a>
882 save-dao (dao)
883 <br/>&#8594; boolean
884 </p>
886 <p class="desc">Tries to insert the given <code>dao</code> using <a
887 href="#insert-dao"><code>insert-dao</code></a>. If this raises a
888 unique key violation error, it tries to update it by using <a
889 href="#update-dao"><code>update-dao</code></a> instead. Be aware
890 that there is a possible race condition here &#x2015; if some
891 other process deletes the row at just the right moment, the update
892 fails as well. Returns a boolean telling you whether a new row was
893 inserted.</p>
895 <p class="desc">This function is unsafe to use inside of a
896 transaction &#x2015; when a row with the given keys already
897 exists, the transaction will be aborted. Use <a
898 href="#save-dao/transaction"><code>save-dao/transaction</code></a>
899 instead in such a situation.</p>
901 <p class="desc"><b>See also:</b> <a href="#upsert-dao">
902 <code>upsert-dao</code></a>.</p>
904 <p class="def">
905 <span>function</span>
906 <a name="save-dao/transaction"></a>
907 save-dao/transaction (dao)
908 <br/>&#8594; boolean
909 </p>
911 <p class="desc">Acts exactly like <a
912 href="#save-dao"><code>save-dao</code></a>, except that it
913 protects its attempt to insert the object with a rollback point,
914 so that a failure will not abort the transaction.</p>
916 <p class="desc"><b>See also:</b> <a href="#upsert-dao">
917 <code>upsert-dao</code></a>.</p>
919 <p class="def">
920 <span>method</span>
921 <a name="upsert-dao"></a>
922 upsert-dao (dao)
923 <br/>&#8594; dao
924 </p>
926 <p class="desc">
927 Like <a href="#save-dao"><code>save-dao</code></a>
928 or <a href="#save-dao/transaction"><code>save-dao/transaction</code></a>
929 but using a different method that doesn't involve a database
930 exception. This is safe to use both in and outside a transaction,
931 though it's advisable to always do it in a transaction to prevent a
932 race condition. The way it works is:
933 </p>
935 <ol class="desc">
936 <li>If the object contains unbound slots, we
937 call <a href="#insert-dao"><code>insert-dao</code></a> directly, thus
938 the behavior is like <code>save-dao</code>.</li>
939 <li>Otherwise we try to update a record with the same primary key. If
940 the PostgreSQL returns a non-zero number of rows updated it
941 <em>treated</em> as the record is already exists in the database, and
942 we stop here.</li>
943 <li>If the PostgreSQL returns a zero number of rows updated, it
944 <em>treated</em> as the record does not exist and we call
945 <code>insert-dao</code>.</li>
946 </ol>
948 <p class="desc">
949 The race condition might occur at step 3 <em>if there's no
950 transaction:</em> if UPDATE returns zero number of rows updated and
951 another thread inserts the record at that moment, the insertion
952 implied by step 3 will fail.
953 </p>
955 <p class="desc">
956 Note, that triggers and rules may affect the number of inserted or
957 updated rows returned by PostgreSQL, so zero or non-zero number of
958 affected rows may not <em>actually</em> indicate the existence of
959 record in the database.
960 </p>
962 <p class="desc">
963 This method returns two values: the DAO object and a boolean
964 (<code>T</code> if the object was inserted, <code>NIL</code> if
965 it was updated).
966 </p>
968 <p class="def">
969 <span>method</span>
970 <a name="delete-dao"></a>
971 delete-dao (dao)
972 </p>
974 <p class="desc">Delete the given <code>dao</code> from the database.</p>
976 <p class="def">
977 <span>function</span>
978 <a name="dao-table-name"></a>
979 dao-table-name (class)
980 <br/>&#8594; string
981 </p>
983 <p class="desc">Get the name of the <code>table</code> associated with
984 the given DAO <code>class</code> (or symbol naming such a class).</p>
986 <p class="def">
987 <span>function</span>
988 <a name="dao-table-definition"></a>
989 dao-table-definition (class)
990 <br/>&#8594; string
991 </p>
993 <p class="desc">Given a DAO <code>class</code>, or the name of one,
994 this will produce an SQL query string with a definition of the table.
995 This is just the bare simple definition, so if you need any extra
996 indices or or constraints, you'll have to write your own queries
997 to add them.</p>
999 <p class="def">
1000 <span>macro</span>
1001 <a name="with-column-writers"></a>
1002 with-column-writers ((&amp;rest writers) &amp;body body)
1003 </p>
1005 <p class="desc">Provides control over the way <code><a
1006 href="#get-dao">get-dao</a></code>, <code><a
1007 href="#select-dao">select-dao</a></code>, and <code><a
1008 href="#query-dao">query-dao</a></code> read values from the
1009 database. This is not commonly needed, but can be used to reduce
1010 the amount of queries a system makes. <code>writers</code> should
1011 be a list of alternating column names (strings or symbols) and
1012 writers, where writers are either symbols referring to a slot in
1013 the objects, or functions taking two arguments &#x2015; an
1014 instance and a value &#x2015; which can be used to somehow store
1015 the value in the new instance. When any DAO-fetching function is
1016 called in the <code>body</code>, and columns matching the given
1017 names are encountered in the result, the writers are used instead
1018 of the default behaviour (try and store the value in the slot that
1019 matches the column name).</p>
1021 <p class="desc">An example of using this is to add some non-column
1022 slots to a DAO class, and use <code><a
1023 href="#query-dao">query-dao</a></code> within a
1024 <code>with-column-writers</code> form to pull in extra information
1025 about the objects, and immediately store it in the new
1026 instances.</p>
1028 <h2 id="tabledef">Table definition and creation</h2>
1030 <p>It can be useful to have the SQL statements needed to build an
1031 application's tables available from the source code, to do things like
1032 automatically deploying a database. The following macro and
1033 functions allow you to group sets of SQL statements under symbols,
1034 with some shortcuts for common elements in table definitions.</p>
1036 <p class="def" id="deftable">
1037 <span>macro</span>
1038 deftable (name &amp;body definition)
1039 </p>
1041 <p class="desc">Define a table. <code>name</code> can be either a symbol
1042 or a <code>(symbol string)</code> list. In the first case, the table
1043 name is derived from the symbol's name by <a
1044 href="s-sql.html">S-SQL</a>'s rules. In the second case, the
1045 <code>name</code> is given explicitly. The body of definitions can contain
1046 anything that evaluates to a string, as well as S-SQL expressions. The
1047 variables <a href="#*table-name*"><code>*table-name*</code></a> and
1048 <a href="#*table-name*"><code>*table-symbol*</code></a> are bound to
1049 the relevant values in the body. Note that the evaluation of the
1050 <code>definition</code> is ordered, so you'll generally want to create your
1051 table first and then define indices on it.</p>
1053 <p class="def">
1054 <a name="!dao-def"></a>
1055 <span>function</span>
1056 !dao-def ()
1057 </p>
1059 <p class="desc">Should only be used inside <a
1060 href="#deftable"><code>deftable</code></a>'s body. Adds the result
1061 of calling <a
1062 href="#dao-table-definition"><code>dao-table-definition</code></a>
1063 on <a href="#*table-symbol*"><code>*table-symbol*</code></a> to
1064 the <code>definition</code>.</p>
1066 <p class="def">
1067 <a name="!index"></a>
1068 <span>function</span>
1069 !index (&amp;rest columns), !unique-index (&amp;rest columns)
1070 </p>
1072 <p class="desc">Define an index on the table being defined. The
1073 <code>columns</code> can be given as symbols or strings.</p>
1075 <p class="def">
1076 <a name="!foreign"></a>
1077 <span>function</span>
1078 !foreign (target-table columns &amp;optional target-columns &amp;key on-delete on-update deferrable initially-deferred)
1079 </p>
1081 <p class="desc">Add a foreign key to the table being defined.
1082 <code>target-table</code> is the referenced table.
1083 <code>columns</code> is a list of column names or single name in
1084 <em>this</em> table, and, if the columns have different names in
1085 the referenced table, <code>target-columns</code> must be
1086 another list of column names or single column name of the
1087 <code>target-table</code>, or <code>:primary-key</code> to denote
1088 the column(s) of the <code>target-table</code>'s primary key
1089 as referenced column(s).
1090 </p>
1092 <p class="desc">The <code>on-delete</code> and
1093 <code>on-update</code> arguments can be used to specify ON DELETE
1094 and ON UPDATE actions, as per the keywords allowed in <a
1095 href="s-sql.html#create-table"><code>create-table</code></a>. In
1096 addition, the <code>deferrable</code> and <code>initially-deferred</code>
1097 arguments can be used to indicate whether constraint checking can be
1098 deferred until the current transaction completed, and whether this should
1099 be done by default. Note that none of these are really &amp;key
1100 arguments, but rather are picked out of a &amp;rest arg at
1101 runtime, so that they can be specified even when
1102 <code>target-columns</code> is not given.</p>
1104 <p class="def">
1105 <a name="!unique"></a>
1106 <span>function</span>
1107 !unique (target-fields &amp;key deferrable initially-deferred)
1108 </p>
1110 <p class="desc">Constrains one or more columns to only contain
1111 unique (combinations of) values, with <code>deferrable</code> and
1112 <code>initially-deferred</code> defined as in <a
1113 href="#!foreign"><code>!foreign</code></a></p>
1115 <p class="def" id="create-table">
1116 <span>function</span>
1117 create-table (symbol)
1118 </p>
1120 <p class="desc">Creates the table identified by
1121 <code>symbol</code> by <a href="#execute">executing</a> all forms in
1122 its definition.</p>
1124 <p class="def" id="create-all-tables">
1125 <span>function</span>
1126 create-all-tables ()
1127 </p>
1129 <p class="desc">Creates all defined tables.</p>
1131 <p class="def" id="create-package-tables">
1132 <span>function</span>
1133 create-package-tables (package)
1134 </p>
1136 <p class="desc">Creates all tables identified by symbols
1137 interned in the given <code>package</code>.</p>
1139 <p class="def">
1140 <a name="*table-name*"></a>
1141 <span>variables</span>
1142 *table-name*, *table-symbol*
1143 </p>
1145 <p class="desc">These variables are bound to the relevant name and
1146 symbol while the forms of a table definition are evaluated. Can be
1147 used to define shorthands like the ones below.</p>
1149 <h2 id="schemata">Schemata</h2>
1151 Schema allow you to separate tables into differnet name spaces. In
1152 different schemata two tables with the same name are allowed to
1153 exists. The tables can be referred by fully qualified names or
1154 with the macro <a href="#with-schema">with-schema</a>. You could
1155 also set the search path
1156 with <a href="#set-search-path">set-search-path<a/>. For listing
1157 end checking there are also the
1158 functions <a href="#list-schemata">list-schemata</a>
1159 and <a href="#schema-exist-p">schema-exist-p</a>. The following
1160 functions allow you to create, drop schemata and to set the search
1161 path.
1163 <p class="def">
1164 <span>function</span>
1165 <a name="create-schema"></a>
1166 create-schema (schema)
1167 </p>
1169 <p class="desc">
1170 Creates a new schema. Raises an error if the schema is already exists.
1171 </p>
1173 <p class="def">
1174 <span>function</span>
1175 <a name="drop-schema"></a>
1176 drop-schema (schema)
1177 </p>
1179 <p class="desc">
1180 Removes a schema. Raises an error if the schema is not empty.
1181 </p>
1183 <p class="def">
1184 <span>function</span>
1185 <a name="get-search-path"></a>
1186 get-search-path ()
1187 </p>
1189 <p class="desc">
1190 Retrieve the current search path.
1191 </p>
1193 <p class="def">
1194 <span>function</span>
1195 <a name="set-search-path"></a>
1196 set-search-path (path)
1197 </p>
1199 <p class="desc">
1200 Sets the search path to the <code>path</code>. This function is used
1201 by <a href="#with-schema">with-schema</a>.
1202 </p>
1204 <h2 id="index">Symbol-index</h2>
1206 <ul class="symbol-index">
1207 <li><a href="#abort-transaction">abort-transaction</a></li>
1208 <li><a href="#deftable">deftable</a></li>
1209 <li><a href="#call-with-connection">call-with-connection</a></li>
1210 <li><a href="#clear-connection-pool">clear-connection-pool</a></li>
1211 <li><a href="#clear-template">clear-template</a></li>
1212 <li><a href="#commit-transaction">commit-transaction</a></li>
1213 <li><a href="#connect">connect</a></li>
1214 <li><a href="#connect-toplevel">connect-toplevel</a></li>
1215 <li><a href="#connected-p">connected-p</a></li>
1216 <li><a href="#create-all-tables">create-all-tables</a></li>
1217 <li><a href="#create-package-tables">create-package-tables</a></li>
1218 <li><a href="#create-schema">create-schema</a></li>
1219 <li><a href="#create-table">create-table</a></li>
1220 <li><a href="#dao-class">dao-class</a></li>
1221 <li><a href="#!dao-def">!dao-def</a></li>
1222 <li><a href="#dao-exists-p">dao-exists-p</a></li>
1223 <li><a href="#dao-keys">dao-keys</a></li>
1224 <li><a href="#dao-table-definition">dao-table-definition</a></li>
1225 <li><a href="#dao-table-name">dao-table-name</a></li>
1226 <li><a href="#*database*">*database*</a></li>
1227 <li><a href="#database-connection">database-connection</a></li>
1228 <li><a href="#define-dao-finalization">define-dao-finalization</a></li>
1229 <li><a href="cl-postgres.html#database-connection-lost">database-connection-lost</a></li>
1230 <li><a href="cl-postgres.html#database-error">database-error</a></li>
1231 <li><a href="cl-postgres.html#database-error-cause">database-error-cause</a></li>
1232 <li><a href="cl-postgres.html#database-error-code">database-error-code</a></li>
1233 <li><a href="cl-postgres.html#database-error-detail">database-error-detail</a></li>
1234 <li><a href="cl-postgres.html#database-error-query">database-error-query</a></li>
1235 <li><a href="cl-postgres.html#database-error-message">database-error-message</a></li>
1236 <li><a href="#*default-use-ssl*">*default-use-ssl*</a></li>
1237 <li><a href="#defprepared">defprepared</a></li>
1238 <li><a href="#defprepared">defprepared-with-names</a></li>
1239 <li><a href="#delete-dao">delete-dao</a></li>
1240 <li><a href="#disconnect">disconnect</a></li>
1241 <li><a href="#disconnect-toplevel">disconnect-toplevel</a></li>
1242 <li><a href="#doquery">doquery</a></li>
1243 <li><a href="#drop-schema">drop-schema</a></li>
1244 <li><a href="#execute">execute</a></li>
1245 <li><a href="#!foreign">!foreign</a></li>
1246 <li><a href="#!unique">!unique</a></li>
1247 <li><a href="#get-dao">get-dao</a></li>
1248 <li><a href="#get-search-path">get-search-path</a></li>
1249 <li><a href="#*ignore-unknown-columns*">*ignore-unknown-columns*</a></li>
1250 <li><a href="#!index">!index</a></li>
1251 <li><a href="#insert-dao">insert-dao</a></li>
1252 <li><a href="#list-sequences">list-sequences</a></li>
1253 <li><a href="#list-schemata">list-schemata</a></li>
1254 <li><a href="#list-tables">list-tables</a></li>
1255 <li><a href="#list-views">list-views</a></li>
1256 <li><a href="#make-dao">make-dao</a></li>
1257 <li><a href="#*max-pool-size*">*max-pool-size*</a></li>
1258 <li><a href="#prepare">prepare</a></li>
1259 <li><a href="#query">query</a></li>
1260 <li><a href="#query-dao">query-dao</a></li>
1261 <li><a href="#reconnect">reconnect</a></li>
1262 <li><a href="#release-savepoint">release-savepoint</a></li>
1263 <li><a href="#reset-table">reset-table</a></li>
1264 <li><a href="#rollback-savepoint">rollback-savepoint</a></li>
1265 <li><a href="#save-dao">save-dao</a></li>
1266 <li><a href="#save-dao/transaction">save-dao/transaction</a></li>
1267 <li><a href="#select-dao">select-dao</a></li>
1268 <li><a href="#set-search-path">set-search-path</a></li>
1269 <li><a href="#schema-exist-p">schema-exist-p</a></li>
1270 <li><a href="#sequence-exists-p">sequence-exists-p</a></li>
1271 <li><a href="#sequence-next">sequence-next</a></li>
1272 <li><a href="#table-description">table-description</a></li>
1273 <li><a href="#table-exists-p">table-exists-p</a></li>
1274 <li><a href="#*table-name*">*table-name*</a></li>
1275 <li><a href="#*table-name*">*table-symbol*</a></li>
1276 <li><a href="#!index">!unique-index</a></li>
1277 <li><a href="#update-dao">update-dao</a></li>
1278 <li><a href="#view-exists-p">view-exists-p</a></li>
1279 <li><a href="#with-column-writers">with-column-writers</a></li>
1280 <li><a href="#with-connection">with-connection</a></li>
1281 <li><a href="#with-savepoint">with-savepoint</a></li>
1282 <li><a href="#with-schema">with-schema</a></li>
1283 <li><a href="#with-transaction">with-transaction</a></li>
1284 </ul>
1286 </body>
1287 </html>