1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
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
11 ;;;; This file is part of CLSQL.
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 ;;;; *************************************************************************
18 (in-package #:clsql-sys)
23 (defun database-identifier (name database)
24 (sql-escape (etypecase name
26 (convert-to-db-default-case name database))
28 (sql-output name database))
30 (sql-output name database)))))
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)))
44 (stmt (make-instance 'sql-create-table
47 :modifiers constraints
48 :transactions transactions)))
49 (execute-command stmt :database database)))
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 database)))
58 (ecase if-does-not-exist
60 (unless (table-exists-p table-name :database database)
61 (return-from drop-table nil)))
64 (let ((expr (concatenate 'string "DROP TABLE " table-name)))
65 (execute-command expr :database database))))
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
74 (database-list-tables database :owner owner))
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 database)
84 (list-tables :owner owner :database database)
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)))
102 (stmt (make-instance 'sql-create-view
104 :column-list column-list
106 :with-check-option with-check-option)))
107 (execute-command stmt :database database)))
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 database)))
116 (ecase if-does-not-exist
118 (unless (view-exists-p view-name :database database)
119 (return-from drop-view)))
122 (let ((expr (concatenate 'string "DROP VIEW " view-name)))
123 (execute-command expr :database database))))
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))
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
140 (when (member (database-identifier name database)
141 (list-views :owner owner :database database)
142 :test #'string-equal)
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 database))
156 (table-name (database-identifier on database))
157 (attributes (mapcar #'(lambda (a) (database-identifier a database)) (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)))
163 (defun drop-index (name &key (if-does-not-exist :error)
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 database)))
172 (ecase if-does-not-exist
174 (unless (index-exists-p index-name :database database)
175 (return-from drop-index)))
177 (unless (db-type-use-column-on-drop-index?
178 (database-underlying-type database))
180 (execute-command (format nil "DROP INDEX ~A~A" index-name
182 (concatenate 'string " ON "
183 (database-identifier on database))))
184 :database database)))
186 (defun list-indexes (&key (owner nil) (database *default-database*))
187 "List all indexes in DATABASE, which defaults to
188 *default-database*. If OWNER is :all , all indexs are considered. If
189 OWNER is a string, this denotes a username and only indexs owned by
190 OWNER are considered. Index names are returned as a list of strings."
191 (database-list-indexes database :owner owner))
193 (defun list-table-indexes (table &key (owner nil)
194 (database *default-database*))
195 "List all indexes in DATABASE for a TABLE, which defaults to
196 *default-database*. If OWNER is :all , all indexs are considered. If
197 OWNER is a string, this denotes a username and only indexs owned by
198 OWNER are considered. Index names are returned as a list of strings."
199 (database-list-table-indexes (database-identifier table database)
200 database :owner owner))
202 (defun index-exists-p (name &key (owner nil) (database *default-database*))
203 "Test for existence of an index called NAME in DATABASE which
204 defaults to *DEFAULT-DATABASE*. If OWNER is :all , all indexs are
205 considered. If OWNER is a string, this denotes a username and only
206 indexs owned by OWNER are considered. Index names are returned as a
208 (when (member (database-identifier name database)
209 (list-indexes :owner owner :database database)
210 :test #'string-equal)
215 (defun list-attributes (name &key (owner nil) (database *default-database*))
216 "List the attributes of a attribute called NAME in DATABASE which
217 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
218 attributes are considered. This is the default. If OWNER is :all , all
219 attributes are considered. If OWNER is a string, this denotes a
220 username and only attributes owned by OWNER are considered. Attribute
221 names are returned as a list of strings. Attributes are returned as a
223 (database-list-attributes (database-identifier name database) database :owner owner))
225 (defun attribute-type (attribute table &key (owner nil)
226 (database *default-database*))
227 "Return the field type of the ATTRIBUTE in TABLE. The optional
228 keyword argument DATABASE specifies the database to query, defaulting
229 to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned attributes are
230 considered. This is the default. If OWNER is :all , all attributes are
231 considered. If OWNER is a string, this denotes a username and only
232 attributes owned by OWNER are considered. Attribute names are returned
233 as a list of strings. Attributes are returned as a list of strings."
234 (database-attribute-type (database-identifier attribute database)
235 (database-identifier table database)
239 (defun list-attribute-types (table &key (owner nil)
240 (database *default-database*))
241 "Returns type information for the attributes in TABLE from DATABASE
242 which has a default value of *default-database*. If OWNER is nil, only
243 user-owned attributes are considered. This is the default. If OWNER is
244 :all, all attributes are considered. If OWNER is a string, this
245 denotes a username and only attributes owned by OWNER are
246 considered. Returns a list in which each element is a list (attribute
247 datatype). Attribute is a string denoting the atribute name. Datatype
248 is the vendor-specific type returned by ATTRIBUTE-TYPE."
249 (mapcar #'(lambda (type)
250 (list type (attribute-type type table :database database
252 (list-attributes table :database database :owner owner)))
254 ;(defun add-attribute (table attribute &key (database *default-database*))
255 ; (database-add-attribute table attribute database))
257 ;(defun rename-attribute (table oldatt newname
258 ; &key (database *default-database*))
259 ; (error "(rename-attribute ~a ~a ~a ~a) is not implemented"
260 ; table oldatt newname database))
265 (defun create-sequence (name &key (database *default-database*))
266 "Create a sequence called NAME in DATABASE which defaults to
268 (let ((sequence-name (database-identifier name database)))
269 (database-create-sequence sequence-name database))
272 (defun drop-sequence (name &key (if-does-not-exist :error)
273 (database *default-database*))
274 "Drops sequence NAME from DATABASE which defaults to
275 *DEFAULT-DATABASE*. If the sequence does not exist and
276 IF-DOES-NOT-EXIST is :ignore then DROP-SEQUENCE returns nil whereas an
277 error is signalled if IF-DOES-NOT-EXIST is :error."
278 (let ((sequence-name (database-identifier name database)))
279 (ecase if-does-not-exist
281 (unless (sequence-exists-p sequence-name :database database)
282 (return-from drop-sequence)))
284 (database-drop-sequence sequence-name database))
287 (defun list-sequences (&key (owner nil) (database *default-database*))
288 "List all sequences in DATABASE, which defaults to
289 *default-database*. If OWNER is nil, only user-owned sequences are
290 considered. This is the default. If OWNER is :all , all sequences are
291 considered. If OWNER is a string, this denotes a username and only
292 sequences owned by OWNER are considered. Sequence names are returned
293 as a list of strings."
294 (database-list-sequences database :owner owner))
296 (defun sequence-exists-p (name &key (owner nil)
297 (database *default-database*))
298 "Test for existence of a sequence called NAME in DATABASE which
299 defaults to *DEFAULT-DATABASE*."
300 (when (member (database-identifier name database)
301 (list-sequences :owner owner :database database)
302 :test #'string-equal)
305 (defun sequence-next (name &key (database *default-database*))
306 "Return the next value in the sequence NAME in DATABASE."
307 (database-sequence-next (database-identifier name database) database))
309 (defun set-sequence-position (name position &key (database *default-database*))
310 "Explicitly set the the position of the sequence NAME in DATABASE to
312 (database-set-sequence-position (database-identifier name database) position database))
314 (defun sequence-last (name &key (database *default-database*))
315 "Return the last value of the sequence NAME in DATABASE."
316 (database-sequence-last (database-identifier name database) database))