r8975: remove schema versioning cruft
[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))
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     (execute-command stmt :database database)))
49
50 (defun drop-table (name &key (if-does-not-exist :error)
51                         (database *default-database*))
52   "Drops table NAME from DATABASE which defaults to
53 *DEFAULT-DATABASE*. If the table does not exist and IF-DOES-NOT-EXIST
54 is :ignore then DROP-TABLE returns nil whereas an error is signalled
55 if IF-DOES-NOT-EXIST is :error."
56   (let ((table-name (database-identifier name)))
57     (ecase if-does-not-exist
58       (:ignore
59        (unless (table-exists-p table-name :database database)
60          (return-from drop-table nil)))
61       (:error
62        t))
63     (let ((expr (concatenate 'string "DROP TABLE " table-name)))
64       (execute-command expr :database database))))
65
66 (defun list-tables (&key (owner nil) (database *default-database*))
67   "List all tables in DATABASE which defaults to
68 *DEFAULT-DATABASE*. If OWNER is nil, only user-owned tables are
69 considered. This is the default. If OWNER is :all , all tables are
70 considered. If OWNER is a string, this denotes a username and only
71 tables owned by OWNER are considered. Table names are returned as a
72 list of strings."
73   (database-list-tables database :owner owner))
74
75 (defun table-exists-p (name &key (owner nil) (database *default-database*))
76   "Test for existence of an SQL table called NAME in DATABASE which
77 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
78 tables are considered. This is the default. If OWNER is :all , all
79 tables are considered. If OWNER is a string, this denotes a username
80 and only tables owned by OWNER are considered. Table names are
81 returned as a list of strings."
82   (when (member (database-identifier name)
83                 (list-tables :owner owner :database database)
84                 :test #'string-equal)
85     t))
86
87
88 ;; Views 
89
90 (defun create-view (name &key as column-list (with-check-option nil)
91                          (database *default-database*))
92   "Creates a view called NAME using the AS query and the optional
93 COLUMN-LIST and WITH-CHECK-OPTION. The COLUMN-LIST argument is a list
94 of columns to add to the view. The WITH-CHECK-OPTION adds 'WITH CHECK
95 OPTION' to the resulting SQL. The default value of WITH-CHECK-OPTION
96 is NIL. The default value of DATABASE is *DEFAULT-DATABASE*."
97   (let* ((view-name (etypecase name 
98                       (symbol (sql-expression :attribute name))
99                       (string (sql-expression :attribute (make-symbol name)))
100                       (sql-ident name)))
101          (stmt (make-instance 'sql-create-view
102                               :name view-name
103                               :column-list column-list
104                               :query as
105                               :with-check-option with-check-option)))
106     (execute-command stmt :database database)))
107
108 (defun drop-view (name &key (if-does-not-exist :error)
109                        (database *default-database*))
110   "Deletes view NAME from DATABASE which defaults to
111 *DEFAULT-DATABASE*. If the view does not exist and IF-DOES-NOT-EXIST
112 is :ignore then DROP-VIEW returns nil whereas an error is signalled if
113 IF-DOES-NOT-EXIST is :error."
114   (let ((view-name (database-identifier name)))
115     (ecase if-does-not-exist
116       (:ignore
117        (unless (view-exists-p view-name :database database)
118          (return-from drop-view)))
119       (:error
120        t))
121     (let ((expr (concatenate 'string "DROP VIEW " view-name)))
122       (execute-command expr :database database))))
123
124 (defun list-views (&key (owner nil) (database *default-database*))
125   "List all views in DATABASE which defaults to *DEFAULT-DATABASE*. If
126 OWNER is nil, only user-owned views are considered. This is the
127 default. If OWNER is :all , all views are considered. If OWNER is a
128 string, this denotes a username and only views owned by OWNER are
129 considered. View names are returned as a list of strings."
130   (database-list-views database :owner owner))
131
132 (defun view-exists-p (name &key (owner nil) (database *default-database*))
133   "Test for existence of an SQL view called NAME in DATABASE which
134 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned views
135 are considered. This is the default. If OWNER is :all , all views are
136 considered. If OWNER is a string, this denotes a username and only
137 views owned by OWNER are considered. View names are returned as a list
138 of strings."
139   (when (member (database-identifier name)
140                 (list-views :owner owner :database database)
141                 :test #'string-equal)
142     t))
143
144
145 ;; Indexes 
146
147 (defun create-index (name &key on (unique nil) attributes
148                           (database *default-database*))
149   "Creates an index called NAME on the table specified by ON. The
150 attributes of the table to index are given by ATTRIBUTES. Setting
151 UNIQUE to T includes UNIQUE in the SQL index command, specifying that
152 the columns indexed must contain unique values. The default value of
153 UNIQUE is nil. The default value of DATABASE is *DEFAULT-DATABASE*."
154   (let* ((index-name (database-identifier name))
155          (table-name (database-identifier on))
156          (attributes (mapcar #'database-identifier (listify attributes)))
157          (stmt (format nil "CREATE ~A INDEX ~A ON ~A (~{~A~^, ~})"
158                        (if unique "UNIQUE" "")
159                        index-name table-name attributes)))
160     (execute-command stmt :database database)))
161
162 (defun drop-index (name &key (if-does-not-exist :error)
163                         (on nil)
164                         (database *default-database*))
165   "Deletes index NAME from table FROM in DATABASE which defaults to
166 *DEFAULT-DATABASE*. If the index does not exist and IF-DOES-NOT-EXIST
167 is :ignore then DROP-INDEX returns nil whereas an error is signalled
168 if IF-DOES-NOT-EXIST is :error. The argument ON allows the optional
169 specification of a table to drop the index from."
170   (let ((index-name (database-identifier name)))
171     (ecase if-does-not-exist
172       (:ignore
173        (unless (index-exists-p index-name :database database)
174          (return-from drop-index)))
175       (:error t))
176     (execute-command (format nil "DROP INDEX ~A~A" index-name
177                              (if (null on) ""
178                                  (concatenate 'string " ON "
179                                               (database-identifier on))))
180                      :database database)))
181
182 (defun list-indexes (&key (owner nil) (database *default-database*))
183   "List all indexes in DATABASE, which defaults to
184 *default-database*. If OWNER is :all , all indexs are considered. If
185 OWNER is a string, this denotes a username and only indexs owned by
186 OWNER are considered. Index names are returned as a list of strings."
187   (database-list-indexes database :owner owner))
188   
189 (defun index-exists-p (name &key (owner nil) (database *default-database*))
190   "Test for existence of an index called NAME in DATABASE which
191 defaults to *DEFAULT-DATABASE*. If OWNER is :all , all indexs are
192 considered. If OWNER is a string, this denotes a username and only
193 indexs owned by OWNER are considered. Index names are returned as a
194 list of strings."
195   (when (member (database-identifier name)
196                 (list-indexes :owner owner :database database)
197                 :test #'string-equal)
198     t))
199
200 ;; Attributes 
201
202 (defun list-attributes (name &key (owner nil) (database *default-database*))
203   "List the attributes of a attribute called NAME in DATABASE which
204 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
205 attributes are considered. This is the default. If OWNER is :all , all
206 attributes are considered. If OWNER is a string, this denotes a
207 username and only attributes owned by OWNER are considered. Attribute
208 names are returned as a list of strings. Attributes are returned as a
209 list of strings."
210   (database-list-attributes (database-identifier name) database :owner owner))
211
212 (defun attribute-type (attribute table &key (owner nil)
213                                  (database *default-database*))
214   "Return the field type of the ATTRIBUTE in TABLE.  The optional
215 keyword argument DATABASE specifies the database to query, defaulting
216 to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned attributes are
217 considered. This is the default. If OWNER is :all , all attributes are
218 considered. If OWNER is a string, this denotes a username and only
219 attributes owned by OWNER are considered. Attribute names are returned
220 as a list of strings. Attributes are returned as a list of strings."
221   (database-attribute-type (database-identifier attribute)
222                            (database-identifier table)
223                            database
224                            :owner owner))
225
226 (defun list-attribute-types (table &key (owner nil)
227                                    (database *default-database*))
228   "Returns type information for the attributes in TABLE from DATABASE
229 which has a default value of *default-database*. If OWNER is nil, only
230 user-owned attributes are considered. This is the default. If OWNER is
231 :all, all attributes are considered. If OWNER is a string, this
232 denotes a username and only attributes owned by OWNER are
233 considered. Returns a list in which each element is a list (attribute
234 datatype). Attribute is a string denoting the atribute name. Datatype
235 is the vendor-specific type returned by ATTRIBUTE-TYPE."
236   (mapcar #'(lambda (type)
237               (list type (attribute-type type table :database database
238                                          :owner owner)))
239           (list-attributes table :database database :owner owner)))
240
241 ;(defun add-attribute (table attribute &key (database *default-database*))
242 ;  (database-add-attribute table attribute database))
243
244 ;(defun rename-attribute (table oldatt newname
245 ;                               &key (database *default-database*))
246 ;  (error "(rename-attribute ~a ~a ~a ~a) is not implemented"
247 ;         table oldatt newname database))
248
249
250 ;; Sequences 
251
252 (defun create-sequence (name &key (database *default-database*))
253   "Create a sequence called NAME in DATABASE which defaults to
254 *DEFAULT-DATABASE*."
255   (let ((sequence-name (database-identifier name)))
256     (database-create-sequence sequence-name database))
257   (values))
258
259 (defun drop-sequence (name &key (if-does-not-exist :error)
260                            (database *default-database*))
261   "Drops sequence NAME from DATABASE which defaults to
262 *DEFAULT-DATABASE*. If the sequence does not exist and
263 IF-DOES-NOT-EXIST is :ignore then DROP-SEQUENCE returns nil whereas an
264 error is signalled if IF-DOES-NOT-EXIST is :error."
265   (let ((sequence-name (database-identifier name)))
266     (ecase if-does-not-exist
267       (:ignore
268        (unless (sequence-exists-p sequence-name :database database)
269          (return-from drop-sequence)))
270       (:error t))
271     (database-drop-sequence sequence-name database))
272   (values))
273
274 (defun list-sequences (&key (owner nil) (database *default-database*))
275   "List all sequences in DATABASE, which defaults to
276 *default-database*. If OWNER is nil, only user-owned sequences are
277 considered. This is the default. If OWNER is :all , all sequences are
278 considered. If OWNER is a string, this denotes a username and only
279 sequences owned by OWNER are considered. Sequence names are returned
280 as a list of strings."
281   (database-list-sequences database :owner owner))
282
283 (defun sequence-exists-p (name &key (owner nil)
284                                (database *default-database*))
285   "Test for existence of a sequence called NAME in DATABASE which
286 defaults to *DEFAULT-DATABASE*."
287   (when (member (database-identifier name)
288                 (list-sequences :owner owner :database database)
289                 :test #'string-equal)
290     t))
291   
292 (defun sequence-next (name &key (database *default-database*))
293   "Return the next value in the sequence NAME in DATABASE."
294   (database-sequence-next (database-identifier name) database))
295
296 (defun set-sequence-position (name position &key (database *default-database*))
297   "Explicitly set the the position of the sequence NAME in DATABASE to
298 POSITION."
299   (database-set-sequence-position (database-identifier name) position database))
300
301 (defun sequence-last (name &key (database *default-database*))
302   "Return the last value of the sequence NAME in DATABASE."
303   (database-sequence-last (database-identifier name) database))