1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; Classes defining SQL expressions and methods for formatting the
7 ;;;; appropriate SQL commands.
9 ;;;; This file is part of CLSQL.
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; *************************************************************************
16 (in-package #:clsql-sys)
18 (defvar +empty-string+ "''")
20 (defvar +null-string+ "NULL")
22 (defvar *sql-stream* nil
23 "stream which accumulates SQL output")
25 (defun sql-output (sql-expr &optional (database *default-database*))
26 "Top-level call for generating SQL strings. Returns an SQL
27 string appropriate for DATABASE which corresponds to the
28 supplied lisp expression SQL-EXPR."
29 (progv '(*sql-stream*)
30 `(,(make-string-output-stream))
31 (output-sql sql-expr database)
32 (get-output-stream-string *sql-stream*)))
34 (defmethod output-sql (expr database)
35 (write-string (database-output-sql expr database) *sql-stream*)
38 (defvar *output-hash* (make-hash-table :test #'equal)
39 "For caching generated SQL strings.")
41 (defmethod output-sql :around ((sql t) database)
42 (let* ((hash-key (output-sql-hash-key sql database))
43 (hash-value (when hash-key (gethash hash-key *output-hash*))))
44 (cond ((and hash-key hash-value)
45 (write-string hash-value *sql-stream*))
47 (let ((*sql-stream* (make-string-output-stream)))
49 (setf hash-value (get-output-stream-string *sql-stream*))
50 (setf (gethash hash-key *output-hash*) hash-value))
51 (write-string hash-value *sql-stream*))
53 (call-next-method)))))
55 (defmethod output-sql-hash-key (expr database)
56 (declare (ignore expr database))
60 (defclass %sql-expression ()
63 (defmethod output-sql ((expr %sql-expression) database)
64 (declare (ignore database))
65 (write-string +null-string+ *sql-stream*))
67 (defmethod print-object ((self %sql-expression) stream)
68 (print-unreadable-object
70 (write-string (sql-output self) stream))
73 ;; For straight up strings
75 (defclass sql (%sql-expression)
79 (:documentation "A literal SQL expression."))
81 (defmethod make-load-form ((sql sql) &optional environment)
82 (declare (ignore environment))
85 `(make-instance 'sql :string ',text)))
87 (defmethod output-sql ((expr sql) database)
88 (declare (ignore database))
89 (write-string (slot-value expr 'text) *sql-stream*)
92 (defmethod print-object ((ident sql) stream)
93 (format stream "#<~S \"~A\">"
95 (sql-output ident nil))
98 ;; For SQL Identifiers of generic type
100 (defclass sql-ident (%sql-expression)
103 :initform +null-string+))
104 (:documentation "An SQL identifer."))
106 (defmethod make-load-form ((sql sql-ident) &optional environment)
107 (declare (ignore environment))
110 `(make-instance 'sql-ident :name ',name)))
112 (defmethod output-sql ((expr sql-ident) database)
114 (with-slots (name) expr
118 (symbol (symbol-name name) database))
122 ;; For SQL Identifiers for attributes
124 (defclass sql-ident-attribute (sql-ident)
127 :initform +null-string+)
130 :initform +null-string+))
131 (:documentation "An SQL Attribute identifier."))
133 (defmethod collect-table-refs (sql)
134 (declare (ignore sql))
137 (defmethod collect-table-refs ((sql sql-ident-attribute))
138 (let ((qual (slot-value sql 'qualifier)))
140 (list (make-instance 'sql-ident-table :name qual)))))
142 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
143 (declare (ignore environment))
144 (with-slots (qualifier type name)
146 `(make-instance 'sql-ident-attribute :name ',name
147 :qualifier ',qualifier
150 (defmethod output-sql ((expr sql-ident-attribute) database)
151 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
152 ;;; should not be output in SQL statements
153 (let ((*print-pretty* nil))
154 (with-slots (qualifier name type) expr
155 (format *sql-stream* "~@[~a.~]~a"
157 (string (format nil "~s" qualifier))
158 (symbol (safety-first (sql-escape qualifier))))
160 (string (format nil "~s" name))
161 (symbol (safety-first (sql-escape name)))))
164 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
165 (with-slots (qualifier name type)
167 (list (and database (database-underlying-type database))
168 'sql-ident-attribute qualifier name type)))
170 ;; For SQL Identifiers for tables
172 (defclass sql-ident-table (sql-ident)
174 :initarg :table-alias :initform nil))
175 (:documentation "An SQL table identifier."))
177 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
178 (declare (ignore environment))
179 (with-slots (alias name)
181 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
183 (defmethod output-sql ((expr sql-ident-table) database)
184 (with-slots (name alias) expr
187 (format *sql-stream* "~s" (sql-escape name)))
189 (write-string (sql-escape name) *sql-stream*)))
191 (format *sql-stream* " ~s" alias)))
194 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
195 (with-slots (name alias)
197 (list (and database (database-underlying-type database))
198 'sql-ident-table name alias)))
200 (defclass sql-relational-exp (%sql-expression)
205 :initarg :sub-expressions
207 (:documentation "An SQL relational expression."))
209 (defmethod make-load-form ((self sql-relational-exp) &optional environment)
210 (make-load-form-saving-slots self
211 :slot-names '(operator sub-expressions)
212 :environment environment))
214 (defmethod collect-table-refs ((sql sql-relational-exp))
216 (dolist (exp (slot-value sql 'sub-expressions))
217 (let ((refs (collect-table-refs exp)))
218 (if refs (setf tabs (append refs tabs)))))
219 (remove-duplicates tabs
220 :test (lambda (tab1 tab2)
221 (equal (slot-value tab1 'name)
222 (slot-value tab2 'name))))))
227 ;; Write SQL for relational operators (like 'AND' and 'OR').
228 ;; should do arity checking of subexpressions
230 (defmethod output-sql ((expr sql-relational-exp) database)
231 (with-slots (operator sub-expressions)
233 (let ((subs (if (consp (car sub-expressions))
234 (car sub-expressions)
236 (write-char #\( *sql-stream*)
237 (do ((sub subs (cdr sub)))
238 ((null (cdr sub)) (output-sql (car sub) database))
239 (output-sql (car sub) database)
240 (write-char #\Space *sql-stream*)
241 (output-sql operator database)
242 (write-char #\Space *sql-stream*))
243 (write-char #\) *sql-stream*)))
246 (defclass sql-array-exp (sql-relational-exp)
248 (:documentation "An SQL relational expression."))
250 (defmethod output-sql ((expr sql-array-exp) database)
251 (with-slots (operator sub-expressions)
253 (let ((subs (if (consp (car sub-expressions))
254 (car sub-expressions)
256 (write-char #\( *sql-stream*)
257 (output-sql operator database)
258 (write-char #\[ *sql-stream*)
259 (do ((sub subs (cdr sub)))
260 ((null (cdr sub)) (output-sql (car sub) database))
261 (output-sql (car sub) database)
262 (write-char #\, *sql-stream*)
263 (write-char #\Space *sql-stream*))
264 (write-char #\] *sql-stream*)
265 (write-char #\) *sql-stream*)))
268 (defclass sql-upcase-like (sql-relational-exp)
270 (:documentation "An SQL 'like' that upcases its arguments."))
272 (defmethod output-sql ((expr sql-upcase-like) database)
273 (flet ((write-term (term)
274 (write-string "upper(" *sql-stream*)
275 (output-sql term database)
276 (write-char #\) *sql-stream*)))
277 (with-slots (sub-expressions)
279 (let ((subs (if (consp (car sub-expressions))
280 (car sub-expressions)
282 (write-char #\( *sql-stream*)
283 (do ((sub subs (cdr sub)))
284 ((null (cdr sub)) (write-term (car sub)))
285 (write-term (car sub))
286 (write-string " LIKE " *sql-stream*))
287 (write-char #\) *sql-stream*))))
290 (defclass sql-assignment-exp (sql-relational-exp)
292 (:documentation "An SQL Assignment expression."))
295 (defmethod output-sql ((expr sql-assignment-exp) database)
296 (with-slots (operator sub-expressions)
298 (do ((sub sub-expressions (cdr sub)))
299 ((null (cdr sub)) (output-sql (car sub) database))
300 (output-sql (car sub) database)
301 (write-char #\Space *sql-stream*)
302 (output-sql operator database)
303 (write-char #\Space *sql-stream*)))
306 (defclass sql-value-exp (%sql-expression)
314 "An SQL value expression.")
317 (defmethod collect-table-refs ((sql sql-value-exp))
319 (if (listp (slot-value sql 'components))
321 (dolist (exp (slot-value sql 'components))
322 (let ((refs (collect-table-refs exp)))
323 (if refs (setf tabs (append refs tabs)))))
324 (remove-duplicates tabs
325 :test (lambda (tab1 tab2)
326 (equal (slot-value tab1 'name)
327 (slot-value tab2 'name)))))
332 (defmethod output-sql ((expr sql-value-exp) database)
333 (with-slots (modifier components)
337 (write-char #\( *sql-stream*)
338 (output-sql modifier database)
339 (write-char #\Space *sql-stream*)
340 (output-sql components database)
341 (write-char #\) *sql-stream*))
342 (output-sql components database))))
344 (defclass sql-typecast-exp (sql-value-exp)
346 (:documentation "An SQL typecast expression."))
348 (defmethod output-sql ((expr sql-typecast-exp) database)
349 (with-slots (components)
351 (output-sql components database)))
353 (defmethod collect-table-refs ((sql sql-typecast-exp))
354 (when (slot-value sql 'components)
355 (collect-table-refs (slot-value sql 'components))))
357 (defclass sql-function-exp (%sql-expression)
365 "An SQL function expression."))
367 (defmethod collect-table-refs ((sql sql-function-exp))
369 (dolist (exp (slot-value sql 'args))
370 (let ((refs (collect-table-refs exp)))
371 (if refs (setf tabs (append refs tabs)))))
372 (remove-duplicates tabs
373 :test (lambda (tab1 tab2)
374 (equal (slot-value tab1 'name)
375 (slot-value tab2 'name))))))
376 (defvar *in-subselect* nil)
378 (defmethod output-sql ((expr sql-function-exp) database)
379 (with-slots (name args)
381 (output-sql name database)
382 (let ((*in-subselect* nil)) ;; aboid double parens
383 (when args (output-sql args database))))
387 (defclass sql-between-exp (sql-function-exp)
389 (:documentation "An SQL between expression."))
391 (defmethod output-sql ((expr sql-between-exp) database)
394 (output-sql (first args) database)
395 (write-string " BETWEEN " *sql-stream*)
396 (output-sql (second args) database)
397 (write-string " AND " *sql-stream*)
398 (output-sql (third args) database))
401 (defclass sql-query-modifier-exp (%sql-expression)
402 ((modifier :initarg :modifier :initform nil)
403 (components :initarg :components :initform nil))
404 (:documentation "An SQL query modifier expression."))
406 (defmethod output-sql ((expr sql-query-modifier-exp) database)
407 (with-slots (modifier components)
409 (output-sql modifier database)
410 (write-string " " *sql-stream*)
411 (output-sql (car components) database)
413 (mapc #'(lambda (comp)
414 (write-string ", " *sql-stream*)
415 (output-sql comp database))
419 (defclass sql-set-exp (%sql-expression)
424 :initarg :sub-expressions
426 (:documentation "An SQL set expression."))
428 (defmethod collect-table-refs ((sql sql-set-exp))
430 (dolist (exp (slot-value sql 'sub-expressions))
431 (let ((refs (collect-table-refs exp)))
432 (if refs (setf tabs (append refs tabs)))))
433 (remove-duplicates tabs
434 :test (lambda (tab1 tab2)
435 (equal (slot-value tab1 'name)
436 (slot-value tab2 'name))))))
438 (defmethod output-sql ((expr sql-set-exp) database)
439 (with-slots (operator sub-expressions)
441 (let ((subs (if (consp (car sub-expressions))
442 (car sub-expressions)
444 (when (= (length subs) 1)
445 (output-sql operator database)
446 (write-char #\Space *sql-stream*))
447 (do ((sub subs (cdr sub)))
448 ((null (cdr sub)) (output-sql (car sub) database))
449 (output-sql (car sub) database)
450 (write-char #\Space *sql-stream*)
451 (output-sql operator database)
452 (write-char #\Space *sql-stream*))))
455 (defclass sql-query (%sql-expression)
466 :initarg :set-operation
498 (:documentation "An SQL SELECT query."))
500 (defclass sql-object-query (%sql-expression)
514 (defmethod collect-table-refs ((sql sql-query))
515 (remove-duplicates (collect-table-refs (slot-value sql 'where))
516 :test (lambda (tab1 tab2)
517 (equal (slot-value tab1 'name)
518 (slot-value tab2 'name)))))
520 (defvar *select-arguments*
521 '(:all :database :distinct :flatp :from :group-by :having :order-by
522 :set-operation :where :offset :limit :inner-join :on
523 ;; below keywords are not a SQL argument, but these keywords may terminate select
526 (defun query-arg-p (sym)
527 (member sym *select-arguments*))
529 (defun query-get-selections (select-args)
530 "Return two values: the list of select-args up to the first keyword,
531 uninclusive, and the args from that keyword to the end."
532 (let ((first-key-arg (position-if #'query-arg-p select-args)))
534 (values (subseq select-args 0 first-key-arg)
535 (subseq select-args first-key-arg))
538 (defun make-query (&rest args)
539 (flet ((select-objects (target-args)
541 (every #'(lambda (arg)
543 (find-class arg nil)))
545 (multiple-value-bind (selections arglist)
546 (query-get-selections args)
547 (if (select-objects selections)
548 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
549 (make-instance 'sql-object-query :objects selections
550 :flatp flatp :refresh refresh
552 (destructuring-bind (&key all flatp set-operation distinct from where
553 group-by having order-by
554 offset limit inner-join on &allow-other-keys)
556 (if (null selections)
557 (error "No target columns supplied to select statement."))
559 (error "No source tables supplied to select statement."))
560 (make-instance 'sql-query :selections selections
561 :all all :flatp flatp :set-operation set-operation
562 :distinct distinct :from from :where where
563 :limit limit :offset offset
564 :group-by group-by :having having :order-by order-by
565 :inner-join inner-join :on on))))))
567 (defmethod output-sql ((query sql-query) database)
568 (with-slots (distinct selections from where group-by having order-by
569 limit offset inner-join on all set-operation)
572 (write-string "(" *sql-stream*))
573 (write-string "SELECT " *sql-stream*)
575 (write-string "ALL " *sql-stream*))
576 (when (and limit (eq :odbc (database-type database)))
577 (write-string " TOP " *sql-stream*)
578 (output-sql limit database))
579 (when (and distinct (not all))
580 (write-string "DISTINCT " *sql-stream*)
581 (unless (eql t distinct)
582 (write-string "ON " *sql-stream*)
583 (output-sql distinct database)
584 (write-char #\Space *sql-stream*)))
585 (let ((*in-subselect* t))
586 (output-sql (apply #'vector selections) database))
588 (write-string " FROM " *sql-stream*)
589 (flet ((ident-table-equal (a b)
590 (and (if (and (eql (type-of a) 'sql-ident-table)
591 (eql (type-of b) 'sql-ident-table))
592 (string-equal (slot-value a 'alias)
593 (slot-value b 'alias))
595 (string-equal (sql-escape (slot-value a 'name))
596 (sql-escape (slot-value b 'name))))))
598 (list (output-sql (apply #'vector
599 (remove-duplicates from
600 :test #'ident-table-equal))
602 (string (format *sql-stream* "~s" (sql-escape from)))
603 (t (let ((*in-subselect* t))
604 (output-sql from database))))))
606 (write-string " INNER JOIN " *sql-stream*)
607 (output-sql inner-join database))
609 (write-string " ON " *sql-stream*)
610 (output-sql on database))
612 (write-string " WHERE " *sql-stream*)
613 (let ((*in-subselect* t))
614 (output-sql where database)))
616 (write-string " GROUP BY " *sql-stream*)
618 (do ((order group-by (cdr order)))
620 (let ((item (car order)))
623 (output-sql (car item) database)
624 (format *sql-stream* " ~A" (cadr item)))
626 (output-sql item database)))
628 (write-char #\, *sql-stream*))))
629 (output-sql group-by database)))
631 (write-string " HAVING " *sql-stream*)
632 (output-sql having database))
634 (write-string " ORDER BY " *sql-stream*)
636 (do ((order order-by (cdr order)))
638 (let ((item (car order)))
641 (output-sql (car item) database)
642 (format *sql-stream* " ~A" (cadr item)))
644 (output-sql item database)))
646 (write-char #\, *sql-stream*))))
647 (output-sql order-by database)))
648 (when (and limit (not (eq :odbc (database-type database))))
649 (write-string " LIMIT " *sql-stream*)
650 (output-sql limit database))
652 (write-string " OFFSET " *sql-stream*)
653 (output-sql offset database))
655 (write-string ")" *sql-stream*))
657 (write-char #\Space *sql-stream*)
658 (output-sql set-operation database)))
661 (defmethod output-sql ((query sql-object-query) database)
662 (declare (ignore database))
663 (with-slots (objects)
666 (format *sql-stream* "(~{~A~^ ~})" objects))))
671 (defclass sql-insert (%sql-expression)
685 "An SQL INSERT statement."))
687 (defmethod output-sql ((ins sql-insert) database)
688 (with-slots (into attributes values query)
690 (write-string "INSERT INTO " *sql-stream*)
693 (string (sql-expression :table into))
697 (write-char #\Space *sql-stream*)
698 (output-sql attributes database))
700 (write-string " VALUES " *sql-stream*)
701 (output-sql values database))
703 (write-char #\Space *sql-stream*)
704 (output-sql query database)))
709 (defclass sql-delete (%sql-expression)
717 "An SQL DELETE statement."))
719 (defmethod output-sql ((stmt sql-delete) database)
720 (with-slots (from where)
722 (write-string "DELETE FROM " *sql-stream*)
724 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
725 (t (output-sql from database)))
727 (write-string " WHERE " *sql-stream*)
728 (output-sql where database)))
733 (defclass sql-update (%sql-expression)
746 (:documentation "An SQL UPDATE statement."))
748 (defmethod output-sql ((expr sql-update) database)
749 (with-slots (table where attributes values)
751 (flet ((update-assignments ()
752 (mapcar #'(lambda (a b)
753 (make-instance 'sql-assignment-exp
755 :sub-expressions (list a b)))
757 (write-string "UPDATE " *sql-stream*)
758 (output-sql table database)
759 (write-string " SET " *sql-stream*)
760 (output-sql (apply #'vector (update-assignments)) database)
762 (write-string " WHERE " *sql-stream*)
763 (output-sql where database))))
768 (defclass sql-create-table (%sql-expression)
779 :initarg :transactions
782 "An SQL CREATE TABLE statement."))
784 ;; Here's a real warhorse of a function!
786 (declaim (inline listify))
792 (defmethod output-sql ((stmt sql-create-table) database)
793 (flet ((output-column (column-spec)
794 (destructuring-bind (name type &optional db-type &rest constraints)
796 (let ((type (listify type)))
797 (output-sql name database)
798 (write-char #\Space *sql-stream*)
800 (if (stringp db-type) db-type ; override definition
801 (database-get-type-specifier (car type) (cdr type) database
802 (database-underlying-type database)))
804 (let ((constraints (database-constraint-statement
805 (if (and db-type (symbolp db-type))
806 (cons db-type constraints)
810 (write-string " " *sql-stream*)
811 (write-string constraints *sql-stream*)))))))
812 (with-slots (name columns modifiers transactions)
814 (write-string "CREATE TABLE " *sql-stream*)
816 (string (format *sql-stream* "~s" (sql-escape name)))
817 (symbol (write-string (sql-escape name) *sql-stream*))
818 (sql-ident (output-sql name database)))
819 (write-string " (" *sql-stream*)
820 (do ((column columns (cdr column)))
822 (output-column (car column)))
823 (output-column (car column))
824 (write-string ", " *sql-stream*))
826 (do ((modifier (listify modifiers) (cdr modifier)))
828 (write-string ", " *sql-stream*)
829 (write-string (car modifier) *sql-stream*)))
830 (write-char #\) *sql-stream*)
831 (when (and (eq :mysql (database-underlying-type database))
833 (db-type-transaction-capable? :mysql database))
834 (write-string " Type=InnoDB" *sql-stream*))))
840 (defclass sql-create-view (%sql-expression)
841 ((name :initarg :name :initform nil)
842 (column-list :initarg :column-list :initform nil)
843 (query :initarg :query :initform nil)
844 (with-check-option :initarg :with-check-option :initform nil))
845 (:documentation "An SQL CREATE VIEW statement."))
847 (defmethod output-sql ((stmt sql-create-view) database)
848 (with-slots (name column-list query with-check-option) stmt
849 (write-string "CREATE VIEW " *sql-stream*)
850 (output-sql name database)
851 (when column-list (write-string " " *sql-stream*)
852 (output-sql (listify column-list) database))
853 (write-string " AS " *sql-stream*)
854 (output-sql query database)
855 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
859 ;; DATABASE-OUTPUT-SQL
862 (defmethod database-output-sql ((str string) database)
863 (declare (optimize (speed 3) (safety 1)
864 #+cmu (extensions:inhibit-warnings 3)))
865 (let ((len (length str)))
866 (declare (type fixnum len))
869 ((and (null (position #\' str))
870 (null (position #\\ str)))
871 (concatenate 'string "'" str "'"))
873 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
874 (declare (simple-string buf))
877 ((= i len) (subseq buf 0 (1+ j)))
878 (declare (type fixnum i j))
879 (let ((char (aref str i)))
880 (declare (character char))
881 (cond ((char= char #\')
882 (setf (aref buf j) #\')
884 (setf (aref buf j) #\'))
885 ((and (char= char #\\)
886 ;; MTP: only escape backslash with pgsql/mysql
887 (member (database-underlying-type database)
888 '(:postgresql :mysql)
890 (setf (aref buf j) #\\)
892 (setf (aref buf j) #\\))
894 (setf (aref buf j) char))))))))))
896 (let ((keyword-package (symbol-package :foo)))
897 (defmethod database-output-sql ((sym symbol) database)
900 (if (equal (symbol-package sym) keyword-package)
901 (concatenate 'string "'" (string sym) "'")
902 (symbol-name sym)))))
904 (defmethod database-output-sql ((tee (eql t)) database)
906 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
909 (string (format nil "'~A'" val))
910 (integer (format nil "~A" val)))))
913 #+nil(defmethod database-output-sql ((tee (eql t)) database)
914 (declare (ignore database))
917 (defmethod database-output-sql ((num number) database)
918 (declare (ignore database))
919 (number-to-sql-string num))
921 (defmethod database-output-sql ((arg list) database)
924 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
925 (sql-output val database))
928 (defmethod database-output-sql ((arg vector) database)
929 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
930 (sql-output val database))
933 (defmethod output-sql-hash-key ((arg vector) database)
934 (list 'vector (map 'list (lambda (arg)
935 (or (output-sql-hash-key arg database)
936 (return-from output-sql-hash-key nil)))
939 (defmethod database-output-sql ((self wall-time) database)
940 (declare (ignore database))
941 (db-timestring self))
943 (defmethod database-output-sql ((self date) database)
944 (declare (ignore database))
945 (db-datestring self))
947 (defmethod database-output-sql ((self duration) database)
948 (declare (ignore database))
949 (format nil "'~a'" (duration-timestring self)))
952 (defmethod database-output-sql ((self money) database)
953 (database-output-sql (slot-value self 'odcl::units) database))
955 (defmethod database-output-sql (thing database)
959 (error 'sql-user-error
962 "No type conversion to SQL for ~A is defined for DB ~A."
963 (type-of thing) (type-of database)))))
967 ;; Column constraint types and conversion to SQL
970 (defparameter *constraint-types*
972 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
973 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
974 (cons (symbol-name-default-case "NOT") "NOT")
975 (cons (symbol-name-default-case "NULL") "NULL")
976 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
977 (cons (symbol-name-default-case "KEY") "KEY")
978 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
979 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
980 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
981 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
982 (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;Added Identity for MS-SQLServer support
985 (defmethod database-constraint-statement (constraint-list database)
986 (declare (ignore database))
987 (make-constraints-description constraint-list))
989 (defun make-constraints-description (constraint-list)
992 (do ((constraint constraint-list (cdr constraint)))
993 ((null constraint) string)
994 (let ((output (assoc (symbol-name (car constraint))
998 (error 'sql-user-error
999 :message (format nil "unsupported column constraint '~A'"
1001 (setq string (concatenate 'string string (cdr output))))
1002 (if (< 1 (length constraint))
1003 (setq string (concatenate 'string string " "))))))))