r9119: Automated commit for Debian build of clsql upstream-version-2.9.2
[clsql.git] / sql / table.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; $Id$
5 ;;;;
6 ;;;; The CLSQL Functional Data Definition Language (FDDL)
7 ;;;; including functions for schema manipulation. Currently supported
8 ;;;; SQL objects include tables, views, indexes, attributes and
9 ;;;; sequences.
10 ;;;;
11 ;;;; This file is part of CLSQL.
12 ;;;;
13 ;;;; CLSQL users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
15 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
16 ;;;; *************************************************************************
17
18 (in-package #:clsql-sys)
19
20
21 ;; Utilities
22
23 (defun database-identifier (name)
24   (sql-escape (etypecase name
25                 (string
26                  (string-upcase name))
27                 (sql-ident
28                  (sql-output name))
29                 (symbol
30                  (sql-output name)))))
31
32
33 ;; Tables 
34
35 (defun create-table (name description &key (database *default-database*)
36                           (constraints nil) (transactions t))
37   "Create a table called NAME, in DATABASE which defaults to
38 *DEFAULT-DATABASE*, containing the attributes in DESCRIPTION which is
39 a list containing lists of attribute-name and type information pairs."
40   (let* ((table-name (etypecase name 
41                        (symbol (sql-expression :attribute name))
42                        (string (sql-expression :attribute (make-symbol name)))
43                        (sql-ident name)))
44          (stmt (make-instance 'sql-create-table
45                               :name table-name
46                               :columns description
47                               :modifiers constraints
48                               :transactions transactions)))
49     (execute-command stmt :database database)))
50
51 (defun drop-table (name &key (if-does-not-exist :error)
52                         (database *default-database*))
53   "Drops table NAME from DATABASE which defaults to
54 *DEFAULT-DATABASE*. If the table does not exist and IF-DOES-NOT-EXIST
55 is :ignore then DROP-TABLE returns nil whereas an error is signalled
56 if IF-DOES-NOT-EXIST is :error."
57   (let ((table-name (database-identifier name)))
58     (ecase if-does-not-exist
59       (:ignore
60        (unless (table-exists-p table-name :database database)
61          (return-from drop-table nil)))
62       (:error
63        t))
64     (let ((expr (concatenate 'string "DROP TABLE " table-name)))
65       (execute-command expr :database database))))
66
67 (defun list-tables (&key (owner nil) (database *default-database*))
68   "List all tables in DATABASE which defaults to
69 *DEFAULT-DATABASE*. If OWNER is nil, only user-owned tables are
70 considered. This is the default. If OWNER is :all , all tables are
71 considered. If OWNER is a string, this denotes a username and only
72 tables owned by OWNER are considered. Table names are returned as a
73 list of strings."
74   (database-list-tables database :owner owner))
75
76 (defun table-exists-p (name &key (owner nil) (database *default-database*))
77   "Test for existence of an SQL table called NAME in DATABASE which
78 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
79 tables are considered. This is the default. If OWNER is :all , all
80 tables are considered. If OWNER is a string, this denotes a username
81 and only tables owned by OWNER are considered. Table names are
82 returned as a list of strings."
83   (when (member (database-identifier name)
84                 (list-tables :owner owner :database database)
85                 :test #'string-equal)
86     t))
87
88
89 ;; Views 
90
91 (defun create-view (name &key as column-list (with-check-option nil)
92                          (database *default-database*))
93   "Creates a view called NAME using the AS query and the optional
94 COLUMN-LIST and WITH-CHECK-OPTION. The COLUMN-LIST argument is a list
95 of columns to add to the view. The WITH-CHECK-OPTION adds 'WITH CHECK
96 OPTION' to the resulting SQL. The default value of WITH-CHECK-OPTION
97 is NIL. The default value of DATABASE is *DEFAULT-DATABASE*."
98   (let* ((view-name (etypecase name 
99                       (symbol (sql-expression :attribute name))
100                       (string (sql-expression :attribute (make-symbol name)))
101                       (sql-ident name)))
102          (stmt (make-instance 'sql-create-view
103                               :name view-name
104                               :column-list column-list
105                               :query as
106                               :with-check-option with-check-option)))
107     (execute-command stmt :database database)))
108
109 (defun drop-view (name &key (if-does-not-exist :error)
110                        (database *default-database*))
111   "Deletes view NAME from DATABASE which defaults to
112 *DEFAULT-DATABASE*. If the view does not exist and IF-DOES-NOT-EXIST
113 is :ignore then DROP-VIEW returns nil whereas an error is signalled if
114 IF-DOES-NOT-EXIST is :error."
115   (let ((view-name (database-identifier name)))
116     (ecase if-does-not-exist
117       (:ignore
118        (unless (view-exists-p view-name :database database)
119          (return-from drop-view)))
120       (:error
121        t))
122     (let ((expr (concatenate 'string "DROP VIEW " view-name)))
123       (execute-command expr :database database))))
124
125 (defun list-views (&key (owner nil) (database *default-database*))
126   "List all views in DATABASE which defaults to *DEFAULT-DATABASE*. If
127 OWNER is nil, only user-owned views are considered. This is the
128 default. If OWNER is :all , all views are considered. If OWNER is a
129 string, this denotes a username and only views owned by OWNER are
130 considered. View names are returned as a list of strings."
131   (database-list-views database :owner owner))
132
133 (defun view-exists-p (name &key (owner nil) (database *default-database*))
134   "Test for existence of an SQL view called NAME in DATABASE which
135 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned views
136 are considered. This is the default. If OWNER is :all , all views are
137 considered. If OWNER is a string, this denotes a username and only
138 views owned by OWNER are considered. View names are returned as a list
139 of strings."
140   (when (member (database-identifier name)
141                 (list-views :owner owner :database database)
142                 :test #'string-equal)
143     t))
144
145
146 ;; Indexes 
147
148 (defun create-index (name &key on (unique nil) attributes
149                           (database *default-database*))
150   "Creates an index called NAME on the table specified by ON. The
151 attributes of the table to index are given by ATTRIBUTES. Setting
152 UNIQUE to T includes UNIQUE in the SQL index command, specifying that
153 the columns indexed must contain unique values. The default value of
154 UNIQUE is nil. The default value of DATABASE is *DEFAULT-DATABASE*."
155   (let* ((index-name (database-identifier name))
156          (table-name (database-identifier on))
157          (attributes (mapcar #'database-identifier (listify attributes)))
158          (stmt (format nil "CREATE ~A INDEX ~A ON ~A (~{~A~^, ~})"
159                        (if unique "UNIQUE" "")
160                        index-name table-name attributes)))
161     (execute-command stmt :database database)))
162
163 (defun drop-index (name &key (if-does-not-exist :error)
164                         (on nil)
165                         (database *default-database*))
166   "Deletes index NAME from table FROM in DATABASE which defaults to
167 *DEFAULT-DATABASE*. If the index does not exist and IF-DOES-NOT-EXIST
168 is :ignore then DROP-INDEX returns nil whereas an error is signalled
169 if IF-DOES-NOT-EXIST is :error. The argument ON allows the optional
170 specification of a table to drop the index from."
171   (let ((index-name (database-identifier name)))
172     (ecase if-does-not-exist
173       (:ignore
174        (unless (index-exists-p index-name :database database)
175          (return-from drop-index)))
176       (:error t))
177     (unless (db-type-use-column-on-drop-index? (database-underlying-type database))
178       (setq on nil))
179     (execute-command (format nil "DROP INDEX ~A~A" index-name
180                              (if (null on) ""
181                                  (concatenate 'string " ON "
182                                               (database-identifier on))))
183                      :database database)))
184
185 (defun list-indexes (&key (owner nil) (database *default-database*))
186   "List all indexes in DATABASE, which defaults to
187 *default-database*. If OWNER is :all , all indexs are considered. If
188 OWNER is a string, this denotes a username and only indexs owned by
189 OWNER are considered. Index names are returned as a list of strings."
190   (database-list-indexes database :owner owner))
191
192 (defun list-table-indexes (table &key (owner nil)
193                                       (database *default-database*))
194   "List all indexes in DATABASE for a TABLE, which defaults to
195 *default-database*. If OWNER is :all , all indexs are considered. If
196 OWNER is a string, this denotes a username and only indexs owned by
197 OWNER are considered. Index names are returned as a list of strings."
198   (database-list-table-indexes (database-identifier table)
199                                database :owner owner))
200   
201 (defun index-exists-p (name &key (owner nil) (database *default-database*))
202   "Test for existence of an index called NAME in DATABASE which
203 defaults to *DEFAULT-DATABASE*. If OWNER is :all , all indexs are
204 considered. If OWNER is a string, this denotes a username and only
205 indexs owned by OWNER are considered. Index names are returned as a
206 list of strings."
207   (when (member (database-identifier name)
208                 (list-indexes :owner owner :database database)
209                 :test #'string-equal)
210     t))
211
212 ;; Attributes 
213
214 (defun list-attributes (name &key (owner nil) (database *default-database*))
215   "List the attributes of a attribute called NAME in DATABASE which
216 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
217 attributes are considered. This is the default. If OWNER is :all , all
218 attributes are considered. If OWNER is a string, this denotes a
219 username and only attributes owned by OWNER are considered. Attribute
220 names are returned as a list of strings. Attributes are returned as a
221 list of strings."
222   (database-list-attributes (database-identifier name) database :owner owner))
223
224 (defun attribute-type (attribute table &key (owner nil)
225                                  (database *default-database*))
226   "Return the field type of the ATTRIBUTE in TABLE.  The optional
227 keyword argument DATABASE specifies the database to query, defaulting
228 to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned attributes are
229 considered. This is the default. If OWNER is :all , all attributes are
230 considered. If OWNER is a string, this denotes a username and only
231 attributes owned by OWNER are considered. Attribute names are returned
232 as a list of strings. Attributes are returned as a list of strings."
233   (database-attribute-type (database-identifier attribute)
234                            (database-identifier table)
235                            database
236                            :owner owner))
237
238 (defun list-attribute-types (table &key (owner nil)
239                                    (database *default-database*))
240   "Returns type information for the attributes in TABLE from DATABASE
241 which has a default value of *default-database*. If OWNER is nil, only
242 user-owned attributes are considered. This is the default. If OWNER is
243 :all, all attributes are considered. If OWNER is a string, this
244 denotes a username and only attributes owned by OWNER are
245 considered. Returns a list in which each element is a list (attribute
246 datatype). Attribute is a string denoting the atribute name. Datatype
247 is the vendor-specific type returned by ATTRIBUTE-TYPE."
248   (mapcar #'(lambda (type)
249               (list type (attribute-type type table :database database
250                                          :owner owner)))
251           (list-attributes table :database database :owner owner)))
252
253 ;(defun add-attribute (table attribute &key (database *default-database*))
254 ;  (database-add-attribute table attribute database))
255
256 ;(defun rename-attribute (table oldatt newname
257 ;                               &key (database *default-database*))
258 ;  (error "(rename-attribute ~a ~a ~a ~a) is not implemented"
259 ;         table oldatt newname database))
260
261
262 ;; Sequences 
263
264 (defun create-sequence (name &key (database *default-database*))
265   "Create a sequence called NAME in DATABASE which defaults to
266 *DEFAULT-DATABASE*."
267   (let ((sequence-name (database-identifier name)))
268     (database-create-sequence sequence-name database))
269   (values))
270
271 (defun drop-sequence (name &key (if-does-not-exist :error)
272                            (database *default-database*))
273   "Drops sequence NAME from DATABASE which defaults to
274 *DEFAULT-DATABASE*. If the sequence does not exist and
275 IF-DOES-NOT-EXIST is :ignore then DROP-SEQUENCE returns nil whereas an
276 error is signalled if IF-DOES-NOT-EXIST is :error."
277   (let ((sequence-name (database-identifier name)))
278     (ecase if-does-not-exist
279       (:ignore
280        (unless (sequence-exists-p sequence-name :database database)
281          (return-from drop-sequence)))
282       (:error t))
283     (database-drop-sequence sequence-name database))
284   (values))
285
286 (defun list-sequences (&key (owner nil) (database *default-database*))
287   "List all sequences in DATABASE, which defaults to
288 *default-database*. If OWNER is nil, only user-owned sequences are
289 considered. This is the default. If OWNER is :all , all sequences are
290 considered. If OWNER is a string, this denotes a username and only
291 sequences owned by OWNER are considered. Sequence names are returned
292 as a list of strings."
293   (database-list-sequences database :owner owner))
294
295 (defun sequence-exists-p (name &key (owner nil)
296                                (database *default-database*))
297   "Test for existence of a sequence called NAME in DATABASE which
298 defaults to *DEFAULT-DATABASE*."
299   (when (member (database-identifier name)
300                 (list-sequences :owner owner :database database)
301                 :test #'string-equal)
302     t))
303   
304 (defun sequence-next (name &key (database *default-database*))
305   "Return the next value in the sequence NAME in DATABASE."
306   (database-sequence-next (database-identifier name) database))
307
308 (defun set-sequence-position (name position &key (database *default-database*))
309   "Explicitly set the the position of the sequence NAME in DATABASE to
310 POSITION."
311   (database-set-sequence-position (database-identifier name) position database))
312
313 (defun sequence-last (name &key (database *default-database*))
314   "Return the last value of the sequence NAME in DATABASE."
315   (database-sequence-last (database-identifier name) database))