1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
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
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 ;;;; *************************************************************************
22 (in-package #:clsql-sys
)
24 (defgeneric database-type-load-foreign
(database-type)
26 "The internal generic implementation of reload-database-types."))
28 (defgeneric database-type-library-loaded
(database-type)
30 "The internal generic implementation for checking if
31 database type library loaded successfully."))
33 (defgeneric database-initialize-database-type
(database-type)
35 "The internal generic implementation of initialize-database-type."))
37 (defgeneric database-name-from-spec
(connection-spec database-type
)
39 "Returns the name of the database that would be created if connect
40 was called with the connection-spec."))
42 (defgeneric database-connect
(connection-spec database-type
)
43 (:documentation
"Internal generic implementation of connect."))
45 (defgeneric database-reconnect
(database)
46 (:method
((database t
))
47 (signal-no-database-error database
))
48 (:documentation
"Internal generic implementation of reconnect."))
50 (defgeneric database-disconnect
(database)
51 (:method
((database t
))
52 (signal-no-database-error database
))
53 (:documentation
"Internal generic implementation of disconnect."))
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."))
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."))
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
)
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
))
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."))
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."))
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
)))
122 "Returns t and stores the next row in the result set in list or
123 returns nil when result-set is finished."))
125 (defgeneric database-create
(connection-spec type
)
127 "Creates a database, returns T if successfull or signals an error."))
129 (defgeneric database-probe
(connection-spec type
)
131 (declare (ignore spec
))
132 (warn "database-proe not support for database-type ~A." type
))
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."))
137 (defgeneric database-list
(connection-spec type
)
139 (declare (ignore spec
))
140 (warn "database-list not support for database-type ~A." type
))
142 "Lists all databases found for TYPE. May signal an error if unable to communicate with database server."))
144 (defgeneric database-destroy
(connection-spec database
)
145 (:documentation
"Destroys (drops) a database."))
147 (defgeneric database-truncate
(database)
148 (:method
((database t
))
149 (signal-no-database-error database
))
150 (:documentation
"Remove all data from database."))
152 (defgeneric database-destory
(connection-spec type
)
154 "Destroys a database, returns T if successfull or signals an error
155 if unable to destory."))
157 (defgeneric database-create-sequence
(name database
)
158 (:documentation
"Create a sequence in DATABASE."))
160 (defgeneric database-drop-sequence
(name database
)
161 (:documentation
"Drop a sequence from DATABASE."))
163 (defgeneric database-sequence-next
(name database
)
164 (:documentation
"Increment a sequence in DATABASE."))
166 (defgeneric database-list-sequences
(database &key owner
)
167 (:documentation
"List all sequences in DATABASE."))
169 (defgeneric database-set-sequence-position
(name position database
)
170 (:documentation
"Set the position of the sequence called NAME in DATABASE."))
172 (defgeneric database-sequence-last
(name database
)
173 (:documentation
"Select the last value in sequence NAME in DATABASE."))
175 (defgeneric database-start-transaction
(database)
176 (:documentation
"Start a transaction in DATABASE.")
177 (:method
((database t
))
178 (signal-no-database-error database
)))
180 (defgeneric database-commit-transaction
(database)
181 (:documentation
"Commit current transaction in DATABASE.")
182 (:method
((database t
))
183 (signal-no-database-error database
)))
185 (defgeneric database-abort-transaction
(database)
186 (:documentation
"Abort current transaction in DATABASE.")
187 (:method
((database t
))
188 (signal-no-database-error database
)))
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."))
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
)))
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
)))
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
)))
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
)))
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
)))
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
)))
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
)))
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
)))
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
)))
283 (defgeneric oid
(object)
284 (:documentation
"Return the unique ID of a database object."))
286 ;;; Database backend capabilities
288 (defgeneric database-underlying-type
(database)
290 (database-type database
))
291 (:documentation
"Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
293 (defgeneric db-type-use-column-on-drop-index?
(db-type)
295 (declare (ignore db-type
))
297 (:documentation
"NIL [default] if database-type does not use column name on DROP INDEX."))
299 (defgeneric db-type-use-fully-qualified-column-on-drop-index?
(db-type)
301 (declare (ignore db-type
))
303 (:documentation
"NIL [default] if database-type does not require fully qualified column name on DROP INDEX."))
305 (defgeneric db-type-has-views?
(db-type)
307 (declare (ignore db-type
))
310 (:documentation
"T [default] if database-type supports views."))
312 (defgeneric db-type-has-bigint?
(db-type)
314 (declare (ignore db-type
))
317 (:documentation
"T [default] if database-type supports bigint."))
319 (defgeneric db-type-default-case
(db-type)
321 (declare (ignore db-type
))
322 ;; By default, CommonSQL converts identifiers to UPPER case.
324 (: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."))
326 (defgeneric db-type-has-fancy-math?
(db-type)
328 (declare (ignore db-type
))
330 (:documentation
"NIL [default] if database-type does not have fancy math."))
332 (defgeneric db-type-has-subqueries?
(db-type)
334 (declare (ignore db-type
))
336 (:documentation
"T [default] if database-type supports views."))
338 (defgeneric db-type-has-boolean-where?
(db-type)
340 (declare (ignore db-type
))
341 ;; SQL99 has boolean where
343 (:documentation
"T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
345 (defgeneric db-type-has-union?
(db-type)
347 (declare (ignore db-type
))
349 (:documentation
"T [default] if database-type supports boolean UNION."))
351 (defgeneric db-backend-has-create
/destroy-db?
(db-type)
353 (declare (ignore db-type
))
355 (:documentation
"T [default] if backend can destroy and create databases."))
357 (defgeneric db-type-transaction-capable?
(db database
)
358 (:method
(db database
)
359 (declare (ignore db database
))
361 (:documentation
"T [default] if database can supports transactions."))
363 (defgeneric db-type-has-prepared-stmt?
(db-type)
364 (:method
((db-type t
))
366 (:documentation
"T if database backend supports prepared statements."))
368 (defgeneric db-type-has-intersect?
(db-type)
370 (declare (ignore db-type
))
372 (:documentation
"T [default] if database-type supports INTERSECT."))
374 (defgeneric db-type-has-except?
(db-type)
376 (declare (ignore db-type
))
378 (:documentation
"T [default] if database-type supports EXCEPT."))
380 ;;; Large objects support (Marc Battyani)
382 (defgeneric database-create-large-object
(database)
383 (:documentation
"Creates a new large object in the database and returns the object identifier"))
385 (defgeneric database-write-large-object
(object-id data database
)
386 (:documentation
"Writes data to the large object"))
388 (defgeneric database-read-large-object
(object-id database
)
389 (:documentation
"Reads the large object content"))
391 (defgeneric database-delete-large-object
(object-id database
)
392 (:documentation
"Deletes the large object in the database"))
394 ;; Prepared statements
396 (defgeneric database-prepare
(stmt types database result-types field-names
)
397 (:method
(stmt types
(database t
) result-types field-names
)
398 (declare (ignore stmt types result-types field-names
))
399 (signal-no-database-error database
))
400 (:method
(stmt types
(database database
) result-types field-names
)
401 (declare (ignore stmt types result-types field-names
))
402 (error 'sql-database-error
404 (format nil
"DATABASE-PREPARE not implemented for ~S" database
)))
405 (:documentation
"Prepare a statement for later execution."))
407 (defgeneric database-bind-parameter
(prepared-stmt position value
)
408 (:method
((pstmt t
) position value
)
409 (declare (ignore position value
))
410 (error 'sql-database-error
412 (format nil
"database-bind-paremeter not implemented for ~S" pstmt
)))
413 (:documentation
"Bind a parameter for a prepared statement."))
415 (defgeneric database-run-prepared
(prepared-stmt)
417 (error 'sql-database-error
418 :message
(format nil
"database-run-prepared not specialized for ~S" pstmt
)))
419 (:documentation
"Execute a prepared statement."))
421 (defgeneric database-free-prepared
(prepared-stmt)
423 ;; nothing to do by default
425 (:documentation
"Free the resources of a prepared statement."))
427 ;; Checks for closed database
429 (defmethod database-disconnect :before
((database database
))
430 (unless (is-database-open database
)
431 (signal-closed-database-error database
)))
433 (defmethod database-query :before
(query-expression (database database
)
434 result-set field-names
)
435 (declare (ignore query-expression result-set field-names
))
436 (unless (is-database-open database
)
437 (signal-closed-database-error database
)))
439 (defmethod database-execute-command :before
(sql-expression (database database
))
440 (declare (ignore sql-expression
))
441 (unless (is-database-open database
)
442 (signal-closed-database-error database
)))
444 (defmethod database-query-result-set :before
(expr (database database
)
445 &key full-set result-types
)
446 (declare (ignore expr full-set result-types
))
447 (unless (is-database-open database
)
448 (signal-closed-database-error database
)))
450 (defmethod database-dump-result-set :before
(result-set (database database
))
451 (declare (ignore result-set
))
452 (unless (is-database-open database
)
453 (signal-closed-database-error database
)))
455 (defmethod database-store-next-row :before
(result-set (database database
) list
)
456 (declare (ignore result-set list
))
457 (unless (is-database-open database
)
458 (signal-closed-database-error database
)))
460 (defmethod database-commit-transaction :before
((database database
))
461 (unless (is-database-open database
)
462 (signal-closed-database-error database
)))
464 (defmethod database-start-transaction :before
((database database
))
465 (unless (is-database-open database
)
466 (signal-closed-database-error database
)))
468 (defmethod database-abort-transaction :before
((database database
))
469 (unless (is-database-open database
)
470 (signal-closed-database-error database
)))
472 (defvar *foreign-library-search-paths
* nil
473 "A list of pathnames denoting directories where CLSQL will look
474 for foreign libraries \(in addition to the default places).")
476 (defun push-library-path (path)
477 "Adds the pathspec PATH \(which should denote a directory) to
478 the list *FOREIGN-LIBRARY-SEARCH-PATHS*."
479 (pushnew path
*foreign-library-search-paths
* :test
#'equal
))