r9831: * db-aodbc/aodbc-sql.lisp: Fix storage location
[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   (:documentation "Internal generic implementation of query."))
60
61
62 (defgeneric database-execute-command (sql-expression database)
63   (:method (sql-expression (database t))
64            (declare (ignore sql-expression))
65            (signal-no-database-error database))
66   (:documentation "Internal generic implementation of execute-command."))
67
68 ;;; Mapping and iteration
69 (defgeneric database-query-result-set
70     (query-expression database &key full-set result-types)
71   (:method (query-expression (database t) &key full-set result-types)
72            (declare (ignore query-expression full-set result-types))
73            (signal-no-database-error database)
74            (values nil nil nil))
75   (:documentation
76    "Internal generic implementation of query mapping.  Starts the
77 query specified by query-expression on the given database and returns
78 a result-set to be used with database-store-next-row and
79 database-dump-result-set to access the returned data.  The second
80 value is the number of columns in the result-set, if there are any.
81 If full-set is true, the number of rows in the result-set is returned
82 as a third value, if this is possible (otherwise nil is returned for
83 the third value).  This might have memory and resource usage
84 implications, since many databases will require the query to be
85 executed in full to answer this question.  If the query produced no
86 results then nil is returned for all values that would have been
87 returned otherwise.  If an error occurs during query execution, the
88 function should signal a sql-database-data-error."))
89
90 (defgeneric database-dump-result-set (result-set database)
91   (:method (result-set (database t))
92            (declare (ignore result-set))
93            (signal-no-database-error database))
94   (:documentation "Dumps the received result-set."))
95
96 (defgeneric database-store-next-row (result-set database list)
97   (:method (result-set (database t) list)
98            (declare (ignore result-set list))
99            (signal-no-database-error database))
100   (:documentation
101    "Returns t and stores the next row in the result set in list or
102 returns nil when result-set is finished."))
103
104 (defgeneric database-create (connection-spec type)
105   (:documentation
106    "Creates a database, returns T if successfull or signals an error."))
107
108 (defgeneric database-probe (connection-spec type)
109   (:method (spec type)
110     (declare (ignore spec))
111     (warn "database-proe not support for database-type ~A." type))
112   (:documentation
113    "Probes for the existence of a database, returns T if database found or NIL 
114 if not found. May signal an error if unable to communicate with database server."))
115
116 (defgeneric database-list (connection-spec type)
117   (:method (spec type)
118     (declare (ignore spec))
119     (warn "database-list not support for database-type ~A." type))
120   (:documentation
121    "Lists all databases found for TYPE. May signal an error if unable to communicate with database server."))
122
123 (defgeneric database-destroy (connection-spec database)
124   (:documentation "Destroys (drops) a database."))
125
126 (defgeneric database-truncate (database)
127   (:method ((database t))
128     (signal-no-database-error database))
129   (:documentation "Remove all data from database."))
130
131 (defgeneric database-destory (connection-spec type)
132   (:documentation
133    "Destroys a database, returns T if successfull or signals an error
134 if unable to destory."))
135
136 (defgeneric database-create-sequence (name database)
137   (:documentation "Create a sequence in DATABASE."))
138
139 (defgeneric database-drop-sequence (name database)
140   (:documentation "Drop a sequence from DATABASE."))
141
142 (defgeneric database-sequence-next (name database)
143   (:documentation "Increment a sequence in DATABASE."))
144
145 (defgeneric database-list-sequences (database &key owner)
146   (:documentation "List all sequences in DATABASE."))
147
148 (defgeneric database-set-sequence-position (name position database)
149   (:documentation "Set the position of the sequence called NAME in DATABASE."))
150
151 (defgeneric database-sequence-last (name database)
152   (:documentation "Select the last value in sequence NAME in DATABASE."))
153
154 (defgeneric database-start-transaction (database)
155   (:documentation "Start a transaction in DATABASE.")
156   (:method ((database t))
157            (signal-no-database-error database)))
158
159 (defgeneric database-commit-transaction (database)
160   (:documentation "Commit current transaction in DATABASE.")
161   (:method ((database t))
162            (signal-no-database-error database)))
163
164 (defgeneric database-abort-transaction (database)
165   (:documentation "Abort current transaction in DATABASE.")
166   (:method ((database t))
167            (signal-no-database-error database)))
168
169 (defgeneric database-get-type-specifier (type args database db-underlying-type)
170   (:documentation "Return the type SQL type specifier as a string, for
171 the given lisp type and parameters."))
172
173 (defgeneric database-list-tables (database &key owner)
174   (:documentation "List all tables in the given database")
175   (:method ((database database) &key owner)
176            (declare (ignore owner))
177            (warn "database-list-tables not implemented for database type ~A."
178                  (database-type database)))
179   (:method ((database t) &key owner)
180            (declare (ignore owner))
181            (signal-no-database-error database)))
182
183 (defgeneric database-list-tables-and-sequences (database &key owner)
184   (:documentation "List all tables in the given database, may include seqeneces")
185   (:method ((database t) &key owner)
186            (declare (ignore owner))
187            (signal-no-database-error database))
188   (:method ((database database) &key owner)
189            (database-list-tables database :owner owner)))
190  
191 (defgeneric database-list-views (database &key owner)
192   (:documentation "List all views in the DATABASE.")
193   (:method ((database database) &key owner)
194            (declare (ignore owner))
195            (warn "database-list-views 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-indexes (database &key owner)
202   (:documentation "List all indexes in the DATABASE.")
203   (:method ((database database) &key owner)
204            (declare (ignore owner))
205            (warn "database-list-indexes not implemented for database type ~A."
206                  (database-type database)))
207   (:method ((database t) &key owner)
208     (declare (ignore owner))
209     (signal-no-database-error database)))
210
211 (defgeneric database-list-table-indexes (table database &key owner)
212   (:documentation "List all indexes for a table in the DATABASE.")
213   (:method (table (database database) &key owner)
214            (declare (ignore table owner))
215            (warn "database-list-table-indexes not implemented for database type ~A."
216                  (database-type database)))
217   (:method (table (database t) &key owner) 
218            (declare (ignore table owner))
219            (signal-no-database-error database)))
220
221 (defgeneric database-list-attributes (table database &key owner)
222   (:documentation "List all attributes in TABLE.")
223   (:method (table (database database) &key owner)
224            (declare (ignore table owner))
225            (warn "database-list-attributes not implemented for database type ~A."
226                  (database-type database)))
227   (:method (table (database t) &key owner)
228            (declare (ignore table owner))
229            (signal-no-database-error database)))
230
231 (defgeneric database-attribute-type (attribute table database &key owner)
232   (:documentation "Return the type of ATTRIBUTE in TABLE. Returns multiple values
233 of TYPE_NAME (keyword) PRECISION SCALE NULLABLE.")
234   (:method (attribute table (database database) &key owner)
235            (declare (ignore attribute table owner))
236            (warn "database-list-attribute-type not implemented for database type ~A."
237                  (database-type database)))
238   (:method (attribute table (database t) &key owner)
239            (declare (ignore attribute table owner))
240            (signal-no-database-error database)))
241
242 (defgeneric database-add-attribute (table attribute database)
243   (:documentation "Add the attribute to the table.")
244   (:method (table attribute (database database))
245            (declare (ignore table attribute))
246            (warn "database-add-attribute not implemented for database type ~A."
247                  (database-type database)))
248   (:method (table attribute (database t))
249            (declare (ignore table attribute))
250            (signal-no-database-error database)))
251
252 (defgeneric database-rename-attribute (table oldatt newname database)
253   (:documentation "Rename the attribute in the table to NEWNAME.")
254   (:method (table oldatt newname (database database))
255            (declare (ignore table oldatt newname))
256            (warn "database-rename-attribute not implemented for database type ~A."
257                  (database-type database)))
258   (:method (table oldatt newname (database t))
259            (declare (ignore table oldatt newname))
260            (signal-no-database-error database)))
261
262 (defgeneric oid (object)
263   (:documentation "Return the unique ID of a database object."))
264
265 ;;; Database backend capabilities
266
267 (defgeneric database-underlying-type (database)
268   (:method (database)
269     (database-type database))
270   (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
271
272 (defgeneric db-type-use-column-on-drop-index? (db-type)
273   (:method (db-type)
274            (declare (ignore db-type))
275            nil)
276   (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
277
278 (defgeneric db-type-has-views? (db-type)
279   (:method (db-type)
280            (declare (ignore db-type))
281            ;; SQL92 has views
282            t)
283   (:documentation "T [default] if database-type supports views."))
284
285 (defgeneric db-type-has-bigint? (db-type)
286   (:method (db-type)
287            (declare (ignore db-type))
288            ;; SQL92 has bigint
289            t)
290   (:documentation "T [default] if database-type supports bigint."))
291
292 (defgeneric db-type-default-case (db-type)
293   (:method (db-type)
294            (declare (ignore db-type))
295            ;; By default, CommonSQL converts identifiers to UPPER case. 
296            :upper)
297   (: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."))
298
299 (defgeneric db-type-has-fancy-math? (db-type)
300   (:method (db-type)
301            (declare (ignore db-type))
302            nil)
303   (:documentation "NIL [default] if database-type does not have fancy math."))
304
305 (defgeneric db-type-has-subqueries? (db-type)
306   (:method (db-type)
307            (declare (ignore db-type))
308            t)
309   (:documentation "T [default] if database-type supports views."))
310
311 (defgeneric db-type-has-boolean-where? (db-type)
312   (:method (db-type)
313            (declare (ignore db-type))
314            ;; SQL99 has boolean where
315            t)
316   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
317
318 (defgeneric db-type-has-union? (db-type)
319   (:method (db-type)
320            (declare (ignore db-type))
321            t)
322   (:documentation "T [default] if database-type supports boolean UNION."))
323
324 (defgeneric db-backend-has-create/destroy-db? (db-type)
325   (:method (db-type)
326            (declare (ignore db-type))
327            t)
328   (:documentation "T [default] if backend can destroy and create databases."))
329
330 (defgeneric db-type-transaction-capable? (db database)
331   (:method (db database)
332            (declare (ignore db database))
333            t)
334   (:documentation "T [default] if database can supports transactions."))
335
336 (defgeneric db-type-has-prepared-stmt? (db-type)
337   (:method ((db-type t))
338     nil)
339   (:documentation "T if database backend supports prepared statements."))
340
341
342 ;;; Large objects support (Marc Battyani)
343
344 (defgeneric database-create-large-object (database)
345   (:documentation "Creates a new large object in the database and returns the object identifier"))
346
347 (defgeneric database-write-large-object (object-id data database)
348   (:documentation "Writes data to the large object"))
349
350 (defgeneric database-read-large-object (object-id database)
351   (:documentation "Reads the large object content"))
352
353 (defgeneric database-delete-large-object (object-id database)
354   (:documentation "Deletes the large object in the database"))
355
356 ;; Prepared statements
357
358 (defgeneric database-prepare (stmt types database result-types field-names)
359   (:method (stmt types (database t) result-types field-names)
360     (declare (ignore stmt types result-types field-names))
361     (signal-no-database-error database))
362   (:method (stmt types (database database) result-types field-names)
363     (declare (ignore stmt types result-types field-names))
364     (error 'sql-database-error
365            :message
366            (format nil "DATABASE-PREPARE not implemented for ~S" database)))
367   (:documentation "Prepare a statement for later execution."))
368
369 (defgeneric database-bind-parameter (prepared-stmt position value)
370   (:method ((pstmt t) position value)
371     (declare (ignore position value))
372     (error 'sql-database-error
373            :message
374            (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
375   (:documentation "Bind a parameter for a prepared statement."))
376
377 (defgeneric database-run-prepared (prepared-stmt)
378   (:method ((pstmt t))
379     (error 'sql-database-error
380            :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
381   (:documentation "Execute a prepared statement."))
382
383 (defgeneric database-free-prepared (prepared-stmt)
384   (:method ((pstmt t))
385     ;; nothing to do by default
386     nil)
387   (:documentation "Free the resources of a prepared statement."))
388
389 ;; Checks for closed database
390
391 (defmethod database-disconnect :before ((database database))
392   (unless (is-database-open database)
393     (signal-closed-database-error database)))
394
395 (defmethod database-query :before (query-expression (database database) 
396                                    result-set field-names)
397   (declare (ignore query-expression result-set field-names))
398   (unless (is-database-open database)
399     (signal-closed-database-error database)))
400
401 (defmethod database-execute-command :before (sql-expression (database database))
402   (declare (ignore sql-expression))
403   (unless (is-database-open database)
404     (signal-closed-database-error database)))
405
406 (defmethod database-query-result-set :before (expr (database database)
407                                             &key full-set result-types)
408   (declare (ignore expr full-set result-types))
409   (unless (is-database-open database)
410     (signal-closed-database-error database)))
411
412 (defmethod database-dump-result-set :before (result-set (database database))
413   (declare (ignore result-set))
414   (unless (is-database-open database)
415     (signal-closed-database-error database)))
416  
417 (defmethod database-store-next-row :before (result-set (database database) list)
418   (declare (ignore result-set list))
419   (unless (is-database-open database)
420     (signal-closed-database-error database)))
421
422 (defmethod database-commit-transaction :before ((database database))
423   (unless (is-database-open database)
424     (signal-closed-database-error database)))
425
426 (defmethod database-start-transaction :before ((database database))
427   (unless (is-database-open database)
428     (signal-closed-database-error database)))
429
430 (defmethod database-abort-transaction :before ((database database))
431   (unless (is-database-open database)
432     (signal-closed-database-error database)))
433
434