More code that should have been in the last patch.
[clsql.git] / sql / db-interface.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          db-interface.lisp
6 ;;;; Purpose:       Generic function definitions for DB interfaces
7 ;;;; Programmers:   Kevin M. Rosenberg based on
8 ;;;;                Original code by Pierre R. Mai. Additions from
9 ;;;;                onShoreD to support UncommonSQL front-end
10 ;;;; Date Started:  Feb 2002
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
13 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai, and onShoreD
14 ;;;;
15 ;;;; CLSQL users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
19
20 (in-package #:clsql-sys)
21
22 (defgeneric database-type-load-foreign (database-type)
23   (:documentation
24    "The internal generic implementation of reload-database-types."))
25
26 (defgeneric database-type-library-loaded (database-type)
27   (:documentation
28    "The internal generic implementation for checking if
29 database type library loaded successfully."))
30
31 (defgeneric database-initialize-database-type (database-type)
32   (:documentation
33    "The internal generic implementation of initialize-database-type."))
34
35 (defgeneric database-name-from-spec (connection-spec database-type)
36   (:documentation
37    "Returns the name of the database that would be created if connect
38 was called with the connection-spec."))
39
40 (defgeneric database-connect (connection-spec database-type)
41   (:documentation "Internal generic implementation of connect."))
42
43 (defgeneric database-reconnect (database)
44   (:method ((database t))
45            (signal-no-database-error database))
46   (:documentation "Internal generic implementation of reconnect."))
47
48 (defgeneric database-disconnect (database)
49   (:method ((database t))
50            (signal-no-database-error database))
51   (:documentation "Internal generic implementation of disconnect."))
52
53 (defgeneric database-query (query-expression database result-types field-names)
54   (:method (query-expression (database t) result-types field-names)
55            (declare (ignore query-expression result-types field-names))
56            (signal-no-database-error database))
57   (:method (query-expression (database database) result-types field-names)
58              (declare (ignore query-expression result-types field-names))
59              (warn "database-query not implemented for database type ~A."
60                    (database-type database)))
61   (:documentation "Internal generic implementation of query."))
62
63
64 (defgeneric database-execute-command (sql-expression database)
65   (:method (sql-expression (database t))
66            (declare (ignore sql-expression))
67            (signal-no-database-error database))
68   (:method (sql-expression (database database))
69            (declare (ignore sql-expression))
70            (warn "database-execute-command not implemented for database type ~A."
71                  (database-type database)))
72   (:documentation "Internal generic implementation of execute-command."))
73
74 ;;; Mapping and iteration
75 (defgeneric database-query-result-set
76     (query-expression database &key full-set result-types)
77   (:method (query-expression (database t) &key full-set result-types)
78            (declare (ignore query-expression full-set result-types))
79            (signal-no-database-error database)
80            (values nil nil nil))
81   (:method (query-expression (database database) &key full-set result-types)
82            (declare (ignore query-expression full-set result-types))
83            (warn "database-query-result-set not implemented for database type ~A."
84                  (database-type database))
85            (values nil nil nil))
86   (:documentation
87    "Internal generic implementation of query mapping.  Starts the
88 query specified by query-expression on the given database and returns
89 a result-set to be used with database-store-next-row and
90 database-dump-result-set to access the returned data.  The second
91 value is the number of columns in the result-set, if there are any.
92 If full-set is true, the number of rows in the result-set is returned
93 as a third value, if this is possible (otherwise nil is returned for
94 the third value).  This might have memory and resource usage
95 implications, since many databases will require the query to be
96 executed in full to answer this question.  If the query produced no
97 results then nil is returned for all values that would have been
98 returned otherwise.  If an error occurs during query execution, the
99 function should signal a sql-database-data-error."))
100
101 (defgeneric database-dump-result-set (result-set database)
102   (:method (result-set (database t))
103            (declare (ignore result-set))
104            (signal-no-database-error database))
105     (:method (result-set (database database))
106            (declare (ignore result-set))
107            (warn "database-dump-result-set not implemented for database type ~A."
108                  (database-type database)))
109   (:documentation "Dumps the received result-set."))
110
111 (defgeneric database-store-next-row (result-set database list)
112   (:method (result-set (database t) list)
113            (declare (ignore result-set list))
114            (signal-no-database-error database))
115     (:method (result-set (database database) list)
116            (declare (ignore result-set list))
117            (warn "database-store-next-row not implemented for database type ~A."
118                  (database-type database)))
119   (:documentation
120    "Returns t and stores the next row in the result set in list or
121 returns nil when result-set is finished."))
122
123 (defgeneric database-create (connection-spec type)
124   (:documentation
125    "Creates a database, returns T if successfull or signals an error."))
126
127 (defgeneric database-probe (connection-spec type)
128   (:method (spec type)
129     (declare (ignore spec))
130     (warn "database-probe not support for database-type ~A." type))
131   (:documentation
132    "Probes for the existence of a database, returns T if database found or NIL
133 if not found. May signal an error if unable to communicate with database server."))
134
135 (defgeneric database-list (connection-spec type)
136   (:method (spec type)
137     (declare (ignore spec))
138     (warn "database-list not support for database-type ~A." type))
139   (:documentation
140    "Lists all databases found for TYPE. May signal an error if unable to communicate with database server."))
141
142 (defgeneric database-truncate (database)
143   (:method ((database t))
144     (signal-no-database-error database))
145   (:documentation "Remove all data from database."))
146
147 (defgeneric database-destroy (connection-spec type)
148   (:documentation
149    "Destroys (drops) a database, returns T if successfull or signals an error
150 if unable to destory."))
151
152 (defgeneric database-create-sequence (name database)
153   (:documentation "Create a sequence in DATABASE."))
154
155 (defgeneric database-drop-sequence (name database)
156   (:documentation "Drop a sequence from DATABASE."))
157
158 (defgeneric database-sequence-next (name database)
159   (:documentation "Increment a sequence in DATABASE."))
160
161 (defgeneric database-list-sequences (database &key owner)
162   (:documentation "List all sequences in DATABASE."))
163
164 (defgeneric database-set-sequence-position (name position database)
165   (:documentation "Set the position of the sequence called NAME in DATABASE."))
166
167 (defgeneric database-sequence-last (name database)
168   (:documentation "Select the last value in sequence NAME in DATABASE."))
169
170 (defgeneric database-last-auto-increment-id (database table column)
171   (:documentation "Many databases have the notion of an auto-increment
172   id; i.e. a sequence implicitly on a table. This function should
173   return that ID." ))
174
175 (defgeneric database-start-transaction (database)
176   (:documentation "Start a transaction in DATABASE.")
177   (:method ((database t))
178            (signal-no-database-error database)))
179
180 (defgeneric database-commit-transaction (database)
181   (:documentation "Commit current transaction in DATABASE.")
182   (:method ((database t))
183            (signal-no-database-error database)))
184
185 (defgeneric database-abort-transaction (database)
186   (:documentation "Abort current transaction in DATABASE.")
187   (:method ((database t))
188            (signal-no-database-error database)))
189
190 (defgeneric database-get-type-specifier (type args database db-underlying-type)
191   (:documentation "Return the type SQL type specifier as a string, for
192 the given lisp type and parameters."))
193
194 (defgeneric database-list-tables (database &key owner)
195   (:documentation "List all tables in the given database")
196   (:method ((database database) &key owner)
197            (declare (ignore owner))
198            (warn "database-list-tables not implemented for database type ~A."
199                  (database-type database)))
200   (:method ((database t) &key owner)
201            (declare (ignore owner))
202            (signal-no-database-error database)))
203
204 (defgeneric database-list-tables-and-sequences (database &key owner)
205   (:documentation "List all tables in the given database, may include seqeneces")
206   (:method ((database t) &key owner)
207            (declare (ignore owner))
208            (signal-no-database-error database))
209   (:method ((database database) &key owner)
210            (database-list-tables database :owner owner)))
211
212 (defgeneric database-list-views (database &key owner)
213   (:documentation "List all views in the DATABASE.")
214   (:method ((database database) &key owner)
215            (declare (ignore owner))
216            (warn "database-list-views not implemented for database type ~A."
217                  (database-type database)))
218   (:method ((database t) &key owner)
219            (declare (ignore owner))
220            (signal-no-database-error database)))
221
222 (defgeneric database-list-indexes (database &key owner)
223   (:documentation "List all indexes in the DATABASE.")
224   (:method ((database database) &key owner)
225            (declare (ignore owner))
226            (warn "database-list-indexes not implemented for database type ~A."
227                  (database-type database)))
228   (:method ((database t) &key owner)
229     (declare (ignore owner))
230     (signal-no-database-error database)))
231
232 (defgeneric database-list-table-indexes (table database &key owner)
233   (:documentation "List all indexes for a table in the DATABASE.")
234   (:method (table (database database) &key owner)
235            (declare (ignore table owner))
236            (warn "database-list-table-indexes not implemented for database type ~A."
237                  (database-type database)))
238   (:method (table (database t) &key owner)
239            (declare (ignore table owner))
240            (signal-no-database-error database)))
241
242 (defgeneric database-list-attributes (table database &key owner)
243   (:documentation "List all attributes in TABLE.")
244   (:method (table (database database) &key owner)
245            (declare (ignore table owner))
246            (warn "database-list-attributes not implemented for database type ~A."
247                  (database-type database)))
248   (:method (table (database t) &key owner)
249            (declare (ignore table owner))
250            (signal-no-database-error database)))
251
252 (defgeneric database-attribute-type (attribute table database &key owner)
253   (:documentation "Return the type of ATTRIBUTE in TABLE. Returns multiple values
254 of TYPE_NAME (keyword) PRECISION SCALE NULLABLE.")
255   (:method (attribute table (database database) &key owner)
256            (declare (ignore attribute table owner))
257            (warn "database-list-attribute-type not implemented for database type ~A."
258                  (database-type database)))
259   (:method (attribute table (database t) &key owner)
260            (declare (ignore attribute table owner))
261            (signal-no-database-error database)))
262
263 (defgeneric database-add-attribute (table attribute database)
264   (:documentation "Add the attribute to the table.")
265   (:method (table attribute (database database))
266            (declare (ignore table attribute))
267            (warn "database-add-attribute not implemented for database type ~A."
268                  (database-type database)))
269   (:method (table attribute (database t))
270            (declare (ignore table attribute))
271            (signal-no-database-error database)))
272
273 (defgeneric database-rename-attribute (table oldatt newname database)
274   (:documentation "Rename the attribute in the table to NEWNAME.")
275   (:method (table oldatt newname (database database))
276            (declare (ignore table oldatt newname))
277            (warn "database-rename-attribute not implemented for database type ~A."
278                  (database-type database)))
279   (:method (table oldatt newname (database t))
280            (declare (ignore table oldatt newname))
281            (signal-no-database-error database)))
282
283 (defgeneric oid (object)
284   (:documentation "Return the unique ID of a database object."))
285
286 ;;; Database backend capabilities
287
288 (defgeneric database-underlying-type (database)
289   (:method (database)
290     (database-type database))
291   (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
292
293 (defgeneric db-type-use-column-on-drop-index? (db-type)
294   (:method (db-type)
295            (declare (ignore db-type))
296            nil)
297   (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
298
299 (defgeneric db-type-use-fully-qualified-column-on-drop-index? (db-type)
300   (:method (db-type)
301            (declare (ignore db-type))
302            nil)
303   (:documentation "NIL [default] if database-type does not require fully qualified column name on DROP INDEX."))
304
305 (defgeneric db-type-has-views? (db-type)
306   (:method (db-type)
307            (declare (ignore db-type))
308            ;; SQL92 has views
309            t)
310   (:documentation "T [default] if database-type supports views."))
311
312 (defgeneric db-type-has-bigint? (db-type)
313   (:method (db-type)
314            (declare (ignore db-type))
315            ;; SQL92 has bigint
316            t)
317   (:documentation "T [default] if database-type supports bigint."))
318
319 (defgeneric db-type-default-case (db-type)
320   (:method (db-type)
321            (declare (ignore db-type))
322            ;; By default, CommonSQL converts identifiers to UPPER case.
323            :upper)
324   (:documentation ":upper [default] if means identifiers mapped to UPPER case SQL like CommonSQL API. However, Postgresql maps identifiers to lower case, so PostgreSQL uses a value of :lower for this result."))
325
326 (defgeneric db-type-has-fancy-math? (db-type)
327   (:method (db-type)
328            (declare (ignore db-type))
329            nil)
330   (:documentation "NIL [default] if database-type does not have fancy math."))
331
332 (defgeneric db-type-has-subqueries? (db-type)
333   (:method (db-type)
334            (declare (ignore db-type))
335            t)
336   (:documentation "T [default] if database-type supports views."))
337
338 (defgeneric db-type-has-boolean-where? (db-type)
339   (:method (db-type)
340            (declare (ignore db-type))
341            ;; SQL99 has boolean where
342            t)
343   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
344
345 (defgeneric db-type-has-union? (db-type)
346   (:method (db-type)
347            (declare (ignore db-type))
348            t)
349   (:documentation "T [default] if database-type supports boolean UNION."))
350
351 (defgeneric db-backend-has-create/destroy-db? (db-type)
352   (:method (db-type)
353            (declare (ignore db-type))
354            t)
355   (:documentation "T [default] if backend can destroy and create databases."))
356
357 (defgeneric db-type-transaction-capable? (db database)
358   (:method (db database)
359            (declare (ignore db database))
360            t)
361   (:documentation "T [default] if database can supports transactions."))
362
363 (defgeneric db-type-has-prepared-stmt? (db-type)
364   (:method ((db-type t))
365     nil)
366   (:documentation "T if database backend supports prepared statements."))
367
368 (defgeneric db-type-has-intersect? (db-type)
369   (:method (db-type)
370            (declare (ignore db-type))
371            t)
372   (:documentation "T [default] if database-type supports INTERSECT."))
373
374 (defgeneric db-type-has-except? (db-type)
375   (:method (db-type)
376            (declare (ignore db-type))
377            t)
378   (:documentation "T [default] if database-type supports EXCEPT."))
379
380 (defgeneric db-type-has-auto-increment? (db-type)
381   (:method (db-type)
382     (declare (ignore db-type)
383              nil))
384   (:documentation "NIL [default] if database-type supports auto-incrementing columns."))
385
386 ;;; Large objects support (Marc Battyani)
387
388 (defgeneric database-create-large-object (database)
389   (:documentation "Creates a new large object in the database and returns the object identifier"))
390
391 (defgeneric database-write-large-object (object-id data database)
392   (:documentation "Writes data to the large object"))
393
394 (defgeneric database-read-large-object (object-id database)
395   (:documentation "Reads the large object content"))
396
397 (defgeneric database-delete-large-object (object-id database)
398   (:documentation "Deletes the large object in the database"))
399
400 ;; Prepared statements
401
402 (defgeneric database-prepare (stmt types database result-types field-names)
403   (:method (stmt types (database t) result-types field-names)
404     (declare (ignore stmt types result-types field-names))
405     (signal-no-database-error database))
406   (:method (stmt types (database database) result-types field-names)
407     (declare (ignore stmt types result-types field-names))
408     (error 'sql-database-error
409            :message
410            (format nil "DATABASE-PREPARE not implemented for ~S" database)))
411   (:documentation "Prepare a statement for later execution."))
412
413 (defgeneric database-bind-parameter (prepared-stmt position value)
414   (:method ((pstmt t) position value)
415     (declare (ignore position value))
416     (error 'sql-database-error
417            :message
418            (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
419   (:documentation "Bind a parameter for a prepared statement."))
420
421 (defgeneric database-run-prepared (prepared-stmt)
422   (:method ((pstmt t))
423     (error 'sql-database-error
424            :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
425   (:documentation "Execute a prepared statement."))
426
427 (defgeneric database-free-prepared (prepared-stmt)
428   (:method ((pstmt t))
429     ;; nothing to do by default
430     nil)
431   (:documentation "Free the resources of a prepared statement."))
432
433 (defgeneric database-acquire-from-conn-pool (database)
434   (:documentation "Acquire a database connection from the pool.  This
435 is a chance to test the connection for validity before returning it to
436 the user. If this function returns NIL or throws an error that
437 database connection is considered bad and we make a new one.
438
439 Database objects have a chance to specialize, otherwise the default
440 method uses the database-underlying-type and tries to do something
441 appropriate."))
442
443 (defgeneric database-release-to-conn-pool (database)
444   (:documentation "Chance for the database to cleanup before it is
445   returned to the connection pool."))
446
447 ;; Checks for closed database
448
449 (defmethod database-disconnect :before ((database database))
450   (unless (is-database-open database)
451     (signal-closed-database-error database)))
452
453 (defmethod database-query :before (query-expression (database database)
454                                    result-set field-names)
455   (declare (ignore query-expression result-set field-names))
456   (unless (is-database-open database)
457     (signal-closed-database-error database)))
458
459 (defmethod database-execute-command :before (sql-expression (database database))
460   (declare (ignore sql-expression))
461   (unless (is-database-open database)
462     (signal-closed-database-error database)))
463
464 (defmethod database-query-result-set :before (expr (database database)
465                                             &key full-set result-types)
466   (declare (ignore expr full-set result-types))
467   (unless (is-database-open database)
468     (signal-closed-database-error database)))
469
470 (defmethod database-dump-result-set :before (result-set (database database))
471   (declare (ignore result-set))
472   (unless (is-database-open database)
473     (signal-closed-database-error database)))
474
475 (defmethod database-store-next-row :before (result-set (database database) list)
476   (declare (ignore result-set list))
477   (unless (is-database-open database)
478     (signal-closed-database-error database)))
479
480 (defmethod database-commit-transaction :before ((database database))
481   (unless (is-database-open database)
482     (signal-closed-database-error database)))
483
484 (defmethod database-start-transaction :before ((database database))
485   (unless (is-database-open database)
486     (signal-closed-database-error database)))
487
488 (defmethod database-abort-transaction :before ((database database))
489   (unless (is-database-open database)
490     (signal-closed-database-error database)))
491
492 (defvar *foreign-library-search-paths* nil
493   "A list of pathnames denoting directories where CLSQL will look
494 for foreign libraries \(in addition to the default places).")
495
496 (defun push-library-path (path)
497   "Adds the pathspec PATH \(which should denote a directory) to
498 the list *FOREIGN-LIBRARY-SEARCH-PATHS*."
499   (pushnew path *foreign-library-search-paths* :test #'equal))