r9520: continue to use odbc2 semantics
[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-describe-table (database table)
132   (:method ((database t) table)
133     (declare (ignore table))
134     (signal-no-database-error database))
135   (:documentation "Return a list of name/type for columns in table"))
136
137 (defgeneric database-destory (connection-spec type)
138   (:documentation
139    "Destroys a database, returns T if successfull or signals an error
140 if unable to destory."))
141
142 (defgeneric database-create-sequence (name database)
143   (:documentation "Create a sequence in DATABASE."))
144
145 (defgeneric database-drop-sequence (name database)
146   (:documentation "Drop a sequence from DATABASE."))
147
148 (defgeneric database-sequence-next (name database)
149   (:documentation "Increment a sequence in DATABASE."))
150
151 (defgeneric database-list-sequences (database &key owner)
152   (:documentation "List all sequences in DATABASE."))
153
154 (defgeneric database-set-sequence-position (name position database)
155   (:documentation "Set the position of the sequence called NAME in DATABASE."))
156
157 (defgeneric database-sequence-last (name database)
158   (:documentation "Select the last value in sequence NAME in DATABASE."))
159
160 (defgeneric database-start-transaction (database)
161   (:documentation "Start a transaction in DATABASE.")
162   (:method ((database t))
163            (signal-no-database-error database)))
164
165 (defgeneric database-commit-transaction (database)
166   (:documentation "Commit current transaction in DATABASE.")
167   (:method ((database t))
168            (signal-no-database-error database)))
169
170 (defgeneric database-abort-transaction (database)
171   (:documentation "Abort current transaction in DATABASE.")
172   (:method ((database t))
173            (signal-no-database-error database)))
174
175 (defgeneric database-get-type-specifier (type args database db-underlying-type)
176   (:documentation "Return the type SQL type specifier as a string, for
177 the given lisp type and parameters."))
178
179 (defgeneric database-list-tables (database &key owner)
180   (:documentation "List all tables in the given database")
181   (:method ((database t) &key owner)
182            (declare (ignore owner))
183            (signal-no-database-error database)))
184
185 (defgeneric database-list-tables-and-sequences (database &key owner)
186   (:documentation "List all tables in the given database, may include seqeneces")
187   (:method ((database t) &key owner)
188            (declare (ignore owner))
189            (signal-no-database-error database))
190   (:method ((database database) &key owner)
191            (database-list-tables database :owner owner)))
192  
193 (defgeneric database-list-views (database &key owner)
194   (:documentation "List all views in the DATABASE.")
195   (:method ((database t) &key owner)
196            (declare (ignore owner))
197            (signal-no-database-error database)))
198
199 (defgeneric database-list-indexes (database &key owner)
200   (:documentation "List all indexes in the DATABASE.")
201   (:method ((database t) &key owner)
202            (declare (ignore owner))
203            (signal-no-database-error database)))
204
205 (defgeneric database-list-table-indexes (table database &key owner)
206   (:documentation "List all indexes for a table in the DATABASE.")
207   (:method (table (database t) &key owner) 
208            (declare (ignore table owner))
209            (signal-no-database-error database)))
210
211 (defgeneric database-list-attributes (table database &key owner)
212   (:documentation "List all attributes in TABLE.")
213   (:method (table (database t) &key owner)
214            (declare (ignore table owner))
215            (signal-no-database-error database)))
216
217 (defgeneric database-attribute-type (attribute table database &key owner)
218   (:documentation "Return the type of ATTRIBUTE in TABLE. Returns multiple values
219 of TYPE_NAME (keyword) PRECISION SCALE NULLABLE.")
220   (:method (attribute table (database t) &key owner)
221            (declare (ignore attribute table owner))
222            (signal-no-database-error database)))
223
224 (defgeneric database-add-attribute (table attribute database)
225   (:documentation "Add the attribute to the table.")
226   (:method (table attribute (database t))
227            (declare (ignore table attribute))
228            (signal-no-database-error database)))
229
230 (defgeneric database-rename-attribute (table oldatt newname database)
231   (:documentation "Rename the attribute in the table to NEWNAME.")
232   (:method (table oldatt newname (database t))
233            (declare (ignore table oldatt newname))
234            (signal-no-database-error database)))
235
236 (defgeneric oid (object)
237   (:documentation "Return the unique ID of a database object."))
238
239 ;;; Database backend capabilities
240
241 (defgeneric database-underlying-type (database)
242   (:method (database)
243     (database-type database))
244   (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
245
246 (defgeneric db-type-use-column-on-drop-index? (db-type)
247   (:method (db-type)
248            (declare (ignore db-type))
249            nil)
250   (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
251
252 (defgeneric db-type-has-views? (db-type)
253   (:method (db-type)
254            (declare (ignore db-type))
255            ;; SQL92 has views
256            t)
257   (:documentation "T [default] if database-type supports views."))
258
259 (defgeneric db-type-has-bigint? (db-type)
260   (:method (db-type)
261            (declare (ignore db-type))
262            ;; SQL92 has bigint
263            t)
264   (:documentation "T [default] if database-type supports bigint."))
265
266 (defgeneric db-type-default-case (db-type)
267   (:method (db-type)
268            (declare (ignore db-type))
269            ;; By default, CommonSQL converts identifiers to UPPER case. 
270            :upper)
271   (: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."))
272
273 (defgeneric db-type-has-fancy-math? (db-type)
274   (:method (db-type)
275            (declare (ignore db-type))
276            nil)
277   (:documentation "NIL [default] if database-type does not have fancy math."))
278
279 (defgeneric db-type-has-subqueries? (db-type)
280   (:method (db-type)
281            (declare (ignore db-type))
282            t)
283   (:documentation "T [default] if database-type supports views."))
284
285 (defgeneric db-type-has-boolean-where? (db-type)
286   (:method (db-type)
287            (declare (ignore db-type))
288            ;; SQL99 has boolean where
289            t)
290   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
291
292 (defgeneric db-type-has-union? (db-type)
293   (:method (db-type)
294            (declare (ignore db-type))
295            t)
296   (:documentation "T [default] if database-type supports boolean UNION."))
297
298 (defgeneric db-backend-has-create/destroy-db? (db-type)
299   (:method (db-type)
300            (declare (ignore db-type))
301            t)
302   (:documentation "T [default] if backend can destroy and create databases."))
303
304 (defgeneric db-type-transaction-capable? (db database)
305   (:method (db database)
306            (declare (ignore db database))
307            t)
308   (:documentation "T [default] if database can supports transactions."))
309
310 (defgeneric db-type-has-prepared-stmt? (db-type)
311   (:method ((db-type t))
312     nil)
313   (:documentation "T if database backend supports prepared statements."))
314
315
316 ;;; Large objects support (Marc Battyani)
317
318 (defgeneric database-create-large-object (database)
319   (:documentation "Creates a new large object in the database and returns the object identifier"))
320
321 (defgeneric database-write-large-object (object-id data database)
322   (:documentation "Writes data to the large object"))
323
324 (defgeneric database-read-large-object (object-id database)
325   (:documentation "Reads the large object content"))
326
327 (defgeneric database-delete-large-object (object-id database)
328   (:documentation "Deletes the large object in the database"))
329
330 ;; Prepared statements
331
332 (defgeneric database-prepare (stmt types database result-types field-names)
333   (:method (stmt types (database t) result-types field-names)
334     (declare (ignore stmt types result-types field-names))
335     (signal-no-database-error database))
336   (:method (stmt types (database database) result-types field-names)
337     (declare (ignore stmt types result-types field-names))
338     (error 'sql-database-error
339            :message
340            (format nil "DATABASE-PREPARE not implemented for ~S" database)))
341   (:documentation "Prepare a statement for later execution."))
342
343 (defgeneric database-bind-parameter (prepared-stmt position value)
344   (:method ((pstmt t) position value)
345     (declare (ignore position value))
346     (error 'sql-database-error
347            :message
348            (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
349   (:documentation "Bind a parameter for a prepared statement."))
350
351 (defgeneric database-run-prepared (prepared-stmt)
352   (:method ((pstmt t))
353     (error 'sql-database-error
354            :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
355   (:documentation "Execute a prepared statement."))
356
357 (defgeneric database-free-prepared (prepared-stmt)
358   (:method ((pstmt t))
359     ;; nothing to do by default
360     nil)
361   (:documentation "Free the resources of a prepared statement."))
362
363 ;; Checks for closed database
364
365 (defmethod database-disconnect :before ((database database))
366   (unless (is-database-open database)
367     (signal-closed-database-error database)))
368
369 (defmethod database-query :before (query-expression (database database) 
370                                    result-set field-names)
371   (declare (ignore query-expression result-set field-names))
372   (unless (is-database-open database)
373     (signal-closed-database-error database)))
374
375 (defmethod database-execute-command :before (sql-expression (database database))
376   (declare (ignore sql-expression))
377   (unless (is-database-open database)
378     (signal-closed-database-error database)))
379
380 (defmethod database-query-result-set :before (expr (database database)
381                                             &key full-set result-types)
382   (declare (ignore expr full-set result-types))
383   (unless (is-database-open database)
384     (signal-closed-database-error database)))
385
386 (defmethod database-dump-result-set :before (result-set (database database))
387   (declare (ignore result-set))
388   (unless (is-database-open database)
389     (signal-closed-database-error database)))
390  
391 (defmethod database-store-next-row :before (result-set (database database) list)
392   (declare (ignore result-set list))
393   (unless (is-database-open database)
394     (signal-closed-database-error database)))
395
396 (defmethod database-commit-transaction :before ((database database))
397   (unless (is-database-open database)
398     (signal-closed-database-error database)))
399
400 (defmethod database-start-transaction :before ((database database))
401   (unless (is-database-open database)
402     (signal-closed-database-error database)))
403
404 (defmethod database-abort-transaction :before ((database database))
405   (unless (is-database-open database)
406     (signal-closed-database-error database)))
407
408 (defgeneric describe-table (table &key database)
409   (:documentation "Describes a table, returns a list of name/type for columns in table"))
410