r8926: add database-create database-destroy database-probe
[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 result-types)
60   (:method (query-expression (database closed-database) result-types)
61            (declare (ignore query-expression result-types))
62            (signal-closed-database-error database))  
63   (:method (query-expression (database t) result-types)
64            (declare (ignore query-expression result-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 result-types)
81   (:method (query-expression (database closed-database) &key full-set result-types)
82            (declare (ignore query-expression full-set result-types))
83            (signal-closed-database-error database)
84            (values nil nil nil))
85   (:method (query-expression (database t) &key full-set result-types)
86            (declare (ignore query-expression full-set result-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 (defgeneric database-create (connection-spec type)
125   (:documentation
126    "Creates a database, returns T if successfull or signals an error."))
127
128 (defgeneric database-probe (connection-spec type)
129   (:documentation
130    "Probes for the existence of a database, returns T if database found or NIL 
131 if not found. May signal an error if unable to communicate with database server."))
132
133 (defgeneric database-destory (connection-spec type)
134   (:documentation
135    "Destroys a database, returns T if successfull or signals an error
136 if unable to destory."))
137
138 (defgeneric database-create-sequence (name database)
139   (:documentation "Create a sequence in DATABASE."))
140
141 (defgeneric database-drop-sequence (name database)
142   (:documentation "Drop a sequence from DATABASE."))
143
144 (defgeneric database-sequence-next (name database)
145   (:documentation "Increment a sequence in DATABASE."))
146
147 (defgeneric database-list-sequences (database &key owner)
148   (:documentation "List all sequences in DATABASE."))
149
150 (defgeneric database-set-sequence-position (name position database)
151   (:documentation "Set the position of the sequence called NAME in DATABASE."))
152
153 (defgeneric database-sequence-last (name database)
154   (:documentation "Select the last value in sequence NAME in DATABASE."))
155
156 (defgeneric database-start-transaction (database)
157   (:documentation "Start a transaction in DATABASE."))
158
159 (defgeneric database-commit-transaction (database)
160   (:documentation "Commit current transaction in DATABASE."))
161
162 (defgeneric database-abort-transaction (database)
163   (:documentation "Abort current transaction in DATABASE."))
164
165 (defgeneric database-get-type-specifier (type args database)
166   (:documentation "Return the type SQL type specifier as a string, for
167 the given lisp type and parameters."))
168
169 (defgeneric database-list-tables (database &key owner)
170   (:documentation "List all tables in the given database"))
171  
172 (defgeneric database-list-views (database &key owner)
173   (:documentation "List all views in the DATABASE."))
174
175 (defgeneric database-list-indexes (database &key owner)
176   (:documentation "List all indexes in the DATABASE."))
177
178 (defgeneric database-list-attributes (table database &key owner)
179   (:documentation "List all attributes in TABLE."))
180
181 (defgeneric database-attribute-type (attribute table database &key owner)
182   (:documentation "Return the type of ATTRIBUTE in TABLE."))
183
184 (defgeneric database-add-attribute (table attribute database)
185   (:documentation "Add the attribute to the table."))
186
187 (defgeneric database-rename-attribute (table oldatt newname database)
188   (:documentation "Rename the attribute in the table to NEWNAME."))
189
190 (defgeneric oid (object)
191   (:documentation "Return the unique ID of a database object."))
192
193
194 ;;; Large objects support (Marc Battyani)
195
196 (defgeneric database-create-large-object (database)
197   (:documentation "Creates a new large object in the database and returns the object identifier"))
198
199 (defgeneric database-write-large-object (object-id data database)
200   (:documentation "Writes data to the large object"))
201
202 (defgeneric database-read-large-object (object-id database)
203   (:documentation "Reads the large object content"))
204
205 (defgeneric database-delete-large-object (object-id database)
206   (:documentation "Deletes the large object in the database"))