r9133: case handling, test report summarizing, documentation additions
[clsql.git] / base / 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-base-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-type (database)
34   (:documentation
35    "Returns database type")
36   (:method (database)
37            (signal-no-database-error database)))
38
39
40 (defgeneric database-initialize-database-type (database-type)
41   (:documentation
42    "The internal generic implementation of initialize-database-type."))
43
44 (defgeneric database-name-from-spec (connection-spec database-type)
45   (:documentation
46    "Returns the name of the database that would be created if connect
47 was called with the connection-spec."))
48
49 (defgeneric database-connect (connection-spec database-type)
50   (:documentation "Internal generic implementation of connect."))
51
52 (defgeneric database-reconnect (database)
53   (:method ((database t))
54            (signal-no-database-error database))
55   (:documentation "Internal generic implementation of reconnect."))
56
57 (defgeneric database-disconnect (database)
58   (:method ((database t))
59            (signal-no-database-error database))
60   (:documentation "Internal generic implementation of disconnect."))
61
62 (defgeneric database-query (query-expression database result-types)
63   (:method (query-expression (database t) result-types)
64            (declare (ignore query-expression result-types))
65            (signal-no-database-error database))
66   (:documentation "Internal generic implementation of query."))
67
68
69 (defgeneric database-execute-command (sql-expression database)
70   (:method (sql-expression (database t))
71            (declare (ignore sql-expression))
72            (signal-no-database-error database))
73   (:documentation "Internal generic implementation of execute-command."))
74
75 ;;; Mapping and iteration
76 (defgeneric database-query-result-set
77     (query-expression database &key full-set result-types)
78   (:method (query-expression (database t) &key full-set result-types)
79            (declare (ignore query-expression full-set result-types))
80            (signal-no-database-error database)
81            (values nil nil nil))
82   (:documentation
83    "Internal generic implementation of query mapping.  Starts the
84 query specified by query-expression on the given database and returns
85 a result-set to be used with database-store-next-row and
86 database-dump-result-set to access the returned data.  The second
87 value is the number of columns in the result-set, if there are any.
88 If full-set is true, the number of rows in the result-set is returned
89 as a third value, if this is possible (otherwise nil is returned for
90 the third value).  This might have memory and resource usage
91 implications, since many databases will require the query to be
92 executed in full to answer this question.  If the query produced no
93 results then nil is returned for all values that would have been
94 returned otherwise.  If an error occurs during query execution, the
95 function should signal a clsql-sql-error."))
96
97 (defgeneric database-dump-result-set (result-set database)
98   (:method (result-set (database t))
99            (declare (ignore result-set))
100            (signal-no-database-error database))
101   (:documentation "Dumps the received result-set."))
102
103 (defgeneric database-store-next-row (result-set database list)
104   (:method (result-set (database t) list)
105            (declare (ignore result-set list))
106            (signal-no-database-error database))
107   (:documentation
108    "Returns t and stores the next row in the result set in list or
109 returns nil when result-set is finished."))
110
111 (defgeneric database-create (connection-spec type)
112   (:documentation
113    "Creates a database, returns T if successfull or signals an error."))
114
115 (defgeneric database-probe (connection-spec type)
116   (:method (spec type)
117     (declare (ignore spec))
118     (warn "database-proe not support for database-type ~A." type))
119   (:documentation
120    "Probes for the existence of a database, returns T if database found or NIL 
121 if not found. May signal an error if unable to communicate with database server."))
122
123 (defgeneric database-list (connection-spec type)
124   (:method (spec type)
125     (declare (ignore spec))
126     (warn "database-list not support for database-type ~A." type))
127   (:documentation
128    "Lists all databases found for TYPE. May signal an error if unable to communicate with database server."))
129
130 (defgeneric database-destroy (connection-spec database)
131   (:documentation "Destroys (drops) a database."))
132
133 (defgeneric database-truncate (database)
134   (:method ((database t))
135     (signal-no-database-error database))
136   (:documentation "Remove all data from database."))
137
138 (defgeneric database-describe-table (database table)
139   (:method ((database t) table)
140     (declare (ignore table))
141     (signal-no-database-error database))
142   (:documentation "Return a list of name/type for columns in table"))
143
144 (defgeneric database-destory (connection-spec type)
145   (:documentation
146    "Destroys a database, returns T if successfull or signals an error
147 if unable to destory."))
148
149 (defgeneric database-create-sequence (name database)
150   (:documentation "Create a sequence in DATABASE."))
151
152 (defgeneric database-drop-sequence (name database)
153   (:documentation "Drop a sequence from DATABASE."))
154
155 (defgeneric database-sequence-next (name database)
156   (:documentation "Increment a sequence in DATABASE."))
157
158 (defgeneric database-list-sequences (database &key owner)
159   (:documentation "List all sequences in DATABASE."))
160
161 (defgeneric database-set-sequence-position (name position database)
162   (:documentation "Set the position of the sequence called NAME in DATABASE."))
163
164 (defgeneric database-sequence-last (name database)
165   (:documentation "Select the last value in sequence NAME in DATABASE."))
166
167 (defgeneric database-start-transaction (database)
168   (:documentation "Start a transaction in DATABASE."))
169
170 (defgeneric database-commit-transaction (database)
171   (:documentation "Commit current transaction in DATABASE."))
172
173 (defgeneric database-abort-transaction (database)
174   (:documentation "Abort current transaction in DATABASE."))
175
176 (defgeneric database-get-type-specifier (type args database)
177   (:documentation "Return the type SQL type specifier as a string, for
178 the given lisp type and parameters."))
179
180 (defgeneric database-list-tables (database &key owner)
181   (:documentation "List all tables in the given database"))
182  
183 (defgeneric database-list-views (database &key owner)
184   (:documentation "List all views in the DATABASE."))
185
186 (defgeneric database-list-indexes (database &key owner)
187   (:documentation "List all indexes in the DATABASE."))
188
189 (defgeneric database-list-table-indexes (table database &key owner)
190   (:documentation "List all indexes for a table in the DATABASE."))
191
192 (defgeneric database-list-attributes (table database &key owner)
193   (:documentation "List all attributes in TABLE."))
194
195 (defgeneric database-attribute-type (attribute table database &key owner)
196   (:documentation "Return the type of ATTRIBUTE in TABLE."))
197
198 (defgeneric database-add-attribute (table attribute database)
199   (:documentation "Add the attribute to the table."))
200
201 (defgeneric database-rename-attribute (table oldatt newname database)
202   (:documentation "Rename the attribute in the table to NEWNAME."))
203
204 (defgeneric oid (object)
205   (:documentation "Return the unique ID of a database object."))
206
207 ;;; Database backend capabilities
208
209 (defgeneric database-underlying-type (database)
210   (:method (database)
211     (database-type database))
212   (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
213
214 (defgeneric db-type-use-column-on-drop-index? (db-type)
215   (:method (db-type)
216            (declare (ignore db-type))
217            nil)
218   (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
219
220 (defgeneric db-type-has-views? (db-type)
221   (:method (db-type)
222            (declare (ignore db-type))
223            ;; SQL92 has views
224            t)
225   (:documentation "T [default] if database-type supports views."))
226
227 (defgeneric db-type-default-case (db-type)
228   (:method (db-type)
229            (declare (ignore db-type))
230            ;; By default, CommonSQL converts identifiers to UPPER case. 
231            :upper)
232   (: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."))
233
234 (defgeneric db-type-has-fancy-math? (db-type)
235   (:method (db-type)
236            (declare (ignore db-type))
237            nil)
238   (:documentation "NIL [default] if database-type does not have fancy math."))
239
240 (defgeneric db-type-has-subqueries? (db-type)
241   (:method (db-type)
242            (declare (ignore db-type))
243            t)
244   (:documentation "T [default] if database-type supports views."))
245
246 (defgeneric db-type-has-boolean-where? (db-type)
247   (:method (db-type)
248            (declare (ignore db-type))
249            ;; SQL99 has boolean where
250            t)
251   (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
252
253 (defgeneric db-backend-has-create/destroy-db? (db-type)
254   (:method (db-type)
255            (declare (ignore db-type))
256            t)
257   (:documentation "T [default] if backend can destroy and create databases."))
258
259 (defgeneric db-type-transaction-capable? (db database)
260   (:method (db database)
261            (declare (ignore db database))
262            t)
263   (:documentation "T [default] if database can supports transactions."))
264
265 ;;; Large objects support (Marc Battyani)
266
267 (defgeneric database-create-large-object (database)
268   (:documentation "Creates a new large object in the database and returns the object identifier"))
269
270 (defgeneric database-write-large-object (object-id data database)
271   (:documentation "Writes data to the large object"))
272
273 (defgeneric database-read-large-object (object-id database)
274   (:documentation "Reads the large object content"))
275
276 (defgeneric database-delete-large-object (object-id database)
277   (:documentation "Deletes the large object in the database"))
278
279
280 ;; Checks for closed database
281
282 (defmethod database-disconnect :before ((database database))
283   (unless (is-database-open database)
284     (signal-closed-database-error database)))
285
286 (defmethod database-query :before (query-expression (database database) 
287                                    result-set)
288   (declare (ignore query-expression result-set))
289   (unless (is-database-open database)
290     (signal-closed-database-error database)))
291
292 (defmethod database-execute-command :before (sql-expression (database database))
293   (declare (ignore sql-expression))
294   (unless (is-database-open database)
295     (signal-closed-database-error database)))
296
297 (defmethod database-query-result-set :before (expr (database database)
298                                             &key full-set result-types)
299   (declare (ignore expr full-set result-types))
300   (unless (is-database-open database)
301     (signal-closed-database-error database)))
302
303 (defmethod database-dump-result-set :before (result-set (database database))
304   (declare (ignore result-set))
305   (unless (is-database-open database)
306     (signal-closed-database-error database)))
307  
308 (defmethod database-store-next-row :before (result-set (database database) list)
309   (declare (ignore result-set list))
310   (unless (is-database-open database)
311     (signal-closed-database-error database)))
312
313 (defmethod database-commit-transaction :before ((database database))
314   (unless (is-database-open database)
315     (signal-closed-database-error database)))
316
317 (defmethod database-start-transaction :before ((database database))
318   (unless (is-database-open database)
319     (signal-closed-database-error database)))
320
321 (defmethod database-abort-transaction :before ((database database))
322   (unless (is-database-open database)
323     (signal-closed-database-error database)))
324
325 (defgeneric describe-table (table &key database)
326   (:documentation "Describes a table, returns a list of name/type for columns in table"))
327