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
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
13 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai, and onShoreD
15 ;;;; CLSQL users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (in-package #:clsql-sys)
22 (defgeneric database-type-load-foreign (database-type)
24 "The internal generic implementation of reload-database-types."))
26 (defgeneric database-type-library-loaded (database-type)
28 "The internal generic implementation for checking if
29 database type library loaded successfully."))
31 (defgeneric database-initialize-database-type (database-type)
33 "The internal generic implementation of initialize-database-type."))
35 (defgeneric database-name-from-spec (connection-spec database-type)
37 "Returns the name of the database that would be created if connect
38 was called with the connection-spec."))
40 (defgeneric database-connect (connection-spec database-type)
41 (:documentation "Internal generic implementation of connect."))
43 (defgeneric database-reconnect (database)
44 (:method ((database t))
45 (signal-no-database-error database))
46 (:documentation "Internal generic implementation of reconnect."))
48 (defgeneric database-disconnect (database)
49 (:method ((database t))
50 (signal-no-database-error database))
51 (:documentation "Internal generic implementation of disconnect."))
53 (defgeneric database-query (query-expression database result-types field-names)
54 (:method (query-expression (database t) result-types field-names)
55 (declare (ignore query-expression result-types field-names))
56 (signal-no-database-error database))
57 (:method (query-expression (database database) result-types field-names)
58 (declare (ignore query-expression result-types field-names))
59 (warn "database-query not implemented for database type ~A."
60 (database-type database)))
61 (:documentation "Internal generic implementation of query."))
64 (defgeneric database-execute-command (sql-expression database)
65 (:method (sql-expression (database t))
66 (declare (ignore sql-expression))
67 (signal-no-database-error database))
68 (:method (sql-expression (database database))
69 (declare (ignore sql-expression))
70 (warn "database-execute-command not implemented for database type ~A."
71 (database-type database)))
72 (:documentation "Internal generic implementation of execute-command."))
74 ;;; Mapping and iteration
75 (defgeneric database-query-result-set
76 (query-expression database &key full-set result-types)
77 (:method (query-expression (database t) &key full-set result-types)
78 (declare (ignore query-expression full-set result-types))
79 (signal-no-database-error database)
81 (:method (query-expression (database database) &key full-set result-types)
82 (declare (ignore query-expression full-set result-types))
83 (warn "database-query-result-set not implemented for database type ~A."
84 (database-type database))
87 "Internal generic implementation of query mapping. Starts the
88 query specified by query-expression on the given database and returns
89 a result-set to be used with database-store-next-row and
90 database-dump-result-set to access the returned data. The second
91 value is the number of columns in the result-set, if there are any.
92 If full-set is true, the number of rows in the result-set is returned
93 as a third value, if this is possible (otherwise nil is returned for
94 the third value). This might have memory and resource usage
95 implications, since many databases will require the query to be
96 executed in full to answer this question. If the query produced no
97 results then nil is returned for all values that would have been
98 returned otherwise. If an error occurs during query execution, the
99 function should signal a sql-database-data-error."))
101 (defgeneric database-dump-result-set (result-set database)
102 (:method (result-set (database t))
103 (declare (ignore result-set))
104 (signal-no-database-error database))
105 (:method (result-set (database database))
106 (declare (ignore result-set))
107 (warn "database-dump-result-set not implemented for database type ~A."
108 (database-type database)))
109 (:documentation "Dumps the received result-set."))
111 (defgeneric database-store-next-row (result-set database list)
112 (:method (result-set (database t) list)
113 (declare (ignore result-set list))
114 (signal-no-database-error database))
115 (:method (result-set (database database) list)
116 (declare (ignore result-set list))
117 (warn "database-store-next-row not implemented for database type ~A."
118 (database-type database)))
120 "Returns t and stores the next row in the result set in list or
121 returns nil when result-set is finished."))
123 (defgeneric database-create (connection-spec type)
125 "Creates a database, returns T if successfull or signals an error."))
127 (defgeneric database-probe (connection-spec type)
129 (declare (ignore spec))
130 (warn "database-probe not support for database-type ~A." type))
132 "Probes for the existence of a database, returns T if database found or NIL
133 if not found. May signal an error if unable to communicate with database server."))
135 (defgeneric database-list (connection-spec type)
137 (declare (ignore spec))
138 (warn "database-list not support for database-type ~A." type))
140 "Lists all databases found for TYPE. May signal an error if unable to communicate with database server."))
142 (defgeneric database-truncate (database)
143 (:method ((database t))
144 (signal-no-database-error database))
145 (:documentation "Remove all data from database."))
147 (defgeneric database-destroy (connection-spec type)
149 "Destroys (drops) a database, returns T if successfull or signals an error
150 if unable to destory."))
152 (defgeneric database-create-sequence (name database)
153 (:documentation "Create a sequence in DATABASE."))
155 (defgeneric database-drop-sequence (name database)
156 (:documentation "Drop a sequence from DATABASE."))
158 (defgeneric database-sequence-next (name database)
159 (:documentation "Increment a sequence in DATABASE."))
161 (defgeneric database-list-sequences (database &key owner)
162 (:documentation "List all sequences in DATABASE."))
164 (defgeneric database-set-sequence-position (name position database)
165 (:documentation "Set the position of the sequence called NAME in DATABASE."))
167 (defgeneric database-sequence-last (name database)
168 (:documentation "Select the last value in sequence NAME in DATABASE."))
170 (defgeneric database-last-autoincrement-id (database table column)
171 (:documentation "Many databases have the notion of an auto-increment
172 id; i.e. a sequence implicitly on a table. This function should
176 (defgeneric database-start-transaction (database)
177 (:documentation "Start a transaction in DATABASE.")
178 (:method ((database t))
179 (signal-no-database-error database)))
181 (defgeneric database-commit-transaction (database)
182 (:documentation "Commit current transaction in DATABASE.")
183 (:method ((database t))
184 (signal-no-database-error database)))
186 (defgeneric database-abort-transaction (database)
187 (:documentation "Abort current transaction in DATABASE.")
188 (:method ((database t))
189 (signal-no-database-error database)))
191 (defgeneric database-get-type-specifier (type args database db-underlying-type)
192 (:documentation "Return the type SQL type specifier as a string, for
193 the given lisp type and parameters."))
195 (defgeneric database-list-tables (database &key owner)
196 (:documentation "List all tables in the given database")
197 (:method ((database database) &key owner)
198 (declare (ignore owner))
199 (warn "database-list-tables not implemented for database type ~A."
200 (database-type database)))
201 (:method ((database t) &key owner)
202 (declare (ignore owner))
203 (signal-no-database-error database)))
205 (defgeneric database-list-tables-and-sequences (database &key owner)
206 (:documentation "List all tables in the given database, may include seqeneces")
207 (:method ((database t) &key owner)
208 (declare (ignore owner))
209 (signal-no-database-error database))
210 (:method ((database database) &key owner)
211 (database-list-tables database :owner owner)))
213 (defgeneric database-list-views (database &key owner)
214 (:documentation "List all views in the DATABASE.")
215 (:method ((database database) &key owner)
216 (declare (ignore owner))
217 (warn "database-list-views not implemented for database type ~A."
218 (database-type database)))
219 (:method ((database t) &key owner)
220 (declare (ignore owner))
221 (signal-no-database-error database)))
223 (defgeneric database-list-indexes (database &key owner)
224 (:documentation "List all indexes in the DATABASE.")
225 (:method ((database database) &key owner)
226 (declare (ignore owner))
227 (warn "database-list-indexes not implemented for database type ~A."
228 (database-type database)))
229 (:method ((database t) &key owner)
230 (declare (ignore owner))
231 (signal-no-database-error database)))
233 (defgeneric database-list-table-indexes (table database &key owner)
234 (:documentation "List all indexes for a table in the DATABASE.")
235 (:method (table (database database) &key owner)
236 (declare (ignore table owner))
237 (warn "database-list-table-indexes not implemented for database type ~A."
238 (database-type database)))
239 (:method (table (database t) &key owner)
240 (declare (ignore table owner))
241 (signal-no-database-error database)))
243 (defgeneric database-list-attributes (table database &key owner)
244 (:documentation "List all attributes in TABLE.")
245 (:method (table (database database) &key owner)
246 (declare (ignore table owner))
247 (warn "database-list-attributes not implemented for database type ~A."
248 (database-type database)))
249 (:method (table (database t) &key owner)
250 (declare (ignore table owner))
251 (signal-no-database-error database)))
253 (defgeneric database-attribute-type (attribute table database &key owner)
254 (:documentation "Return the type of ATTRIBUTE in TABLE. Returns multiple values
255 of TYPE_NAME (keyword) PRECISION SCALE NULLABLE.")
256 (:method (attribute table (database database) &key owner)
257 (declare (ignore attribute table owner))
258 (warn "database-list-attribute-type not implemented for database type ~A."
259 (database-type database)))
260 (:method (attribute table (database t) &key owner)
261 (declare (ignore attribute table owner))
262 (signal-no-database-error database)))
264 (defgeneric database-add-attribute (table attribute database)
265 (:documentation "Add the attribute to the table.")
266 (:method (table attribute (database database))
267 (declare (ignore table attribute))
268 (warn "database-add-attribute not implemented for database type ~A."
269 (database-type database)))
270 (:method (table attribute (database t))
271 (declare (ignore table attribute))
272 (signal-no-database-error database)))
274 (defgeneric database-rename-attribute (table oldatt newname database)
275 (:documentation "Rename the attribute in the table to NEWNAME.")
276 (:method (table oldatt newname (database database))
277 (declare (ignore table oldatt newname))
278 (warn "database-rename-attribute not implemented for database type ~A."
279 (database-type database)))
280 (:method (table oldatt newname (database t))
281 (declare (ignore table oldatt newname))
282 (signal-no-database-error database)))
284 (defgeneric oid (object)
285 (:documentation "Return the unique ID of a database object."))
287 ;;; Database backend capabilities
289 (defgeneric database-underlying-type (database)
291 (database-type database))
292 (:documentation "Returns the type of the underlying database. For ODBC, needs to query ODBC driver."))
294 (defgeneric db-type-use-column-on-drop-index? (db-type)
296 (declare (ignore db-type))
298 (:documentation "NIL [default] if database-type does not use column name on DROP INDEX."))
300 (defgeneric db-type-use-fully-qualified-column-on-drop-index? (db-type)
302 (declare (ignore db-type))
304 (:documentation "NIL [default] if database-type does not require fully qualified column name on DROP INDEX."))
306 (defgeneric db-type-has-views? (db-type)
308 (declare (ignore db-type))
311 (:documentation "T [default] if database-type supports views."))
313 (defgeneric db-type-has-bigint? (db-type)
315 (declare (ignore db-type))
318 (:documentation "T [default] if database-type supports bigint."))
320 (defgeneric db-type-default-case (db-type)
322 (declare (ignore db-type))
323 ;; By default, CommonSQL converts identifiers to UPPER case.
325 (: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."))
327 (defgeneric db-type-has-fancy-math? (db-type)
329 (declare (ignore db-type))
331 (:documentation "NIL [default] if database-type does not have fancy math."))
333 (defgeneric db-type-has-subqueries? (db-type)
335 (declare (ignore db-type))
337 (:documentation "T [default] if database-type supports views."))
339 (defgeneric db-type-has-boolean-where? (db-type)
341 (declare (ignore db-type))
342 ;; SQL99 has boolean where
344 (:documentation "T [default] if database-type supports boolean WHERE clause, such as 'WHERE MARRIED'."))
346 (defgeneric db-type-has-union? (db-type)
348 (declare (ignore db-type))
350 (:documentation "T [default] if database-type supports boolean UNION."))
352 (defgeneric db-backend-has-create/destroy-db? (db-type)
354 (declare (ignore db-type))
356 (:documentation "T [default] if backend can destroy and create databases."))
358 (defgeneric db-type-transaction-capable? (db database)
359 (:method (db database)
360 (declare (ignore db database))
362 (:documentation "T [default] if database can supports transactions."))
364 (defgeneric db-type-has-prepared-stmt? (db-type)
365 (:method ((db-type t))
367 (:documentation "T if database backend supports prepared statements."))
369 (defgeneric db-type-has-intersect? (db-type)
371 (declare (ignore db-type))
373 (:documentation "T [default] if database-type supports INTERSECT."))
375 (defgeneric db-type-has-except? (db-type)
377 (declare (ignore db-type))
379 (:documentation "T [default] if database-type supports EXCEPT."))
381 (defgeneric db-type-has-auto-increment? (db-type)
383 (declare (ignore db-type)
385 (:documentation "NIL [default] if database-type supports auto-incrementing columns."))
387 ;;; Large objects support (Marc Battyani)
389 (defgeneric database-create-large-object (database)
390 (:documentation "Creates a new large object in the database and returns the object identifier"))
392 (defgeneric database-write-large-object (object-id data database)
393 (:documentation "Writes data to the large object"))
395 (defgeneric database-read-large-object (object-id database)
396 (:documentation "Reads the large object content"))
398 (defgeneric database-delete-large-object (object-id database)
399 (:documentation "Deletes the large object in the database"))
401 ;; Prepared statements
403 (defgeneric database-prepare (stmt types database result-types field-names)
404 (:method (stmt types (database t) result-types field-names)
405 (declare (ignore stmt types result-types field-names))
406 (signal-no-database-error database))
407 (:method (stmt types (database database) result-types field-names)
408 (declare (ignore stmt types result-types field-names))
409 (error 'sql-database-error
411 (format nil "DATABASE-PREPARE not implemented for ~S" database)))
412 (:documentation "Prepare a statement for later execution."))
414 (defgeneric database-bind-parameter (prepared-stmt position value)
415 (:method ((pstmt t) position value)
416 (declare (ignore position value))
417 (error 'sql-database-error
419 (format nil "database-bind-paremeter not implemented for ~S" pstmt)))
420 (:documentation "Bind a parameter for a prepared statement."))
422 (defgeneric database-run-prepared (prepared-stmt)
424 (error 'sql-database-error
425 :message (format nil "database-run-prepared not specialized for ~S" pstmt)))
426 (:documentation "Execute a prepared statement."))
428 (defgeneric database-free-prepared (prepared-stmt)
430 ;; nothing to do by default
432 (:documentation "Free the resources of a prepared statement."))
434 (defgeneric database-acquire-from-conn-pool (database)
435 (:documentation "Acquire a database connection from the pool. This
436 is a chance to test the connection for validity before returning it to
437 the user. If this function returns NIL or throws an error that
438 database connection is considered bad and we make a new one.
440 Database objects have a chance to specialize, otherwise the default
441 method uses the database-underlying-type and tries to do something
444 (defgeneric database-release-to-conn-pool (database)
445 (:documentation "Chance for the database to cleanup before it is
446 returned to the connection pool."))
448 ;; Checks for closed database
450 (defmethod database-disconnect :before ((database database))
451 (unless (is-database-open database)
452 (signal-closed-database-error database)))
454 (defmethod database-query :before (query-expression (database database)
455 result-set field-names)
456 (declare (ignore query-expression result-set field-names))
457 (unless (is-database-open database)
458 (signal-closed-database-error database)))
460 (defmethod database-execute-command :before (sql-expression (database database))
461 (declare (ignore sql-expression))
462 (unless (is-database-open database)
463 (signal-closed-database-error database)))
465 (defmethod database-query-result-set :before (expr (database database)
466 &key full-set result-types)
467 (declare (ignore expr full-set result-types))
468 (unless (is-database-open database)
469 (signal-closed-database-error database)))
471 (defmethod database-dump-result-set :before (result-set (database database))
472 (declare (ignore result-set))
473 (unless (is-database-open database)
474 (signal-closed-database-error database)))
476 (defmethod database-store-next-row :before (result-set (database database) list)
477 (declare (ignore result-set list))
478 (unless (is-database-open database)
479 (signal-closed-database-error database)))
481 (defmethod database-commit-transaction :before ((database database))
482 (unless (is-database-open database)
483 (signal-closed-database-error database)))
485 (defmethod database-start-transaction :before ((database database))
486 (unless (is-database-open database)
487 (signal-closed-database-error database)))
489 (defmethod database-abort-transaction :before ((database database))
490 (unless (is-database-open database)
491 (signal-closed-database-error database)))
493 (defvar *foreign-library-search-paths* nil
494 "A list of pathnames denoting directories where CLSQL will look
495 for foreign libraries \(in addition to the default places).")
497 (defun push-library-path (path)
498 "Adds the pathspec PATH \(which should denote a directory) to
499 the list *FOREIGN-LIBRARY-SEARCH-PATHS*."
500 (pushnew path *foreign-library-search-paths* :test #'equal))