r8963: pre 2.6.4
[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   (:documentation
117    "Probes for the existence of a database, returns T if database found or NIL 
118 if not found. May signal an error if unable to communicate with database server."))
119
120 (defgeneric database-destroy (connection-spec database)
121   (:method (connection-spec (database t))
122            (declare (ignore connection-spec))
123            (signal-no-database-error 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
163 (defgeneric database-commit-transaction (database)
164   (:documentation "Commit current transaction in DATABASE."))
165
166 (defgeneric database-abort-transaction (database)
167   (:documentation "Abort current transaction in DATABASE."))
168
169 (defgeneric database-get-type-specifier (type args database)
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  
176 (defgeneric database-list-views (database &key owner)
177   (:documentation "List all views in the DATABASE."))
178
179 (defgeneric database-list-indexes (database &key owner)
180   (:documentation "List all indexes in the DATABASE."))
181
182 (defgeneric database-list-attributes (table database &key owner)
183   (:documentation "List all attributes in TABLE."))
184
185 (defgeneric database-attribute-type (attribute table database &key owner)
186   (:documentation "Return the type of ATTRIBUTE in TABLE."))
187
188 (defgeneric database-add-attribute (table attribute database)
189   (:documentation "Add the attribute to the table."))
190
191 (defgeneric database-rename-attribute (table oldatt newname database)
192   (:documentation "Rename the attribute in the table to NEWNAME."))
193
194 (defgeneric oid (object)
195   (:documentation "Return the unique ID of a database object."))
196
197
198 ;;; Large objects support (Marc Battyani)
199
200 (defgeneric database-create-large-object (database)
201   (:documentation "Creates a new large object in the database and returns the object identifier"))
202
203 (defgeneric database-write-large-object (object-id data database)
204   (:documentation "Writes data to the large object"))
205
206 (defgeneric database-read-large-object (object-id database)
207   (:documentation "Reads the large object content"))
208
209 (defgeneric database-delete-large-object (object-id database)
210   (:documentation "Deletes the large object in the database"))
211
212
213 ;; Checks for closed database
214
215 (defmethod database-disconnect :before ((database database))
216   (unless (is-database-open database)
217     (signal-closed-database-error database)))
218
219 (defmethod database-query :before (query-expression (database database) 
220                                    result-set)
221   (declare (ignore query-expression result-set))
222   (unless (is-database-open database)
223     (signal-closed-database-error database)))
224
225 (defmethod database-execute-command :before (sql-expression (database database))
226   (declare (ignore sql-expression))
227   (unless (is-database-open database)
228     (signal-closed-database-error database)))
229
230 (defmethod database-query-result-set :before (expr (database database)
231                                             &key full-set result-types)
232   (declare (ignore expr full-set result-types))
233   (unless (is-database-open database)
234     (signal-closed-database-error database)))
235
236 (defmethod database-dump-result-set :before (result-set (database database))
237   (declare (ignore result-set))
238   (unless (is-database-open database)
239     (signal-closed-database-error database)))
240  
241 (defmethod database-store-next-row :before (result-set (database database) list)
242   (declare (ignore result-set list))
243   (unless (is-database-open database)
244     (signal-closed-database-error database)))
245
246 (defmethod database-commit-transaction :before ((database database))
247   (unless (is-database-open database)
248     (signal-closed-database-error database)))
249
250 (defmethod database-start-transaction :before ((database database))
251   (unless (is-database-open database)
252     (signal-closed-database-error database)))
253
254 (defmethod database-abort-transaction :before ((database database))
255   (unless (is-database-open database)
256     (signal-closed-database-error database)))
257
258 (defgeneric describe-table (table &key database)
259   (:documentation "Describes a table, returns a list of name/type for columns in table"))
260