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