Remove CVS $Id$ keyword
[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-start-transaction (database)
171   (:documentation "Start a transaction in DATABASE.")
172   (:method ((database t))
173            (signal-no-database-error database)))
174
175 (defgeneric database-commit-transaction (database)
176   (:documentation "Commit current transaction in DATABASE.")
177   (:method ((database t))
178            (signal-no-database-error database)))
179
180 (defgeneric database-abort-transaction (database)
181   (:documentation "Abort current transaction in DATABASE.")
182   (:method ((database t))
183            (signal-no-database-error database)))
184
185 (defgeneric database-get-type-specifier (type args database db-underlying-type)
186   (:documentation "Return the type SQL type specifier as a string, for
187 the given lisp type and parameters."))
188
189 (defgeneric database-list-tables (database &key owner)
190   (:documentation "List all tables in the given database")
191   (:method ((database database) &key owner)
192            (declare (ignore owner))
193            (warn "database-list-tables not implemented for database type ~A."
194                  (database-type database)))
195   (:method ((database t) &key owner)
196            (declare (ignore owner))
197            (signal-no-database-error database)))
198
199 (defgeneric database-list-tables-and-sequences (database &key owner)
200   (:documentation "List all tables in the given database, may include seqeneces")
201   (:method ((database t) &key owner)
202            (declare (ignore owner))
203            (signal-no-database-error database))
204   (:method ((database database) &key owner)
205            (database-list-tables database :owner owner)))
206
207 (defgeneric database-list-views (database &key owner)
208   (:documentation "List all views in the DATABASE.")
209   (:method ((database database) &key owner)
210            (declare (ignore owner))
211            (warn "database-list-views not implemented for database type ~A."
212                  (database-type database)))
213   (:method ((database t) &key owner)
214            (declare (ignore owner))
215            (signal-no-database-error database)))
216
217 (defgeneric database-list-indexes (database &key owner)
218   (:documentation "List all indexes in the DATABASE.")
219   (:method ((database database) &key owner)
220            (declare (ignore owner))
221            (warn "database-list-indexes not implemented for database type ~A."
222                  (database-type database)))
223   (:method ((database t) &key owner)
224     (declare (ignore owner))
225     (signal-no-database-error database)))
226
227 (defgeneric database-list-table-indexes (table database &key owner)
228   (:documentation "List all indexes for a table in the DATABASE.")
229   (:method (table (database database) &key owner)
230            (declare (ignore table owner))
231            (warn "database-list-table-indexes not implemented for database type ~A."
232                  (database-type database)))
233   (:method (table (database t) &key owner)
234            (declare (ignore table owner))
235            (signal-no-database-error database)))
236
237 (defgeneric database-list-attributes (table database &key owner)
238   (:documentation "List all attributes in TABLE.")
239   (:method (table (database database) &key owner)
240            (declare (ignore table owner))
241            (warn "database-list-attributes not implemented for database type ~A."
242                  (database-type database)))
243   (:method (table (database t) &key owner)
244            (declare (ignore table owner))
245            (signal-no-database-error database)))
246
247 (defgeneric database-attribute-type (attribute table database &key owner)
248   (:documentation "Return the type of ATTRIBUTE in TABLE. Returns multiple values
249 of TYPE_NAME (keyword) PRECISION SCALE NULLABLE.")
250   (:method (attribute table (database database) &key owner)
251            (declare (ignore attribute table owner))
252            (warn "database-list-attribute-type not implemented for database type ~A."
253                  (database-type database)))
254   (:method (attribute table (database t) &key owner)
255            (declare (ignore attribute table owner))
256            (signal-no-database-error database)))
257
258 (defgeneric database-add-attribute (table attribute database)
259   (:documentation "Add the attribute to the table.")
260   (:method (table attribute (database database))
261            (declare (ignore table attribute))
262            (warn "database-add-attribute not implemented for database type ~A."
263                  (database-type database)))
264   (:method (table attribute (database t))
265            (declare (ignore table attribute))
266            (signal-no-database-error database)))
267
268 (defgeneric database-rename-attribute (table oldatt newname database)
269   (:documentation "Rename the attribute in the table to NEWNAME.")
270   (:method (table oldatt newname (database database))
271            (declare (ignore table oldatt newname))
272            (warn "database-rename-attribute not implemented for database type ~A."
273                  (database-type database)))
274   (:method (table oldatt newname (database t))
275            (declare (ignore table oldatt newname))
276            (signal-no-database-error database)))
277
278 (defgeneric oid (object)
279   (:documentation "Return the unique ID of a database object."))
280
281 ;;; Database backend capabilities
282
283 (defgeneric database-underlying-type (database)
284   (:method (database)
285     (database-type database))
286   (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
287
288 (defgeneric db-type-use-column-on-drop-index? (db-type)
289   (:method (db-type)
290            (declare (ignore db-type))
291            nil)
292   (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
293
294 (defgeneric db-type-use-fully-qualified-column-on-drop-index? (db-type)
295   (:method (db-type)
296            (declare (ignore db-type))
297            nil)
298   (:documentation "NIL [default] if database-type does not require fully qualified column name on DROP INDEX."))
299
300 (defgeneric db-type-has-views? (db-type)
301   (:method (db-type)
302            (declare (ignore db-type))
303            ;; SQL92 has views
304            t)
305   (:documentation "T [default] if database-type supports views."))
306
307 (defgeneric db-type-has-bigint? (db-type)
308   (:method (db-type)
309            (declare (ignore db-type))
310            ;; SQL92 has bigint
311            t)
312   (:documentation "T [default] if database-type supports bigint."))
313
314 (defgeneric db-type-default-case (db-type)
315   (:method (db-type)
316            (declare (ignore db-type))
317            ;; By default, CommonSQL converts identifiers to UPPER case.
318            :upper)
319   (: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."))
320
321 (defgeneric db-type-has-fancy-math? (db-type)
322   (:method (db-type)
323            (declare (ignore db-type))
324            nil)
325   (:documentation "NIL [default] if database-type does not have fancy math."))
326
327 (defgeneric db-type-has-subqueries? (db-type)
328   (:method (db-type)
329            (declare (ignore db-type))
330            t)
331   (:documentation "T [default] if database-type supports views."))
332
333 (defgeneric db-type-has-boolean-where? (db-type)
334   (:method (db-type)
335            (declare (ignore db-type))
336            ;; SQL99 has boolean where
337            t)
338   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
339
340 (defgeneric db-type-has-union? (db-type)
341   (:method (db-type)
342            (declare (ignore db-type))
343            t)
344   (:documentation "T [default] if database-type supports boolean UNION."))
345
346 (defgeneric db-backend-has-create/destroy-db? (db-type)
347   (:method (db-type)
348            (declare (ignore db-type))
349            t)
350   (:documentation "T [default] if backend can destroy and create databases."))
351
352 (defgeneric db-type-transaction-capable? (db database)
353   (:method (db database)
354            (declare (ignore db database))
355            t)
356   (:documentation "T [default] if database can supports transactions."))
357
358 (defgeneric db-type-has-prepared-stmt? (db-type)
359   (:method ((db-type t))
360     nil)
361   (:documentation "T if database backend supports prepared statements."))
362
363 (defgeneric db-type-has-intersect? (db-type)
364   (:method (db-type)
365            (declare (ignore db-type))
366            t)
367   (:documentation "T [default] if database-type supports INTERSECT."))
368
369 (defgeneric db-type-has-except? (db-type)
370   (:method (db-type)
371            (declare (ignore db-type))
372            t)
373   (:documentation "T [default] if database-type supports EXCEPT."))
374
375 (defgeneric db-type-has-auto-increment? (db-type)
376   (:method (db-type)
377     (declare (ignore db-type)
378              nil))
379   (:documentation "NIL [default] if database-type supports auto-incrementing columns."))
380
381 ;;; Large objects support (Marc Battyani)
382
383 (defgeneric database-create-large-object (database)
384   (:documentation "Creates a new large object in the database and returns the object identifier"))
385
386 (defgeneric database-write-large-object (object-id data database)
387   (:documentation "Writes data to the large object"))
388
389 (defgeneric database-read-large-object (object-id database)
390   (:documentation "Reads the large object content"))
391
392 (defgeneric database-delete-large-object (object-id database)
393   (:documentation "Deletes the large object in the database"))
394
395 ;; Prepared statements
396
397 (defgeneric database-prepare (stmt types database result-types field-names)
398   (:method (stmt types (database t) result-types field-names)
399     (declare (ignore stmt types result-types field-names))
400     (signal-no-database-error database))
401   (:method (stmt types (database database) result-types field-names)
402     (declare (ignore stmt types result-types field-names))
403     (error 'sql-database-error
404            :message
405            (format nil "DATABASE-PREPARE not implemented for ~S" database)))
406   (:documentation "Prepare a statement for later execution."))
407
408 (defgeneric database-bind-parameter (prepared-stmt position value)
409   (:method ((pstmt t) position value)
410     (declare (ignore position value))
411     (error 'sql-database-error
412            :message
413            (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
414   (:documentation "Bind a parameter for a prepared statement."))
415
416 (defgeneric database-run-prepared (prepared-stmt)
417   (:method ((pstmt t))
418     (error 'sql-database-error
419            :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
420   (:documentation "Execute a prepared statement."))
421
422 (defgeneric database-free-prepared (prepared-stmt)
423   (:method ((pstmt t))
424     ;; nothing to do by default
425     nil)
426   (:documentation "Free the resources of a prepared statement."))
427
428 ;; Checks for closed database
429
430 (defmethod database-disconnect :before ((database database))
431   (unless (is-database-open database)
432     (signal-closed-database-error database)))
433
434 (defmethod database-query :before (query-expression (database database)
435                                    result-set field-names)
436   (declare (ignore query-expression result-set field-names))
437   (unless (is-database-open database)
438     (signal-closed-database-error database)))
439
440 (defmethod database-execute-command :before (sql-expression (database database))
441   (declare (ignore sql-expression))
442   (unless (is-database-open database)
443     (signal-closed-database-error database)))
444
445 (defmethod database-query-result-set :before (expr (database database)
446                                             &key full-set result-types)
447   (declare (ignore expr full-set result-types))
448   (unless (is-database-open database)
449     (signal-closed-database-error database)))
450
451 (defmethod database-dump-result-set :before (result-set (database database))
452   (declare (ignore result-set))
453   (unless (is-database-open database)
454     (signal-closed-database-error database)))
455
456 (defmethod database-store-next-row :before (result-set (database database) list)
457   (declare (ignore result-set list))
458   (unless (is-database-open database)
459     (signal-closed-database-error database)))
460
461 (defmethod database-commit-transaction :before ((database database))
462   (unless (is-database-open database)
463     (signal-closed-database-error database)))
464
465 (defmethod database-start-transaction :before ((database database))
466   (unless (is-database-open database)
467     (signal-closed-database-error database)))
468
469 (defmethod database-abort-transaction :before ((database database))
470   (unless (is-database-open database)
471     (signal-closed-database-error database)))
472
473 (defvar *foreign-library-search-paths* nil
474   "A list of pathnames denoting directories where CLSQL will look
475 for foreign libraries \(in addition to the default places).")
476
477 (defun push-library-path (path)
478   "Adds the pathspec PATH \(which should denote a directory) to
479 the list *FOREIGN-LIBRARY-SEARCH-PATHS*."
480   (pushnew path *foreign-library-search-paths* :test #'equal))