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