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)
113 (with-slots (name) expr
117 (symbol (symbol-name name)))
121 ;; For SQL Identifiers for attributes
123 (defclass sql-ident-attribute (sql-ident)
126 :initform +null-string+)
129 :initform +null-string+))
130 (:documentation "An SQL Attribute identifier."))
132 (defmethod collect-table-refs (sql)
133 (declare (ignore sql))
136 (defmethod collect-table-refs ((sql sql-ident-attribute))
137 (let ((qual (slot-value sql 'qualifier)))
139 (list (make-instance 'sql-ident-table :name qual)))))
141 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
142 (declare (ignore environment))
143 (with-slots (qualifier type name)
145 `(make-instance 'sql-ident-attribute :name ',name
146 :qualifier ',qualifier
149 (defmethod output-sql ((expr sql-ident-attribute) database)
150 (with-slots (qualifier name type) expr
151 (if (and (not qualifier) (not type))
154 (write-string name *sql-stream*))
157 (sql-escape (symbol-name name)) *sql-stream*)))
159 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
160 ;;; should not be output in SQL statements
162 (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
164 (sql-escape qualifier))
168 (format *sql-stream* "~@[~A.~]~A"
171 (string (format nil "~s" qualifier))
172 (t (sql-escape qualifier))))
174 (string (format nil "~s" (sql-escape name)))
175 (t (sql-escape name)))))
178 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
179 (with-slots (qualifier name type)
181 (list (and database (database-underlying-type database))
182 'sql-ident-attribute qualifier name type)))
184 ;; For SQL Identifiers for tables
186 (defclass sql-ident-table (sql-ident)
188 :initarg :table-alias :initform nil))
189 (:documentation "An SQL table identifier."))
191 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
192 (declare (ignore environment))
193 (with-slots (alias name)
195 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
197 (defmethod output-sql ((expr sql-ident-table) database)
198 (with-slots (name alias) expr
201 (format *sql-stream* "~s" (sql-escape name)))
203 (write-string (sql-escape name) *sql-stream*)))
205 (format *sql-stream* " ~s" alias)))
208 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
209 (with-slots (name alias)
211 (list (and database (database-underlying-type database))
212 'sql-ident-table name alias)))
214 (defclass sql-relational-exp (%sql-expression)
219 :initarg :sub-expressions
221 (:documentation "An SQL relational expression."))
223 (defmethod make-load-form ((self sql-relational-exp) &optional environment)
224 (make-load-form-saving-slots self
225 :slot-names '(operator sub-expressions)
226 :environment environment))
228 (defmethod collect-table-refs ((sql sql-relational-exp))
230 (dolist (exp (slot-value sql 'sub-expressions))
231 (let ((refs (collect-table-refs exp)))
232 (if refs (setf tabs (append refs tabs)))))
233 (remove-duplicates tabs
234 :test (lambda (tab1 tab2)
235 (equal (slot-value tab1 'name)
236 (slot-value tab2 'name))))))
241 ;; Write SQL for relational operators (like 'AND' and 'OR').
242 ;; should do arity checking of subexpressions
244 (defmethod output-sql ((expr sql-relational-exp) database)
245 (with-slots (operator sub-expressions) expr
246 ;; we do this as two runs so as not to emit confusing superflous parentheses
247 ;; The first loop renders all the child outputs so that we can skip anding with
248 ;; empty output (which causes sql errors)
249 ;; the next loop simply emits each sub-expression with the appropriate number of
250 ;; parens and operators
252 (string-trim '(#\space #\newline #\return #\tab
253 ;; sbcl, allegrocl, and clisp use #\no-break_space
254 ;; lispworks uses #\no-break-space
255 #-lispworks #\no-break_space
256 #+lispworks #\no-break-space
258 (with-output-to-string (*sql-stream*)
259 (output-sql sub database)))))
260 (let ((str-subs (loop for sub in sub-expressions
261 for str-sub = (trim sub)
262 when (and str-sub (> (length str-sub) 0))
264 (case (length str-subs)
266 (1 (write-string (first str-subs) *sql-stream*))
268 (write-char #\( *sql-stream*)
269 (write-string (first str-subs) *sql-stream*)
270 (loop for str-sub in (rest str-subs)
272 (write-char #\Space *sql-stream*)
273 (output-sql operator database)
274 (write-char #\Space *sql-stream*)
275 (write-string str-sub *sql-stream*))
276 (write-char #\) *sql-stream*))
280 (defclass sql-upcase-like (sql-relational-exp)
282 (:documentation "An SQL 'like' that upcases its arguments."))
284 (defmethod output-sql ((expr sql-upcase-like) database)
285 (flet ((write-term (term)
286 (write-string "upper(" *sql-stream*)
287 (output-sql term database)
288 (write-char #\) *sql-stream*)))
289 (with-slots (sub-expressions)
291 (let ((subs (if (consp (car sub-expressions))
292 (car sub-expressions)
294 (write-char #\( *sql-stream*)
295 (do ((sub subs (cdr sub)))
296 ((null (cdr sub)) (write-term (car sub)))
297 (write-term (car sub))
298 (write-string " LIKE " *sql-stream*))
299 (write-char #\) *sql-stream*))))
302 (defclass sql-assignment-exp (sql-relational-exp)
304 (:documentation "An SQL Assignment expression."))
307 (defmethod output-sql ((expr sql-assignment-exp) database)
308 (with-slots (operator sub-expressions)
310 (do ((sub sub-expressions (cdr sub)))
311 ((null (cdr sub)) (output-sql (car sub) database))
312 (output-sql (car sub) database)
313 (write-char #\Space *sql-stream*)
314 (output-sql operator database)
315 (write-char #\Space *sql-stream*)))
318 (defclass sql-value-exp (%sql-expression)
326 "An SQL value expression.")
329 (defmethod collect-table-refs ((sql sql-value-exp))
331 (if (listp (slot-value sql 'components))
333 (dolist (exp (slot-value sql 'components))
334 (let ((refs (collect-table-refs exp)))
335 (if refs (setf tabs (append refs tabs)))))
336 (remove-duplicates tabs
337 :test (lambda (tab1 tab2)
338 (equal (slot-value tab1 'name)
339 (slot-value tab2 'name)))))
344 (defmethod output-sql ((expr sql-value-exp) database)
345 (with-slots (modifier components)
349 (write-char #\( *sql-stream*)
350 (output-sql modifier database)
351 (write-char #\Space *sql-stream*)
352 (output-sql components database)
353 (write-char #\) *sql-stream*))
354 (output-sql components database))))
356 (defclass sql-typecast-exp (sql-value-exp)
358 (:documentation "An SQL typecast expression."))
360 (defmethod output-sql ((expr sql-typecast-exp) database)
361 (with-slots (components)
363 (output-sql components database)))
365 (defmethod collect-table-refs ((sql sql-typecast-exp))
366 (when (slot-value sql 'components)
367 (collect-table-refs (slot-value sql 'components))))
369 (defclass sql-function-exp (%sql-expression)
377 "An SQL function expression."))
379 (defmethod collect-table-refs ((sql sql-function-exp))
381 (dolist (exp (slot-value sql 'args))
382 (let ((refs (collect-table-refs exp)))
383 (if refs (setf tabs (append refs tabs)))))
384 (remove-duplicates tabs
385 :test (lambda (tab1 tab2)
386 (equal (slot-value tab1 'name)
387 (slot-value tab2 'name))))))
388 (defvar *in-subselect* nil)
390 (defmethod output-sql ((expr sql-function-exp) database)
391 (with-slots (name args)
393 (output-sql name database)
394 (let ((*in-subselect* nil)) ;; aboid double parens
395 (when args (output-sql args database))))
399 (defclass sql-between-exp (sql-function-exp)
401 (:documentation "An SQL between expression."))
403 (defmethod output-sql ((expr sql-between-exp) database)
406 (output-sql (first args) database)
407 (write-string " BETWEEN " *sql-stream*)
408 (output-sql (second args) database)
409 (write-string " AND " *sql-stream*)
410 (output-sql (third args) database))
413 (defclass sql-query-modifier-exp (%sql-expression)
414 ((modifier :initarg :modifier :initform nil)
415 (components :initarg :components :initform nil))
416 (:documentation "An SQL query modifier expression."))
418 (defmethod output-sql ((expr sql-query-modifier-exp) database)
419 (with-slots (modifier components)
421 (output-sql modifier database)
422 (write-string " " *sql-stream*)
423 (output-sql (car components) database)
425 (mapc #'(lambda (comp)
426 (write-string ", " *sql-stream*)
427 (output-sql comp database))
431 (defclass sql-set-exp (%sql-expression)
436 :initarg :sub-expressions
438 (:documentation "An SQL set expression."))
440 (defmethod collect-table-refs ((sql sql-set-exp))
442 (dolist (exp (slot-value sql 'sub-expressions))
443 (let ((refs (collect-table-refs exp)))
444 (if refs (setf tabs (append refs tabs)))))
445 (remove-duplicates tabs
446 :test (lambda (tab1 tab2)
447 (equal (slot-value tab1 'name)
448 (slot-value tab2 'name))))))
450 (defmethod output-sql ((expr sql-set-exp) database)
451 (with-slots (operator sub-expressions)
453 (let ((subs (if (consp (car sub-expressions))
454 (car sub-expressions)
456 (when (= (length subs) 1)
457 (output-sql operator database)
458 (write-char #\Space *sql-stream*))
459 (do ((sub subs (cdr sub)))
460 ((null (cdr sub)) (output-sql (car sub) database))
461 (output-sql (car sub) database)
462 (write-char #\Space *sql-stream*)
463 (output-sql operator database)
464 (write-char #\Space *sql-stream*))))
467 (defclass sql-query (%sql-expression)
478 :initarg :set-operation
510 (:documentation "An SQL SELECT query."))
512 (defclass sql-object-query (%sql-expression)
526 (defmethod collect-table-refs ((sql sql-query))
527 (remove-duplicates (collect-table-refs (slot-value sql 'where))
528 :test (lambda (tab1 tab2)
529 (equal (slot-value tab1 'name)
530 (slot-value tab2 'name)))))
532 (defvar *select-arguments*
533 '(:all :database :distinct :flatp :from :group-by :having :order-by
534 :set-operation :where :offset :limit :inner-join :on
535 ;; below keywords are not a SQL argument, but these keywords may terminate select
538 (defun query-arg-p (sym)
539 (member sym *select-arguments*))
541 (defun query-get-selections (select-args)
542 "Return two values: the list of select-args up to the first keyword,
543 uninclusive, and the args from that keyword to the end."
544 (let ((first-key-arg (position-if #'query-arg-p select-args)))
546 (values (subseq select-args 0 first-key-arg)
547 (subseq select-args first-key-arg))
550 (defun make-query (&rest args)
551 (flet ((select-objects (target-args)
553 (every #'(lambda (arg)
555 (find-class arg nil)))
557 (multiple-value-bind (selections arglist)
558 (query-get-selections args)
559 (if (select-objects selections)
560 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
561 (make-instance 'sql-object-query :objects selections
562 :flatp flatp :refresh refresh
564 (destructuring-bind (&key all flatp set-operation distinct from where
565 group-by having order-by
566 offset limit inner-join on &allow-other-keys)
568 (if (null selections)
569 (error "No target columns supplied to select statement."))
571 (error "No source tables supplied to select statement."))
572 (make-instance 'sql-query :selections selections
573 :all all :flatp flatp :set-operation set-operation
574 :distinct distinct :from from :where where
575 :limit limit :offset offset
576 :group-by group-by :having having :order-by order-by
577 :inner-join inner-join :on on))))))
579 (defmethod output-sql ((query sql-query) database)
580 (with-slots (distinct selections from where group-by having order-by
581 limit offset inner-join on all set-operation)
584 (write-string "(" *sql-stream*))
585 (write-string "SELECT " *sql-stream*)
587 (write-string "ALL " *sql-stream*))
588 (when (and distinct (not all))
589 (write-string "DISTINCT " *sql-stream*)
590 (unless (eql t distinct)
591 (write-string "ON " *sql-stream*)
592 (output-sql distinct database)
593 (write-char #\Space *sql-stream*)))
594 (let ((*in-subselect* t))
595 (output-sql (apply #'vector selections) database))
597 (write-string " FROM " *sql-stream*)
598 (flet ((ident-table-equal (a b)
599 (and (if (and (eql (type-of a) 'sql-ident-table)
600 (eql (type-of b) 'sql-ident-table))
601 (string-equal (slot-value a 'alias)
602 (slot-value b 'alias))
604 (string-equal (sql-escape (slot-value a 'name))
605 (sql-escape (slot-value b 'name))))))
607 (list (output-sql (apply #'vector
608 (remove-duplicates from
609 :test #'ident-table-equal))
611 (string (format *sql-stream* "~s" (sql-escape from)))
612 (t (let ((*in-subselect* t))
613 (output-sql from database))))))
615 (write-string " INNER JOIN " *sql-stream*)
616 (output-sql inner-join database))
618 (write-string " ON " *sql-stream*)
619 (output-sql on database))
621 (write-string " WHERE " *sql-stream*)
622 (let ((*in-subselect* t))
623 (output-sql where database)))
625 (write-string " GROUP BY " *sql-stream*)
627 (do ((order group-by (cdr order)))
629 (let ((item (car order)))
632 (output-sql (car item) database)
633 (format *sql-stream* " ~A" (cadr item)))
635 (output-sql item database)))
637 (write-char #\, *sql-stream*))))
638 (output-sql group-by database)))
640 (write-string " HAVING " *sql-stream*)
641 (output-sql having database))
643 (write-string " ORDER BY " *sql-stream*)
645 (do ((order order-by (cdr order)))
647 (let ((item (car order)))
650 (output-sql (car item) database)
651 (format *sql-stream* " ~A" (cadr item)))
653 (output-sql item database)))
655 (write-char #\, *sql-stream*))))
656 (output-sql order-by database)))
658 (write-string " LIMIT " *sql-stream*)
659 (output-sql limit database))
661 (write-string " OFFSET " *sql-stream*)
662 (output-sql offset database))
664 (write-string ")" *sql-stream*))
666 (write-char #\Space *sql-stream*)
667 (output-sql set-operation database)))
670 (defmethod output-sql ((query sql-object-query) database)
671 (declare (ignore database))
672 (with-slots (objects)
675 (format *sql-stream* "(~{~A~^ ~})" objects))))
680 (defclass sql-insert (%sql-expression)
694 "An SQL INSERT statement."))
696 (defmethod output-sql ((ins sql-insert) database)
697 (with-slots (into attributes values query)
699 (write-string "INSERT INTO " *sql-stream*)
702 (string (sql-expression :table into))
706 (write-char #\Space *sql-stream*)
707 (output-sql attributes database))
709 (write-string " VALUES " *sql-stream*)
710 (output-sql values database))
712 (write-char #\Space *sql-stream*)
713 (output-sql query database)))
718 (defclass sql-delete (%sql-expression)
726 "An SQL DELETE statement."))
728 (defmethod output-sql ((stmt sql-delete) database)
729 (with-slots (from where)
731 (write-string "DELETE FROM " *sql-stream*)
733 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
734 (t (output-sql from database)))
736 (write-string " WHERE " *sql-stream*)
737 (output-sql where database)))
742 (defclass sql-update (%sql-expression)
755 (:documentation "An SQL UPDATE statement."))
757 (defmethod output-sql ((expr sql-update) database)
758 (with-slots (table where attributes values)
760 (flet ((update-assignments ()
761 (mapcar #'(lambda (a b)
762 (make-instance 'sql-assignment-exp
764 :sub-expressions (list a b)))
766 (write-string "UPDATE " *sql-stream*)
767 (output-sql table database)
768 (write-string " SET " *sql-stream*)
769 (output-sql (apply #'vector (update-assignments)) database)
771 (write-string " WHERE " *sql-stream*)
772 (output-sql where database))))
777 (defclass sql-create-table (%sql-expression)
788 :initarg :transactions
791 "An SQL CREATE TABLE statement."))
793 ;; Here's a real warhorse of a function!
795 (declaim (inline listify))
801 (defmethod output-sql ((stmt sql-create-table) database)
802 (flet ((output-column (column-spec)
803 (destructuring-bind (name type &optional db-type &rest constraints)
805 (let ((type (listify type)))
806 (output-sql name database)
807 (write-char #\Space *sql-stream*)
809 (if (stringp db-type) db-type ; override definition
810 (database-get-type-specifier (car type) (cdr type) database
811 (database-underlying-type database)))
813 (let ((constraints (database-constraint-statement
814 (if (and db-type (symbolp db-type))
815 (cons db-type constraints)
819 (write-string " " *sql-stream*)
820 (write-string constraints *sql-stream*)))))))
821 (with-slots (name columns modifiers transactions)
823 (write-string "CREATE TABLE " *sql-stream*)
825 (string (format *sql-stream* "~s" (sql-escape name)))
826 (symbol (write-string (sql-escape name) *sql-stream*))
827 (sql-ident (output-sql name database)))
828 (write-string " (" *sql-stream*)
829 (do ((column columns (cdr column)))
831 (output-column (car column)))
832 (output-column (car column))
833 (write-string ", " *sql-stream*))
835 (do ((modifier (listify modifiers) (cdr modifier)))
837 (write-string ", " *sql-stream*)
838 (write-string (car modifier) *sql-stream*)))
839 (write-char #\) *sql-stream*)
840 (when (and (eq :mysql (database-underlying-type database))
842 (db-type-transaction-capable? :mysql database))
843 (write-string " Type=InnoDB" *sql-stream*))))
849 (defclass sql-create-view (%sql-expression)
850 ((name :initarg :name :initform nil)
851 (column-list :initarg :column-list :initform nil)
852 (query :initarg :query :initform nil)
853 (with-check-option :initarg :with-check-option :initform nil))
854 (:documentation "An SQL CREATE VIEW statement."))
856 (defmethod output-sql ((stmt sql-create-view) database)
857 (with-slots (name column-list query with-check-option) stmt
858 (write-string "CREATE VIEW " *sql-stream*)
859 (output-sql name database)
860 (when column-list (write-string " " *sql-stream*)
861 (output-sql (listify column-list) database))
862 (write-string " AS " *sql-stream*)
863 (output-sql query database)
864 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
868 ;; DATABASE-OUTPUT-SQL
871 (defmethod database-output-sql ((str string) database)
872 (declare (optimize (speed 3) (safety 1)
873 #+cmu (extensions:inhibit-warnings 3)))
874 (let ((len (length str)))
875 (declare (type fixnum len))
878 ((and (null (position #\' str))
879 (null (position #\\ str)))
880 (concatenate 'string "'" str "'"))
882 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
883 (declare (simple-string buf))
886 ((= i len) (subseq buf 0 (1+ j)))
887 (declare (type fixnum i j))
888 (let ((char (aref str i)))
889 (declare (character char))
890 (cond ((char= char #\')
891 (setf (aref buf j) #\')
893 (setf (aref buf j) #\'))
894 ((and (char= char #\\)
895 ;; MTP: only escape backslash with pgsql/mysql
896 (member (database-underlying-type database)
897 '(:postgresql :mysql)
899 (setf (aref buf j) #\\)
901 (setf (aref buf j) #\\))
903 (setf (aref buf j) char))))))))))
905 (let ((keyword-package (symbol-package :foo)))
906 (defmethod database-output-sql ((sym symbol) database)
909 (if (equal (symbol-package sym) keyword-package)
910 (concatenate 'string "'" (string sym) "'")
911 (symbol-name sym)))))
913 (defmethod database-output-sql ((tee (eql t)) database)
915 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
918 (string (format nil "'~A'" val))
919 (integer (format nil "~A" val)))))
922 #+nil(defmethod database-output-sql ((tee (eql t)) database)
923 (declare (ignore database))
926 (defmethod database-output-sql ((num number) database)
927 (declare (ignore database))
928 (number-to-sql-string num))
930 (defmethod database-output-sql ((arg list) database)
933 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
934 (sql-output val database))
937 (defmethod database-output-sql ((arg vector) database)
938 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
939 (sql-output val database))
942 (defmethod output-sql-hash-key ((arg vector) database)
943 (list 'vector (map 'list (lambda (arg)
944 (or (output-sql-hash-key arg database)
945 (return-from output-sql-hash-key nil)))
948 (defmethod database-output-sql ((self wall-time) database)
949 (declare (ignore database))
950 (db-timestring self))
952 (defmethod database-output-sql ((self date) database)
953 (declare (ignore database))
954 (db-datestring self))
956 (defmethod database-output-sql ((self duration) database)
957 (declare (ignore database))
958 (format nil "'~a'" (duration-timestring self)))
961 (defmethod database-output-sql ((self money) database)
962 (database-output-sql (slot-value self 'odcl::units) database))
964 (defmethod database-output-sql (thing database)
968 (error 'sql-user-error
971 "No type conversion to SQL for ~A is defined for DB ~A."
972 (type-of thing) (type-of database)))))
976 ;; Column constraint types and conversion to SQL
979 (defparameter *constraint-types*
981 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
982 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
983 (cons (symbol-name-default-case "NOT") "NOT")
984 (cons (symbol-name-default-case "NULL") "NULL")
985 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
986 (cons (symbol-name-default-case "KEY") "KEY")
987 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
988 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
989 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
990 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")))
992 (defmethod database-constraint-statement (constraint-list database)
993 (declare (ignore database))
994 (make-constraints-description constraint-list))
996 (defun make-constraints-description (constraint-list)
999 (do ((constraint constraint-list (cdr constraint)))
1000 ((null constraint) string)
1001 (let ((output (assoc (symbol-name (car constraint))
1005 (error 'sql-user-error
1006 :message (format nil "unsupported column constraint '~A'"
1008 (setq string (concatenate 'string string (cdr output))))
1009 (if (< 1 (length constraint))
1010 (setq string (concatenate 'string string " "))))))))