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