1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
4 ;;;; Classes defining SQL expressions and methods for formatting the
5 ;;;; appropriate SQL commands.
7 ;;;; This file is part of CLSQL.
9 ;;;; CLSQL users are granted the rights to distribute and use this software
10 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
11 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
12 ;;;; *************************************************************************
14 (in-package #:clsql-sys)
16 (defvar +empty-string+ "''")
18 (defvar +null-string+ "NULL")
20 (defvar *sql-stream* nil
21 "stream which accumulates SQL output")
23 (defun sql-output (sql-expr &optional (database *default-database*))
24 "Top-level call for generating SQL strings. Returns an SQL
25 string appropriate for DATABASE which corresponds to the
26 supplied lisp expression SQL-EXPR."
27 (progv '(*sql-stream*)
28 `(,(make-string-output-stream))
29 (output-sql sql-expr database)
30 (get-output-stream-string *sql-stream*)))
32 (defmethod output-sql (expr database)
33 (write-string (database-output-sql expr database) *sql-stream*)
36 (defvar *output-hash* (make-hash-table :test #'equal)
37 "For caching generated SQL strings.")
39 (defmethod output-sql :around ((sql t) database)
40 (let* ((hash-key (output-sql-hash-key sql database))
41 (hash-value (when hash-key (gethash hash-key *output-hash*))))
42 (cond ((and hash-key hash-value)
43 (write-string hash-value *sql-stream*))
45 (let ((*sql-stream* (make-string-output-stream)))
47 (setf hash-value (get-output-stream-string *sql-stream*))
48 (setf (gethash hash-key *output-hash*) hash-value))
49 (write-string hash-value *sql-stream*))
51 (call-next-method)))))
53 (defmethod output-sql-hash-key (expr database)
54 (declare (ignore expr database))
58 (defclass %sql-expression ()
61 (defmethod output-sql ((expr %sql-expression) database)
62 (declare (ignore database))
63 (write-string +null-string+ *sql-stream*))
65 (defmethod print-object ((self %sql-expression) stream)
66 (print-unreadable-object
68 (write-string (sql-output self) stream))
71 ;; For straight up strings
73 (defclass sql (%sql-expression)
77 (:documentation "A literal SQL expression."))
79 (defmethod make-load-form ((sql sql) &optional environment)
80 (declare (ignore environment))
83 `(make-instance 'sql :string ',text)))
85 (defmethod output-sql ((expr sql) database)
86 (declare (ignore database))
87 (write-string (slot-value expr 'text) *sql-stream*)
90 (defmethod print-object ((ident sql) stream)
91 (format stream "#<~S \"~A\">"
93 (sql-output ident nil))
96 ;; For SQL Identifiers of generic type
98 (defclass sql-ident (%sql-expression)
101 :initform +null-string+))
102 (:documentation "An SQL identifer."))
104 (defmethod make-load-form ((sql sql-ident) &optional environment)
105 (declare (ignore environment))
108 `(make-instance 'sql-ident :name ',name)))
110 (defmethod output-sql ((expr sql-ident) database)
111 (with-slots (name) expr
115 (symbol (symbol-name name)))
119 ;; For SQL Identifiers for attributes
121 (defclass sql-ident-attribute (sql-ident)
124 :initform +null-string+)
127 :initform +null-string+))
128 (:documentation "An SQL Attribute identifier."))
130 (defmethod collect-table-refs (sql)
131 (declare (ignore sql))
134 (defmethod collect-table-refs ((sql sql-ident-attribute))
135 (let ((qual (slot-value sql 'qualifier)))
137 (list (make-instance 'sql-ident-table :name qual)))))
139 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
140 (declare (ignore environment))
141 (with-slots (qualifier type name)
143 `(make-instance 'sql-ident-attribute :name ',name
144 :qualifier ',qualifier
147 (defmethod output-sql ((expr sql-ident-attribute) database)
148 (with-slots (qualifier name type) expr
149 (if (and (not qualifier) (not type))
152 (write-string name *sql-stream*))
155 (sql-escape (symbol-name name)) *sql-stream*)))
157 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
158 ;;; should not be output in SQL statements
160 (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
162 (sql-escape qualifier))
166 (format *sql-stream* "~@[~A.~]~A"
169 (string (format nil "~s" qualifier))
170 (t (sql-escape qualifier))))
172 (string (format nil "~s" (sql-escape name)))
173 (t (sql-escape name)))))
176 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
177 (with-slots (qualifier name type)
179 (list (and database (database-underlying-type database))
180 'sql-ident-attribute qualifier name type)))
182 ;; For SQL Identifiers for tables
184 (defclass sql-ident-table (sql-ident)
186 :initarg :table-alias :initform nil))
187 (:documentation "An SQL table identifier."))
189 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
190 (declare (ignore environment))
191 (with-slots (alias name)
193 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
195 (defun special-char-p (s)
196 "Check if a string has any special characters"
197 (loop for char across s
198 thereis (find char '(#\space #\, #\. #\! #\@ #\# #\$ #\%
199 #\^ #\& #\* #\| #\( #\) #\- #\+))))
201 (defmethod output-sql ((expr sql-ident-table) database)
202 (with-slots (name alias) expr
203 (flet ((p (s) ;; the etypecase is in sql-escape too
204 (let ((sym? (symbolp s))
207 (if (and sym? (not (special-char-p s)))
212 (princ #\space *sql-stream*)
216 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
217 (with-slots (name alias)
219 (list (and database (database-underlying-type database))
220 'sql-ident-table name alias)))
222 (defclass sql-relational-exp (%sql-expression)
227 :initarg :sub-expressions
229 (:documentation "An SQL relational expression."))
231 (defmethod make-load-form ((self sql-relational-exp) &optional environment)
232 (make-load-form-saving-slots self
233 :slot-names '(operator sub-expressions)
234 :environment environment))
236 (defmethod collect-table-refs ((sql sql-relational-exp))
238 (dolist (exp (slot-value sql 'sub-expressions))
239 (let ((refs (collect-table-refs exp)))
240 (if refs (setf tabs (append refs tabs)))))
241 (remove-duplicates tabs
242 :test (lambda (tab1 tab2)
243 (equal (slot-value tab1 'name)
244 (slot-value tab2 'name))))))
249 ;; Write SQL for relational operators (like 'AND' and 'OR').
250 ;; should do arity checking of subexpressions
252 (defmethod output-sql ((expr sql-relational-exp) database)
253 (with-slots (operator sub-expressions) expr
254 ;; we do this as two runs so as not to emit confusing superflous parentheses
255 ;; The first loop renders all the child outputs so that we can skip anding with
256 ;; empty output (which causes sql errors)
257 ;; the next loop simply emits each sub-expression with the appropriate number of
258 ;; parens and operators
260 (string-trim +whitespace-chars+
261 (with-output-to-string (*sql-stream*)
262 (output-sql sub database)))))
263 (let ((str-subs (loop for sub in sub-expressions
264 for str-sub = (trim sub)
265 when (and str-sub (> (length str-sub) 0))
267 (case (length str-subs)
269 (1 (write-string (first str-subs) *sql-stream*))
271 (write-char #\( *sql-stream*)
272 (write-string (first str-subs) *sql-stream*)
273 (loop for str-sub in (rest str-subs)
275 (write-char #\Space *sql-stream*)
276 (output-sql operator database)
277 (write-char #\Space *sql-stream*)
278 (write-string str-sub *sql-stream*))
279 (write-char #\) *sql-stream*))
283 (defclass sql-upcase-like (sql-relational-exp)
285 (:documentation "An SQL 'like' that upcases its arguments."))
287 (defmethod output-sql ((expr sql-upcase-like) database)
288 (flet ((write-term (term)
289 (write-string "upper(" *sql-stream*)
290 (output-sql term database)
291 (write-char #\) *sql-stream*)))
292 (with-slots (sub-expressions)
294 (let ((subs (if (consp (car sub-expressions))
295 (car sub-expressions)
297 (write-char #\( *sql-stream*)
298 (do ((sub subs (cdr sub)))
299 ((null (cdr sub)) (write-term (car sub)))
300 (write-term (car sub))
301 (write-string " LIKE " *sql-stream*))
302 (write-char #\) *sql-stream*))))
305 (defclass sql-assignment-exp (sql-relational-exp)
307 (:documentation "An SQL Assignment expression."))
310 (defmethod output-sql ((expr sql-assignment-exp) database)
311 (with-slots (operator sub-expressions)
313 (do ((sub sub-expressions (cdr sub)))
314 ((null (cdr sub)) (output-sql (car sub) database))
315 (output-sql (car sub) database)
316 (write-char #\Space *sql-stream*)
317 (output-sql operator database)
318 (write-char #\Space *sql-stream*)))
321 (defclass sql-value-exp (%sql-expression)
329 "An SQL value expression.")
332 (defmethod collect-table-refs ((sql sql-value-exp))
334 (if (listp (slot-value sql 'components))
336 (dolist (exp (slot-value sql 'components))
337 (let ((refs (collect-table-refs exp)))
338 (if refs (setf tabs (append refs tabs)))))
339 (remove-duplicates tabs
340 :test (lambda (tab1 tab2)
341 (equal (slot-value tab1 'name)
342 (slot-value tab2 'name)))))
347 (defmethod output-sql ((expr sql-value-exp) database)
348 (with-slots (modifier components)
352 (write-char #\( *sql-stream*)
353 (output-sql modifier database)
354 (write-char #\Space *sql-stream*)
355 (output-sql components database)
356 (write-char #\) *sql-stream*))
357 (output-sql components database))))
359 (defclass sql-typecast-exp (sql-value-exp)
361 (:documentation "An SQL typecast expression."))
363 (defmethod output-sql ((expr sql-typecast-exp) database)
364 (with-slots (components)
366 (output-sql components database)))
368 (defmethod collect-table-refs ((sql sql-typecast-exp))
369 (when (slot-value sql 'components)
370 (collect-table-refs (slot-value sql 'components))))
372 (defclass sql-function-exp (%sql-expression)
380 "An SQL function expression."))
382 (defmethod collect-table-refs ((sql sql-function-exp))
384 (dolist (exp (slot-value sql 'args))
385 (let ((refs (collect-table-refs exp)))
386 (if refs (setf tabs (append refs tabs)))))
387 (remove-duplicates tabs
388 :test (lambda (tab1 tab2)
389 (equal (slot-value tab1 'name)
390 (slot-value tab2 'name))))))
391 (defvar *in-subselect* nil)
393 (defmethod output-sql ((expr sql-function-exp) database)
394 (with-slots (name args)
396 (output-sql name database)
397 (let ((*in-subselect* nil)) ;; aboid double parens
398 (when args (output-sql args database))))
402 (defclass sql-between-exp (sql-function-exp)
404 (:documentation "An SQL between expression."))
406 (defmethod output-sql ((expr sql-between-exp) database)
409 (output-sql (first args) database)
410 (write-string " BETWEEN " *sql-stream*)
411 (output-sql (second args) database)
412 (write-string " AND " *sql-stream*)
413 (output-sql (third args) database))
416 (defclass sql-query-modifier-exp (%sql-expression)
417 ((modifier :initarg :modifier :initform nil)
418 (components :initarg :components :initform nil))
419 (:documentation "An SQL query modifier expression."))
421 (defmethod output-sql ((expr sql-query-modifier-exp) database)
422 (with-slots (modifier components)
424 (output-sql modifier database)
425 (write-string " " *sql-stream*)
426 (output-sql (car components) database)
428 (mapc #'(lambda (comp)
429 (write-string ", " *sql-stream*)
430 (output-sql comp database))
434 (defclass sql-set-exp (%sql-expression)
439 :initarg :sub-expressions
441 (:documentation "An SQL set expression."))
443 (defmethod collect-table-refs ((sql sql-set-exp))
445 (dolist (exp (slot-value sql 'sub-expressions))
446 (let ((refs (collect-table-refs exp)))
447 (if refs (setf tabs (append refs tabs)))))
448 (remove-duplicates tabs
449 :test (lambda (tab1 tab2)
450 (equal (slot-value tab1 'name)
451 (slot-value tab2 'name))))))
453 (defmethod output-sql ((expr sql-set-exp) database)
454 (with-slots (operator sub-expressions)
456 (let ((subs (if (consp (car sub-expressions))
457 (car sub-expressions)
459 (when (= (length subs) 1)
460 (output-sql operator database)
461 (write-char #\Space *sql-stream*))
462 (do ((sub subs (cdr sub)))
463 ((null (cdr sub)) (output-sql (car sub) database))
464 (output-sql (car sub) database)
465 (write-char #\Space *sql-stream*)
466 (output-sql operator database)
467 (write-char #\Space *sql-stream*))))
470 (defclass sql-query (%sql-expression)
481 :initarg :set-operation
513 (:documentation "An SQL SELECT query."))
515 (defclass sql-object-query (%sql-expression)
529 (defmethod collect-table-refs ((sql sql-query))
530 (remove-duplicates (collect-table-refs (slot-value sql 'where))
531 :test (lambda (tab1 tab2)
532 (equal (slot-value tab1 'name)
533 (slot-value tab2 'name)))))
535 (defvar *select-arguments*
536 '(:all :database :distinct :flatp :from :group-by :having :order-by
537 :set-operation :where :offset :limit :inner-join :on
538 ;; below keywords are not a SQL argument, but these keywords may terminate select
541 (defun query-arg-p (sym)
542 (member sym *select-arguments*))
544 (defun query-get-selections (select-args)
545 "Return two values: the list of select-args up to the first keyword,
546 uninclusive, and the args from that keyword to the end."
547 (let ((first-key-arg (position-if #'query-arg-p select-args)))
549 (values (subseq select-args 0 first-key-arg)
550 (subseq select-args first-key-arg))
553 (defun make-query (&rest args)
554 (flet ((select-objects (target-args)
556 (every #'(lambda (arg)
558 (find-class arg nil)))
560 (multiple-value-bind (selections arglist)
561 (query-get-selections args)
562 (if (select-objects selections)
563 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
564 (make-instance 'sql-object-query :objects selections
565 :flatp flatp :refresh refresh
567 (destructuring-bind (&key all flatp set-operation distinct from where
568 group-by having order-by
569 offset limit inner-join on &allow-other-keys)
571 (if (null selections)
572 (error "No target columns supplied to select statement."))
574 (error "No source tables supplied to select statement."))
575 (make-instance 'sql-query :selections selections
576 :all all :flatp flatp :set-operation set-operation
577 :distinct distinct :from from :where where
578 :limit limit :offset offset
579 :group-by group-by :having having :order-by order-by
580 :inner-join inner-join :on on))))))
582 (defmethod output-sql ((query sql-query) database)
583 (with-slots (distinct selections from where group-by having order-by
584 limit offset inner-join on all set-operation)
587 (write-string "(" *sql-stream*))
588 (write-string "SELECT " *sql-stream*)
590 (write-string "ALL " *sql-stream*))
591 (when (and distinct (not all))
592 (write-string "DISTINCT " *sql-stream*)
593 (unless (eql t distinct)
594 (write-string "ON " *sql-stream*)
595 (output-sql distinct database)
596 (write-char #\Space *sql-stream*)))
597 (let ((*in-subselect* t))
598 (output-sql (apply #'vector selections) database))
600 (write-string " FROM " *sql-stream*)
601 (flet ((ident-table-equal (a b)
602 (and (if (and (eql (type-of a) 'sql-ident-table)
603 (eql (type-of b) 'sql-ident-table))
604 (string-equal (slot-value a 'alias)
605 (slot-value b 'alias))
607 (string-equal (sql-escape (slot-value a 'name))
608 (sql-escape (slot-value b 'name))))))
610 (list (output-sql (apply #'vector
611 (remove-duplicates from
612 :test #'ident-table-equal))
614 (string (format *sql-stream* "~s" (sql-escape from)))
615 (t (let ((*in-subselect* t))
616 (output-sql from database))))))
618 (write-string " INNER JOIN " *sql-stream*)
619 (output-sql inner-join database))
621 (write-string " ON " *sql-stream*)
622 (output-sql on database))
624 (write-string " WHERE " *sql-stream*)
625 (let ((*in-subselect* t))
626 (output-sql where database)))
628 (write-string " GROUP BY " *sql-stream*)
630 (do ((order group-by (cdr order)))
632 (let ((item (car order)))
635 (output-sql (car item) database)
636 (format *sql-stream* " ~A" (cadr item)))
638 (output-sql item database)))
640 (write-char #\, *sql-stream*))))
641 (output-sql group-by database)))
643 (write-string " HAVING " *sql-stream*)
644 (output-sql having database))
646 (write-string " ORDER BY " *sql-stream*)
648 (do ((order order-by (cdr order)))
650 (let ((item (car order)))
653 (output-sql (car item) database)
654 (format *sql-stream* " ~A" (cadr item)))
656 (output-sql item database)))
658 (write-char #\, *sql-stream*))))
659 (output-sql order-by database)))
661 (write-string " LIMIT " *sql-stream*)
662 (output-sql limit database))
664 (write-string " OFFSET " *sql-stream*)
665 (output-sql offset database))
667 (write-string ")" *sql-stream*))
669 (write-char #\Space *sql-stream*)
670 (output-sql set-operation database)))
673 (defmethod output-sql ((query sql-object-query) database)
674 (declare (ignore database))
675 (with-slots (objects)
678 (format *sql-stream* "(~{~A~^ ~})" objects))))
683 (defclass sql-insert (%sql-expression)
697 "An SQL INSERT statement."))
699 (defmethod output-sql ((ins sql-insert) database)
700 (with-slots (into attributes values query)
702 (write-string "INSERT INTO " *sql-stream*)
705 (string (sql-expression :table into))
709 (write-char #\Space *sql-stream*)
710 (output-sql attributes database))
712 (write-string " VALUES " *sql-stream*)
713 (output-sql values database))
715 (write-char #\Space *sql-stream*)
716 (output-sql query database)))
721 (defclass sql-delete (%sql-expression)
729 "An SQL DELETE statement."))
731 (defmethod output-sql ((stmt sql-delete) database)
732 (with-slots (from where)
734 (write-string "DELETE FROM " *sql-stream*)
736 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
737 (t (output-sql from database)))
739 (write-string " WHERE " *sql-stream*)
740 (output-sql where database)))
745 (defclass sql-update (%sql-expression)
758 (:documentation "An SQL UPDATE statement."))
760 (defmethod output-sql ((expr sql-update) database)
761 (with-slots (table where attributes values)
763 (flet ((update-assignments ()
764 (mapcar #'(lambda (a b)
765 (make-instance 'sql-assignment-exp
767 :sub-expressions (list a b)))
769 (write-string "UPDATE " *sql-stream*)
770 (output-sql table database)
771 (write-string " SET " *sql-stream*)
772 (output-sql (apply #'vector (update-assignments)) database)
774 (write-string " WHERE " *sql-stream*)
775 (output-sql where database))))
780 (defclass sql-create-table (%sql-expression)
791 :initarg :transactions
794 "An SQL CREATE TABLE statement."))
796 ;; Here's a real warhorse of a function!
798 (declaim (inline listify))
804 (defmethod output-sql ((stmt sql-create-table) database)
805 (flet ((output-column (column-spec)
806 (destructuring-bind (name type &optional db-type &rest constraints)
808 (let ((type (listify type)))
809 (output-sql name database)
810 (write-char #\Space *sql-stream*)
812 (if (stringp db-type) db-type ; override definition
813 (database-get-type-specifier (car type) (cdr type) database
814 (database-underlying-type database)))
816 (let ((constraints (database-constraint-statement
817 (if (and db-type (symbolp db-type))
818 (cons db-type constraints)
822 (write-string " " *sql-stream*)
823 (write-string constraints *sql-stream*)))))))
824 (with-slots (name columns modifiers transactions)
826 (write-string "CREATE TABLE " *sql-stream*)
828 (string (format *sql-stream* "~s" (sql-escape name)))
829 (symbol (write-string (sql-escape name) *sql-stream*))
830 (sql-ident (output-sql name database)))
831 (write-string " (" *sql-stream*)
832 (do ((column columns (cdr column)))
834 (output-column (car column)))
835 (output-column (car column))
836 (write-string ", " *sql-stream*))
838 (do ((modifier (listify modifiers) (cdr modifier)))
840 (write-string ", " *sql-stream*)
841 (write-string (car modifier) *sql-stream*)))
842 (write-char #\) *sql-stream*)
843 (when (and (eq :mysql (database-underlying-type database))
845 (db-type-transaction-capable? :mysql database))
846 (write-string " Type=InnoDB" *sql-stream*))))
852 (defclass sql-create-view (%sql-expression)
853 ((name :initarg :name :initform nil)
854 (column-list :initarg :column-list :initform nil)
855 (query :initarg :query :initform nil)
856 (with-check-option :initarg :with-check-option :initform nil))
857 (:documentation "An SQL CREATE VIEW statement."))
859 (defmethod output-sql ((stmt sql-create-view) database)
860 (with-slots (name column-list query with-check-option) stmt
861 (write-string "CREATE VIEW " *sql-stream*)
862 (output-sql name database)
863 (when column-list (write-string " " *sql-stream*)
864 (output-sql (listify column-list) database))
865 (write-string " AS " *sql-stream*)
866 (output-sql query database)
867 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
871 ;; DATABASE-OUTPUT-SQL
874 (defmethod database-output-sql ((str string) database)
875 (declare (optimize (speed 3) (safety 1)
876 #+cmu (extensions:inhibit-warnings 3)))
877 (let ((len (length str)))
878 (declare (type fixnum len))
881 ((and (null (position #\' str))
882 (null (position #\\ str)))
883 (concatenate 'string "'" str "'"))
885 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
886 (declare (simple-string buf))
889 ((= i len) (subseq buf 0 (1+ j)))
890 (declare (type fixnum i j))
891 (let ((char (aref str i)))
892 (declare (character char))
893 (cond ((char= char #\')
894 (setf (aref buf j) #\')
896 (setf (aref buf j) #\'))
897 ((and (char= char #\\)
898 ;; MTP: only escape backslash with pgsql/mysql
899 (member (database-underlying-type database)
900 '(:postgresql :mysql)
902 (setf (aref buf j) #\\)
904 (setf (aref buf j) #\\))
906 (setf (aref buf j) char))))))))))
908 (let ((keyword-package (symbol-package :foo)))
909 (defmethod database-output-sql ((sym symbol) database)
912 (if (equal (symbol-package sym) keyword-package)
913 (concatenate 'string "'" (string sym) "'")
914 (symbol-name sym)))))
916 (defmethod database-output-sql ((tee (eql t)) database)
918 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
921 (string (format nil "'~A'" val))
922 (integer (format nil "~A" val)))))
925 #+nil(defmethod database-output-sql ((tee (eql t)) database)
926 (declare (ignore database))
929 (defmethod database-output-sql ((num number) database)
930 (declare (ignore database))
931 (number-to-sql-string num))
933 (defmethod database-output-sql ((arg list) database)
936 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
937 (sql-output val database))
940 (defmethod database-output-sql ((arg vector) database)
941 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
942 (sql-output val database))
945 (defmethod output-sql-hash-key ((arg vector) database)
946 (list 'vector (map 'list (lambda (arg)
947 (or (output-sql-hash-key arg database)
948 (return-from output-sql-hash-key nil)))
951 (defmethod database-output-sql ((self wall-time) database)
952 (declare (ignore database))
953 (db-timestring self))
955 (defmethod database-output-sql ((self date) database)
956 (declare (ignore database))
957 (db-datestring self))
959 (defmethod database-output-sql ((self duration) database)
960 (declare (ignore database))
961 (format nil "'~a'" (duration-timestring self)))
964 (defmethod database-output-sql ((self money) database)
965 (database-output-sql (slot-value self 'odcl::units) database))
967 (defmethod database-output-sql (thing database)
971 (error 'sql-user-error
974 "No type conversion to SQL for ~A is defined for DB ~A."
975 (type-of thing) (type-of database)))))
979 ;; Column constraint types and conversion to SQL
982 (defparameter *constraint-types*
984 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
985 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
986 (cons (symbol-name-default-case "NOT") "NOT")
987 (cons (symbol-name-default-case "NULL") "NULL")
988 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
989 (cons (symbol-name-default-case "KEY") "KEY")
990 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
991 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
992 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
993 (cons (symbol-name-default-case "DEFAULT") "DEFAULT")
994 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")))
996 (defmethod database-constraint-statement (constraint-list database)
997 (declare (ignore database))
998 (make-constraints-description constraint-list))
1000 (defun make-constraints-description (constraint-list)
1003 (do ((constraint constraint-list (cdr constraint)))
1004 ((null constraint) string)
1005 (let ((output (assoc (symbol-name (car constraint))
1009 (error 'sql-user-error
1010 :message (format nil "unsupported column constraint '~A'"
1012 (setq string (concatenate 'string string (cdr output))))
1013 (when (equal (symbol-name (car constraint)) "DEFAULT")
1014 (setq constraint (cdr constraint))
1015 (setq string (concatenate 'string string " " (car constraint))))
1016 (if (< 1 (length constraint))
1017 (setq string (concatenate 'string string " "))))))))