479534ec90fca793ad5bc24bb16e6a0be5a7d5db
[clsql.git] / clsql-base / db-interface.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          db-interface.cl
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: db-interface.cl,v 1.1 2002/08/01 03:06:26 kevin Exp $
13 ;;;;
14 ;;;; This file, part of CLSQL, is Copyright (c) 2002 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
23 (in-package :clsql-base-sys)
24
25 (defgeneric database-type-load-foreign (database-type)
26   (:documentation
27    "The internal generic implementation of reload-database-types."))
28
29 (defgeneric database-type-library-loaded (database-type)
30   (:documentation
31    "The internal generic implementation for checking if
32 database type library loaded successfully."))
33
34 (defgeneric database-type (database)
35   (:documentation
36    "Returns database type")
37   (:method (database)
38            (signal-nodb-error database)))
39
40
41 (defgeneric database-initialize-database-type (database-type)
42   (:documentation
43    "The internal generic implementation of initialize-database-type."))
44
45 (defgeneric database-name-from-spec (connection-spec database-type)
46   (:documentation
47    "Returns the name of the database that would be created if connect
48 was called with the connection-spec."))
49
50 (defgeneric database-connect (connection-spec database-type)
51   (:documentation "Internal generic implementation of connect."))
52
53 (defgeneric database-disconnect (database)
54   (:method ((database closed-database))
55            (signal-closed-database-error database))
56   (:method ((database t))
57            (signal-nodb-error database))
58   (:documentation "Internal generic implementation of disconnect."))
59
60 (defgeneric database-query (query-expression database types)
61   (:method (query-expression (database closed-database) types)
62            (declare (ignore query-expression types))
63            (signal-closed-database-error database))  
64   (:method (query-expression (database t) types)
65            (declare (ignore query-expression types))
66            (signal-nodb-error database))
67   (:documentation "Internal generic implementation of query."))
68
69
70 (defgeneric database-execute-command (sql-expression database)
71   (:method (sql-expression (database closed-database))
72            (declare (ignore sql-expression))
73            (signal-closed-database-error database))
74   (:method (sql-expression (database t))
75            (declare (ignore sql-expression))
76            (signal-nodb-error database))
77   (:documentation "Internal generic implementation of execute-command."))
78
79 ;;; Mapping and iteration
80 (defgeneric database-query-result-set
81     (query-expression database &key full-set types)
82   (:method (query-expression (database closed-database) &key full-set types)
83            (declare (ignore query-expression full-set types))
84            (signal-closed-database-error database)
85            (values nil nil nil))
86   (:method (query-expression (database t) &key full-set types)
87            (declare (ignore query-expression full-set types))
88            (signal-nodb-error database)
89            (values nil nil nil))
90   (:documentation
91    "Internal generic implementation of query mapping.  Starts the
92 query specified by query-expression on the given database and returns
93 a result-set to be used with database-store-next-row and
94 database-dump-result-set to access the returned data.  The second
95 value is the number of columns in the result-set, if there are any.
96 If full-set is true, the number of rows in the result-set is returned
97 as a third value, if this is possible (otherwise nil is returned for
98 the third value).  This might have memory and resource usage
99 implications, since many databases will require the query to be
100 executed in full to answer this question.  If the query produced no
101 results then nil is returned for all values that would have been
102 returned otherwise.  If an error occurs during query execution, the
103 function should signal a clsql-sql-error."))
104
105 (defgeneric database-dump-result-set (result-set database)
106   (:method (result-set (database closed-database))
107            (declare (ignore result-set))
108            (signal-closed-database-error database))
109   (:method (result-set (database t))
110            (declare (ignore result-set))
111            (signal-nodb-error database))
112   (:documentation "Dumps the received result-set."))
113
114 (defgeneric database-store-next-row (result-set database list)
115   (:method (result-set (database closed-database) list)
116            (declare (ignore result-set list))
117            (signal-closed-database-error database))
118   (:method (result-set (database t) list)
119            (declare (ignore result-set list))
120            (signal-nodb-error database))
121   (:documentation
122    "Returns t and stores the next row in the result set in list or
123 returns nil when result-set is finished."))
124
125
126 ;; Interfaces to support UncommonSQL
127
128 (defgeneric database-create-sequence (name database)
129   (:documentation "Create a sequence in DATABASE."))
130
131 (defgeneric database-drop-sequence (name database)
132   (:documentation "Drop a sequence from DATABASE."))
133
134 (defgeneric database-sequence-next (name database)
135   (:documentation "Increment a sequence in DATABASE."))
136
137 (defgeneric database-start-transaction (database)
138   (:documentation "Start a transaction in DATABASE."))
139
140 (defgeneric database-commit-transaction (database)
141   (:documentation "Commit current transaction in DATABASE."))
142
143 (defgeneric database-abort-transaction (database)
144   (:documentation "Abort current transaction in DATABASE."))
145
146 (defgeneric database-get-type-specifier (type args database)
147   (:documentation "Return the type SQL type specifier as a string, for
148 the given lisp type and parameters."))
149
150 (defgeneric database-list-tables (database &key (system-tables nil))
151   (:documentation "List all tables in the given database"))
152
153 (defgeneric database-list-attributes (table database)
154   (:documentation "List all attributes in TABLE."))
155
156 (defgeneric database-attribute-type (attribute table database)
157   (:documentation "Return the type of ATTRIBUTE in TABLE."))
158
159 (defgeneric database-add-attribute (table attribute database)
160   (:documentation "Add the attribute to the table."))
161
162 (defgeneric database-rename-attribute (table oldatt newname database)
163   (:documentation "Rename the attribute in the table to NEWNAME."))
164
165 (defgeneric oid (object)
166   (:documentation "Return the unique ID of a database object."))
167
168  
169 ;;; Large objects support (Marc Battyani)
170
171 (defgeneric database-create-large-object (database)
172   (:documentation "Creates a new large object in the database and returns the object identifier"))
173
174 (defgeneric database-write-large-object (object-id (data string) database)
175   (:documentation "Writes data to the large object"))
176
177 (defgeneric database-read-large-object (object-id database)
178   (:documentation "Reads the large object content"))
179
180 (defgeneric database-delete-large-object (object-id database)
181   (:documentation "Deletes the large object in the database"))