r8821: integrate usql support
[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-nodb-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-disconnect (database)
53   (:method ((database closed-database))
54            (signal-closed-database-error database))
55   (:method ((database t))
56            (signal-nodb-error database))
57   (:documentation "Internal generic implementation of disconnect."))
58
59 (defgeneric database-query (query-expression database types)
60   (:method (query-expression (database closed-database) types)
61            (declare (ignore query-expression types))
62            (signal-closed-database-error database))  
63   (:method (query-expression (database t) types)
64            (declare (ignore query-expression types))
65            (signal-nodb-error database))
66   (:documentation "Internal generic implementation of query."))
67
68
69 (defgeneric database-execute-command (sql-expression database)
70   (:method (sql-expression (database closed-database))
71            (declare (ignore sql-expression))
72            (signal-closed-database-error database))
73   (:method (sql-expression (database t))
74            (declare (ignore sql-expression))
75            (signal-nodb-error database))
76   (:documentation "Internal generic implementation of execute-command."))
77
78 ;;; Mapping and iteration
79 (defgeneric database-query-result-set
80     (query-expression database &key full-set types)
81   (:method (query-expression (database closed-database) &key full-set types)
82            (declare (ignore query-expression full-set types))
83            (signal-closed-database-error database)
84            (values nil nil nil))
85   (:method (query-expression (database t) &key full-set types)
86            (declare (ignore query-expression full-set types))
87            (signal-nodb-error database)
88            (values nil nil nil))
89   (:documentation
90    "Internal generic implementation of query mapping.  Starts the
91 query specified by query-expression on the given database and returns
92 a result-set to be used with database-store-next-row and
93 database-dump-result-set to access the returned data.  The second
94 value is the number of columns in the result-set, if there are any.
95 If full-set is true, the number of rows in the result-set is returned
96 as a third value, if this is possible (otherwise nil is returned for
97 the third value).  This might have memory and resource usage
98 implications, since many databases will require the query to be
99 executed in full to answer this question.  If the query produced no
100 results then nil is returned for all values that would have been
101 returned otherwise.  If an error occurs during query execution, the
102 function should signal a clsql-sql-error."))
103
104 (defgeneric database-dump-result-set (result-set database)
105   (:method (result-set (database closed-database))
106            (declare (ignore result-set))
107            (signal-closed-database-error database))
108   (:method (result-set (database t))
109            (declare (ignore result-set))
110            (signal-nodb-error database))
111   (:documentation "Dumps the received result-set."))
112
113 (defgeneric database-store-next-row (result-set database list)
114   (:method (result-set (database closed-database) list)
115            (declare (ignore result-set list))
116            (signal-closed-database-error database))
117   (:method (result-set (database t) list)
118            (declare (ignore result-set list))
119            (signal-nodb-error database))
120   (:documentation
121    "Returns t and stores the next row in the result set in list or
122 returns nil when result-set is finished."))
123
124
125 ;; Interfaces to support UncommonSQL
126
127 (defgeneric database-create-sequence (name database)
128   (:documentation "Create a sequence in DATABASE."))
129
130 (defgeneric database-drop-sequence (name database)
131   (:documentation "Drop a sequence from DATABASE."))
132
133 (defgeneric database-sequence-next (name database)
134   (:documentation "Increment a sequence in DATABASE."))
135
136 (defgeneric database-list-sequences (database &key owner)
137   (:documentation "List all sequences in DATABASE."))
138
139 (defgeneric database-set-sequence-position (name position database)
140   (:documentation "Set the position of the sequence called NAME in DATABASE."))
141
142 (defgeneric database-sequence-last (name database)
143   (:documentation "Select the last value in sequence NAME in DATABASE."))
144
145 (defgeneric database-start-transaction (database)
146   (:documentation "Start a transaction in DATABASE."))
147
148 (defgeneric database-commit-transaction (database)
149   (:documentation "Commit current transaction in DATABASE."))
150
151 (defgeneric database-abort-transaction (database)
152   (:documentation "Abort current transaction in DATABASE."))
153
154 (defgeneric database-get-type-specifier (type args database)
155   (:documentation "Return the type SQL type specifier as a string, for
156 the given lisp type and parameters."))
157
158 (defgeneric database-list-tables (database &key owner)
159   (:documentation "List all tables in the given database"))
160  
161 (defgeneric database-list-views (database &key owner)
162   (:documentation "List all views in the DATABASE."))
163
164 (defgeneric database-list-indexes (database &key owner)
165   (:documentation "List all indexes in the DATABASE."))
166
167 (defgeneric database-list-attributes (table database &key owner)
168   (:documentation "List all attributes in TABLE."))
169
170 (defgeneric database-attribute-type (attribute table database &key owner)
171   (:documentation "Return the type of ATTRIBUTE in TABLE."))
172
173 (defgeneric database-add-attribute (table attribute database)
174   (:documentation "Add the attribute to the table."))
175
176 (defgeneric database-rename-attribute (table oldatt newname database)
177   (:documentation "Rename the attribute in the table to NEWNAME."))
178
179 (defgeneric oid (object)
180   (:documentation "Return the unique ID of a database object."))
181
182
183 ;;; Large objects support (Marc Battyani)
184
185 (defgeneric database-create-large-object (database)
186   (:documentation "Creates a new large object in the database and returns the object identifier"))
187
188 (defgeneric database-write-large-object (object-id data database)
189   (:documentation "Writes data to the large object"))
190
191 (defgeneric database-read-large-object (object-id database)
192   (:documentation "Reads the large object content"))
193
194 (defgeneric database-delete-large-object (object-id database)
195   (:documentation "Deletes the large object in the database"))