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 ;;;; *************************************************************************
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 (defvar *cache-table-queries-default* "Default atribute type caching behavior.")
217 (defun cache-table-queries (table &key (action nil) (database *default-database*))
218 "Provides per-table control on the caching in a particular database
219 connection of attribute type information using during update
220 operations. If TABLE is a string, it is the name of the table for
221 which caching is to be altered. If TABLE is t, then the action applies
222 to all tables. If TABLE is :default, then the default caching action
223 is set for those tables which do not have an explicit setting. ACTION
224 specifies the caching action. The value t means cache the attribute
225 type information. The value nil means do not cache the attribute type
226 information. If TABLE is :default, the setting applies to all tables
227 which do not have an explicit setup. The value :flush means remove any
228 existing cache for table in database, but continue to cache. This
229 function should be called with action :flush when the attribute
230 specifications in table have changed."
231 (with-slots (attribute-cache) database
234 (multiple-value-bind (val found) (gethash table attribute-cache)
236 ((and found (eq action :flush))
237 (setf (gethash table attribute-cache) (list t nil)))
238 ((and found (eq action t))
239 (setf (gethash table attribute-cache) (list t (second val))))
240 ((and found (null action))
241 (setf (gethash table attribute-cache) (list nil nil)))
243 (setf (gethash table attribute-cache) (list action nil))))))
245 (maphash (lambda (k v)
248 (setf (gethash k attribute-cache) (list t nil)))
250 (setf (gethash k attribute-cache) (list nil nil)))
252 (setf (gethash k attribute-cache) (list t (second v))))))
255 (maphash (lambda (k v)
256 (when (eq (first v) :unspecified)
259 (setf (gethash k attribute-cache) (list t nil)))
261 (setf (gethash k attribute-cache) (list nil nil)))
263 (setf (gethash k attribute-cache) (list t (second v)))))))
268 (defun list-attributes (name &key (owner nil) (database *default-database*))
269 "List the attributes of a attribute called NAME in DATABASE which
270 defaults to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned
271 attributes are considered. This is the default. If OWNER is :all , all
272 attributes are considered. If OWNER is a string, this denotes a
273 username and only attributes owned by OWNER are considered. Attribute
274 names are returned as a list of strings. Attributes are returned as a
276 (database-list-attributes (database-identifier name database) database :owner owner))
278 (defun attribute-type (attribute table &key (owner nil)
279 (database *default-database*))
280 "Return the field type of the ATTRIBUTE in TABLE. The optional
281 keyword argument DATABASE specifies the database to query, defaulting
282 to *DEFAULT-DATABASE*. If OWNER is nil, only user-owned attributes are
283 considered. This is the default. If OWNER is :all , all attributes are
284 considered. If OWNER is a string, this denotes a username and only
285 attributes owned by OWNER are considered. Attribute names are returned
286 as a list of strings. Attributes are returned as a list of strings."
287 (database-attribute-type (database-identifier attribute database)
288 (database-identifier table database)
292 (defun list-attribute-types (table &key (owner nil)
293 (database *default-database*))
294 "Returns type information for the attributes in TABLE from DATABASE
295 which has a default value of *default-database*. If OWNER is nil, only
296 user-owned attributes are considered. This is the default. If OWNER is
297 :all, all attributes are considered. If OWNER is a string, this
298 denotes a username and only attributes owned by OWNER are
299 considered. Returns a list in which each element is a list (attribute
300 datatype). Attribute is a string denoting the atribute name. Datatype
301 is the vendor-specific type returned by ATTRIBUTE-TYPE."
302 (with-slots (attribute-cache) database
303 (let ((table-ident (database-identifier table database)))
304 (multiple-value-bind (val found) (gethash table-ident attribute-cache)
305 (if (and found (second val))
307 (let ((types (mapcar #'(lambda (attribute)
310 (database-attribute-type
311 (database-identifier attribute database)
315 (list-attributes table :database database :owner owner))))
317 ((and (not found) (eq t *cache-table-queries-default*))
318 (setf (gethash table-ident attribute-cache) (list :unspecified types)))
319 ((and found (eq t (first val))
320 (setf (gethash table-ident attribute-cache) (list t types)))))
326 (defun create-sequence (name &key (database *default-database*))
327 "Create a sequence called NAME in DATABASE which defaults to
329 (let ((sequence-name (database-identifier name database)))
330 (database-create-sequence sequence-name database))
333 (defun drop-sequence (name &key (if-does-not-exist :error)
334 (database *default-database*))
335 "Drops sequence NAME from DATABASE which defaults to
336 *DEFAULT-DATABASE*. If the sequence does not exist and
337 IF-DOES-NOT-EXIST is :ignore then DROP-SEQUENCE returns nil whereas an
338 error is signalled if IF-DOES-NOT-EXIST is :error."
339 (let ((sequence-name (database-identifier name database)))
340 (ecase if-does-not-exist
342 (unless (sequence-exists-p sequence-name :database database)
343 (return-from drop-sequence)))
345 (database-drop-sequence sequence-name database))
348 (defun list-sequences (&key (owner nil) (database *default-database*))
349 "List all sequences in DATABASE, which defaults to
350 *default-database*. If OWNER is nil, only user-owned sequences are
351 considered. This is the default. If OWNER is :all , all sequences are
352 considered. If OWNER is a string, this denotes a username and only
353 sequences owned by OWNER are considered. Sequence names are returned
354 as a list of strings."
355 (database-list-sequences database :owner owner))
357 (defun sequence-exists-p (name &key (owner nil)
358 (database *default-database*))
359 "Test for existence of a sequence called NAME in DATABASE which
360 defaults to *DEFAULT-DATABASE*."
361 (when (member (database-identifier name database)
362 (list-sequences :owner owner :database database)
363 :test #'string-equal)
366 (defun sequence-next (name &key (database *default-database*))
367 "Return the next value in the sequence NAME in DATABASE."
368 (database-sequence-next (database-identifier name database) database))
370 (defun set-sequence-position (name position &key (database *default-database*))
371 "Explicitly set the the position of the sequence NAME in DATABASE to
373 (database-set-sequence-position (database-identifier name database) position database))
375 (defun sequence-last (name &key (database *default-database*))
376 "Return the last value of the sequence NAME in DATABASE."
377 (database-sequence-last (database-identifier name database) database))
381 (defvar *default-update-objects-max-len* nil
382 "The default maximum number of objects supplying data for a query when updating remote joins.")