r10547: fix warning
[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-proe 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-destroy (connection-spec database)
145   (:documentation "Destroys (drops) a database."))
146
147 (defgeneric database-truncate (database)
148   (:method ((database t))
149     (signal-no-database-error database))
150   (:documentation "Remove all data from database."))
151
152 (defgeneric database-destory (connection-spec type)
153   (:documentation
154    "Destroys a database, returns T if successfull or signals an error
155 if unable to destory."))
156
157 (defgeneric database-create-sequence (name database)
158   (:documentation "Create a sequence in DATABASE."))
159
160 (defgeneric database-drop-sequence (name database)
161   (:documentation "Drop a sequence from DATABASE."))
162
163 (defgeneric database-sequence-next (name database)
164   (:documentation "Increment a sequence in DATABASE."))
165
166 (defgeneric database-list-sequences (database &key owner)
167   (:documentation "List all sequences in DATABASE."))
168
169 (defgeneric database-set-sequence-position (name position database)
170   (:documentation "Set the position of the sequence called NAME in DATABASE."))
171
172 (defgeneric database-sequence-last (name database)
173   (:documentation "Select the last value in sequence NAME in DATABASE."))
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-has-views? (db-type)
300   (:method (db-type)
301            (declare (ignore db-type))
302            ;; SQL92 has views
303            t)
304   (:documentation "T [default] if database-type supports views."))
305
306 (defgeneric db-type-has-bigint? (db-type)
307   (:method (db-type)
308            (declare (ignore db-type))
309            ;; SQL92 has bigint
310            t)
311   (:documentation "T [default] if database-type supports bigint."))
312
313 (defgeneric db-type-default-case (db-type)
314   (:method (db-type)
315            (declare (ignore db-type))
316            ;; By default, CommonSQL converts identifiers to UPPER case. 
317            :upper)
318   (: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."))
319
320 (defgeneric db-type-has-fancy-math? (db-type)
321   (:method (db-type)
322            (declare (ignore db-type))
323            nil)
324   (:documentation "NIL [default] if database-type does not have fancy math."))
325
326 (defgeneric db-type-has-subqueries? (db-type)
327   (:method (db-type)
328            (declare (ignore db-type))
329            t)
330   (:documentation "T [default] if database-type supports views."))
331
332 (defgeneric db-type-has-boolean-where? (db-type)
333   (:method (db-type)
334            (declare (ignore db-type))
335            ;; SQL99 has boolean where
336            t)
337   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
338
339 (defgeneric db-type-has-union? (db-type)
340   (:method (db-type)
341            (declare (ignore db-type))
342            t)
343   (:documentation "T [default] if database-type supports boolean UNION."))
344
345 (defgeneric db-backend-has-create/destroy-db? (db-type)
346   (:method (db-type)
347            (declare (ignore db-type))
348            t)
349   (:documentation "T [default] if backend can destroy and create databases."))
350
351 (defgeneric db-type-transaction-capable? (db database)
352   (:method (db database)
353            (declare (ignore db database))
354            t)
355   (:documentation "T [default] if database can supports transactions."))
356
357 (defgeneric db-type-has-prepared-stmt? (db-type)
358   (:method ((db-type t))
359     nil)
360   (:documentation "T if database backend supports prepared statements."))
361
362
363 ;;; Large objects support (Marc Battyani)
364
365 (defgeneric database-create-large-object (database)
366   (:documentation "Creates a new large object in the database and returns the object identifier"))
367
368 (defgeneric database-write-large-object (object-id data database)
369   (:documentation "Writes data to the large object"))
370
371 (defgeneric database-read-large-object (object-id database)
372   (:documentation "Reads the large object content"))
373
374 (defgeneric database-delete-large-object (object-id database)
375   (:documentation "Deletes the large object in the database"))
376
377 ;; Prepared statements
378
379 (defgeneric database-prepare (stmt types database result-types field-names)
380   (:method (stmt types (database t) result-types field-names)
381     (declare (ignore stmt types result-types field-names))
382     (signal-no-database-error database))
383   (:method (stmt types (database database) result-types field-names)
384     (declare (ignore stmt types result-types field-names))
385     (error 'sql-database-error
386            :message
387            (format nil "DATABASE-PREPARE not implemented for ~S" database)))
388   (:documentation "Prepare a statement for later execution."))
389
390 (defgeneric database-bind-parameter (prepared-stmt position value)
391   (:method ((pstmt t) position value)
392     (declare (ignore position value))
393     (error 'sql-database-error
394            :message
395            (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
396   (:documentation "Bind a parameter for a prepared statement."))
397
398 (defgeneric database-run-prepared (prepared-stmt)
399   (:method ((pstmt t))
400     (error 'sql-database-error
401            :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
402   (:documentation "Execute a prepared statement."))
403
404 (defgeneric database-free-prepared (prepared-stmt)
405   (:method ((pstmt t))
406     ;; nothing to do by default
407     nil)
408   (:documentation "Free the resources of a prepared statement."))
409
410 ;; Checks for closed database
411
412 (defmethod database-disconnect :before ((database database))
413   (unless (is-database-open database)
414     (signal-closed-database-error database)))
415
416 (defmethod database-query :before (query-expression (database database) 
417                                    result-set field-names)
418   (declare (ignore query-expression result-set field-names))
419   (unless (is-database-open database)
420     (signal-closed-database-error database)))
421
422 (defmethod database-execute-command :before (sql-expression (database database))
423   (declare (ignore sql-expression))
424   (unless (is-database-open database)
425     (signal-closed-database-error database)))
426
427 (defmethod database-query-result-set :before (expr (database database)
428                                             &key full-set result-types)
429   (declare (ignore expr full-set result-types))
430   (unless (is-database-open database)
431     (signal-closed-database-error database)))
432
433 (defmethod database-dump-result-set :before (result-set (database database))
434   (declare (ignore result-set))
435   (unless (is-database-open database)
436     (signal-closed-database-error database)))
437  
438 (defmethod database-store-next-row :before (result-set (database database) list)
439   (declare (ignore result-set list))
440   (unless (is-database-open database)
441     (signal-closed-database-error database)))
442
443 (defmethod database-commit-transaction :before ((database database))
444   (unless (is-database-open database)
445     (signal-closed-database-error database)))
446
447 (defmethod database-start-transaction :before ((database database))
448   (unless (is-database-open database)
449     (signal-closed-database-error database)))
450
451 (defmethod database-abort-transaction :before ((database database))
452   (unless (is-database-open database)
453     (signal-closed-database-error database)))
454
455