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 (labels ((quoted-string-p (inp)
155 (and (char-equal #\" (elt inp 0))
156 (char-equal #\" (elt inp (1- (length inp))))))
158 "do our best not to output sql that we can guarantee is invalid.
159 if the ident has a space or quote in it, instead output a quoted
160 identifier containing those chars"
161 (when (and (not (quoted-string-p inp))
163 (lambda (x) (member x '(#\space #\' #\")
164 :test #'char-equal)) inp))
165 (setf inp (format nil "~s" (substitute "\\\"" "\"" inp :test #'string-equal))))
167 (with-slots (qualifier name type) expr
168 (format *sql-stream* "~@[~a.~]~a"
170 (null nil) ; nil is a symbol
171 (string (format nil "~s" qualifier))
172 (symbol (safety-first (sql-escape qualifier))))
173 (typecase name ;; could never get this to be nil without getting another error first
174 (string (format nil "~s" name))
175 (symbol (safety-first (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 #\no-break_space)
253 (with-output-to-string (*sql-stream*)
254 (output-sql sub database)))))
255 (let ((str-subs (loop for sub in sub-expressions
256 for str-sub = (trim sub)
257 when (and str-sub (> (length str-sub) 0))
259 (case (length str-subs)
261 (1 (write-string (first str-subs) *sql-stream*))
263 (write-char #\( *sql-stream*)
264 (write-string (first str-subs) *sql-stream*)
265 (loop for str-sub in (rest str-subs)
267 (write-char #\Space *sql-stream*)
268 (output-sql operator database)
269 (write-char #\Space *sql-stream*)
270 (write-string str-sub *sql-stream*))
271 (write-char #\) *sql-stream*))
275 (defclass sql-array-exp (sql-relational-exp)
277 (:documentation "An SQL relational expression."))
279 (defmethod output-sql ((expr sql-array-exp) database)
280 (with-slots (operator sub-expressions)
282 (let ((subs (if (consp (car sub-expressions))
283 (car sub-expressions)
285 (write-char #\( *sql-stream*)
286 (output-sql operator database)
287 (write-char #\[ *sql-stream*)
288 (do ((sub subs (cdr sub)))
289 ((null (cdr sub)) (output-sql (car sub) database))
290 (output-sql (car sub) database)
291 (write-char #\, *sql-stream*)
292 (write-char #\Space *sql-stream*))
293 (write-char #\] *sql-stream*)
294 (write-char #\) *sql-stream*)))
297 (defclass sql-upcase-like (sql-relational-exp)
299 (:documentation "An SQL 'like' that upcases its arguments."))
301 (defmethod output-sql ((expr sql-upcase-like) database)
302 (flet ((write-term (term)
303 (write-string "upper(" *sql-stream*)
304 (output-sql term database)
305 (write-char #\) *sql-stream*)))
306 (with-slots (sub-expressions)
308 (let ((subs (if (consp (car sub-expressions))
309 (car sub-expressions)
311 (write-char #\( *sql-stream*)
312 (do ((sub subs (cdr sub)))
313 ((null (cdr sub)) (write-term (car sub)))
314 (write-term (car sub))
315 (write-string " LIKE " *sql-stream*))
316 (write-char #\) *sql-stream*))))
319 (defclass sql-assignment-exp (sql-relational-exp)
321 (:documentation "An SQL Assignment expression."))
324 (defmethod output-sql ((expr sql-assignment-exp) database)
325 (with-slots (operator sub-expressions)
327 (do ((sub sub-expressions (cdr sub)))
328 ((null (cdr sub)) (output-sql (car sub) database))
329 (output-sql (car sub) database)
330 (write-char #\Space *sql-stream*)
331 (output-sql operator database)
332 (write-char #\Space *sql-stream*)))
335 (defclass sql-value-exp (%sql-expression)
343 "An SQL value expression.")
346 (defmethod collect-table-refs ((sql sql-value-exp))
348 (if (listp (slot-value sql 'components))
350 (dolist (exp (slot-value sql 'components))
351 (let ((refs (collect-table-refs exp)))
352 (if refs (setf tabs (append refs tabs)))))
353 (remove-duplicates tabs
354 :test (lambda (tab1 tab2)
355 (equal (slot-value tab1 'name)
356 (slot-value tab2 'name)))))
361 (defmethod output-sql ((expr sql-value-exp) database)
362 (with-slots (modifier components)
366 (write-char #\( *sql-stream*)
367 (output-sql modifier database)
368 (write-char #\Space *sql-stream*)
369 (output-sql components database)
370 (write-char #\) *sql-stream*))
371 (output-sql components database))))
373 (defclass sql-typecast-exp (sql-value-exp)
375 (:documentation "An SQL typecast expression."))
377 (defmethod output-sql ((expr sql-typecast-exp) database)
378 (with-slots (components)
380 (output-sql components database)))
382 (defmethod collect-table-refs ((sql sql-typecast-exp))
383 (when (slot-value sql 'components)
384 (collect-table-refs (slot-value sql 'components))))
386 (defclass sql-function-exp (%sql-expression)
394 "An SQL function expression."))
396 (defmethod collect-table-refs ((sql sql-function-exp))
398 (dolist (exp (slot-value sql 'args))
399 (let ((refs (collect-table-refs exp)))
400 (if refs (setf tabs (append refs tabs)))))
401 (remove-duplicates tabs
402 :test (lambda (tab1 tab2)
403 (equal (slot-value tab1 'name)
404 (slot-value tab2 'name))))))
405 (defvar *in-subselect* nil)
407 (defmethod output-sql ((expr sql-function-exp) database)
408 (with-slots (name args)
410 (output-sql name database)
411 (let ((*in-subselect* nil)) ;; aboid double parens
412 (when args (output-sql args database))))
416 (defclass sql-between-exp (sql-function-exp)
418 (:documentation "An SQL between expression."))
420 (defmethod output-sql ((expr sql-between-exp) database)
423 (output-sql (first args) database)
424 (write-string " BETWEEN " *sql-stream*)
425 (output-sql (second args) database)
426 (write-string " AND " *sql-stream*)
427 (output-sql (third args) database))
430 (defclass sql-query-modifier-exp (%sql-expression)
431 ((modifier :initarg :modifier :initform nil)
432 (components :initarg :components :initform nil))
433 (:documentation "An SQL query modifier expression."))
435 (defmethod output-sql ((expr sql-query-modifier-exp) database)
436 (with-slots (modifier components)
438 (output-sql modifier database)
439 (write-string " " *sql-stream*)
440 (output-sql (car components) database)
442 (mapc #'(lambda (comp)
443 (write-string ", " *sql-stream*)
444 (output-sql comp database))
448 (defclass sql-set-exp (%sql-expression)
453 :initarg :sub-expressions
455 (:documentation "An SQL set expression."))
457 (defmethod collect-table-refs ((sql sql-set-exp))
459 (dolist (exp (slot-value sql 'sub-expressions))
460 (let ((refs (collect-table-refs exp)))
461 (if refs (setf tabs (append refs tabs)))))
462 (remove-duplicates tabs
463 :test (lambda (tab1 tab2)
464 (equal (slot-value tab1 'name)
465 (slot-value tab2 'name))))))
467 (defmethod output-sql ((expr sql-set-exp) database)
468 (with-slots (operator sub-expressions)
470 (let ((subs (if (consp (car sub-expressions))
471 (car sub-expressions)
473 (when (= (length subs) 1)
474 (output-sql operator database)
475 (write-char #\Space *sql-stream*))
476 (do ((sub subs (cdr sub)))
477 ((null (cdr sub)) (output-sql (car sub) database))
478 (output-sql (car sub) database)
479 (write-char #\Space *sql-stream*)
480 (output-sql operator database)
481 (write-char #\Space *sql-stream*))))
484 (defclass sql-query (%sql-expression)
495 :initarg :set-operation
527 (:documentation "An SQL SELECT query."))
529 (defclass sql-object-query (%sql-expression)
543 (defmethod collect-table-refs ((sql sql-query))
544 (remove-duplicates (collect-table-refs (slot-value sql 'where))
545 :test (lambda (tab1 tab2)
546 (equal (slot-value tab1 'name)
547 (slot-value tab2 'name)))))
549 (defvar *select-arguments*
550 '(:all :database :distinct :flatp :from :group-by :having :order-by
551 :set-operation :where :offset :limit :inner-join :on
552 ;; below keywords are not a SQL argument, but these keywords may terminate select
555 (defun query-arg-p (sym)
556 (member sym *select-arguments*))
558 (defun query-get-selections (select-args)
559 "Return two values: the list of select-args up to the first keyword,
560 uninclusive, and the args from that keyword to the end."
561 (let ((first-key-arg (position-if #'query-arg-p select-args)))
563 (values (subseq select-args 0 first-key-arg)
564 (subseq select-args first-key-arg))
567 (defun make-query (&rest args)
568 (flet ((select-objects (target-args)
570 (every #'(lambda (arg)
572 (find-class arg nil)))
574 (multiple-value-bind (selections arglist)
575 (query-get-selections args)
576 (if (select-objects selections)
577 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
578 (make-instance 'sql-object-query :objects selections
579 :flatp flatp :refresh refresh
581 (destructuring-bind (&key all flatp set-operation distinct from where
582 group-by having order-by
583 offset limit inner-join on &allow-other-keys)
585 (if (null selections)
586 (error "No target columns supplied to select statement."))
588 (error "No source tables supplied to select statement."))
589 (make-instance 'sql-query :selections selections
590 :all all :flatp flatp :set-operation set-operation
591 :distinct distinct :from from :where where
592 :limit limit :offset offset
593 :group-by group-by :having having :order-by order-by
594 :inner-join inner-join :on on))))))
596 (defmethod output-sql ((query sql-query) database)
597 (with-slots (distinct selections from where group-by having order-by
598 limit offset inner-join on all set-operation)
601 (write-string "(" *sql-stream*))
602 (write-string "SELECT " *sql-stream*)
604 (write-string "ALL " *sql-stream*))
605 (when (and limit (eq :odbc (database-type database)))
606 (write-string " TOP " *sql-stream*)
607 (output-sql limit database))
608 (when (and distinct (not all))
609 (write-string "DISTINCT " *sql-stream*)
610 (unless (eql t distinct)
611 (write-string "ON " *sql-stream*)
612 (output-sql distinct database)
613 (write-char #\Space *sql-stream*)))
614 (let ((*in-subselect* t))
615 (output-sql (apply #'vector selections) database))
617 (write-string " FROM " *sql-stream*)
618 (flet ((ident-table-equal (a b)
619 (and (if (and (eql (type-of a) 'sql-ident-table)
620 (eql (type-of b) 'sql-ident-table))
621 (string-equal (slot-value a 'alias)
622 (slot-value b 'alias))
624 (string-equal (sql-escape (slot-value a 'name))
625 (sql-escape (slot-value b 'name))))))
627 (list (output-sql (apply #'vector
628 (remove-duplicates from
629 :test #'ident-table-equal))
631 (string (format *sql-stream* "~s" (sql-escape from)))
632 (t (let ((*in-subselect* t))
633 (output-sql from database))))))
635 (write-string " INNER JOIN " *sql-stream*)
636 (output-sql inner-join database))
638 (write-string " ON " *sql-stream*)
639 (output-sql on database))
641 (let ((where-out (string-trim '(#\newline #\space #\tab #\return)
642 (with-output-to-string (*sql-stream*)
643 (let ((*in-subselect* t))
644 (output-sql where database))))))
645 (when (> (length where-out) 0)
646 (write-string " WHERE " *sql-stream*)
647 (write-string where-out *sql-stream*))))
649 (write-string " GROUP BY " *sql-stream*)
651 (do ((order group-by (cdr order)))
653 (let ((item (car order)))
656 (output-sql (car item) database)
657 (format *sql-stream* " ~A" (cadr item)))
659 (output-sql item database)))
661 (write-char #\, *sql-stream*))))
662 (output-sql group-by database)))
664 (write-string " HAVING " *sql-stream*)
665 (output-sql having database))
667 (write-string " ORDER BY " *sql-stream*)
669 (do ((order order-by (cdr order)))
671 (let ((item (car order)))
674 (output-sql (car item) database)
675 (format *sql-stream* " ~A" (cadr item)))
677 (output-sql item database)))
679 (write-char #\, *sql-stream*))))
680 (output-sql order-by database)))
681 (when (and limit (not (eq :odbc (database-type database))))
682 (write-string " LIMIT " *sql-stream*)
683 (output-sql limit database))
685 (write-string " OFFSET " *sql-stream*)
686 (output-sql offset database))
688 (write-string ")" *sql-stream*))
690 (write-char #\Space *sql-stream*)
691 (output-sql set-operation database)))
694 (defmethod output-sql ((query sql-object-query) database)
695 (declare (ignore database))
696 (with-slots (objects)
699 (format *sql-stream* "(~{~A~^ ~})" objects))))
704 (defclass sql-insert (%sql-expression)
718 "An SQL INSERT statement."))
720 (defmethod output-sql ((ins sql-insert) database)
721 (with-slots (into attributes values query)
723 (write-string "INSERT INTO " *sql-stream*)
726 (string (sql-expression :table into))
730 (write-char #\Space *sql-stream*)
731 (output-sql attributes database))
733 (write-string " VALUES " *sql-stream*)
734 (output-sql values database))
736 (write-char #\Space *sql-stream*)
737 (output-sql query database)))
742 (defclass sql-delete (%sql-expression)
750 "An SQL DELETE statement."))
752 (defmethod output-sql ((stmt sql-delete) database)
753 (with-slots (from where)
755 (write-string "DELETE FROM " *sql-stream*)
757 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
758 (t (output-sql from database)))
760 (write-string " WHERE " *sql-stream*)
761 (output-sql where database)))
766 (defclass sql-update (%sql-expression)
779 (:documentation "An SQL UPDATE statement."))
781 (defmethod output-sql ((expr sql-update) database)
782 (with-slots (table where attributes values)
784 (flet ((update-assignments ()
785 (mapcar #'(lambda (a b)
786 (make-instance 'sql-assignment-exp
788 :sub-expressions (list a b)))
790 (write-string "UPDATE " *sql-stream*)
791 (output-sql table database)
792 (write-string " SET " *sql-stream*)
793 (output-sql (apply #'vector (update-assignments)) database)
795 (write-string " WHERE " *sql-stream*)
796 (output-sql where database))))
801 (defclass sql-create-table (%sql-expression)
812 :initarg :transactions
815 "An SQL CREATE TABLE statement."))
817 ;; Here's a real warhorse of a function!
819 (declaim (inline listify))
825 (defmethod output-sql ((stmt sql-create-table) database)
826 (flet ((output-column (column-spec)
827 (destructuring-bind (name type &optional db-type &rest constraints)
829 (let ((type (listify type)))
830 (output-sql name database)
831 (write-char #\Space *sql-stream*)
833 (if (stringp db-type) db-type ; override definition
834 (database-get-type-specifier (car type) (cdr type) database
835 (database-underlying-type database)))
837 (let ((constraints (database-constraint-statement
838 (if (and db-type (symbolp db-type))
839 (cons db-type constraints)
843 (write-string " " *sql-stream*)
844 (write-string constraints *sql-stream*)))))))
845 (with-slots (name columns modifiers transactions)
847 (write-string "CREATE TABLE " *sql-stream*)
849 (string (format *sql-stream* "~s" (sql-escape name)))
850 (symbol (write-string (sql-escape name) *sql-stream*))
851 (sql-ident (output-sql name database)))
852 (write-string " (" *sql-stream*)
853 (do ((column columns (cdr column)))
855 (output-column (car column)))
856 (output-column (car column))
857 (write-string ", " *sql-stream*))
859 (do ((modifier (listify modifiers) (cdr modifier)))
861 (write-string ", " *sql-stream*)
862 (write-string (car modifier) *sql-stream*)))
863 (write-char #\) *sql-stream*)
864 (when (and (eq :mysql (database-underlying-type database))
866 (db-type-transaction-capable? :mysql database))
867 (write-string " Type=InnoDB" *sql-stream*))))
873 (defclass sql-create-view (%sql-expression)
874 ((name :initarg :name :initform nil)
875 (column-list :initarg :column-list :initform nil)
876 (query :initarg :query :initform nil)
877 (with-check-option :initarg :with-check-option :initform nil))
878 (:documentation "An SQL CREATE VIEW statement."))
880 (defmethod output-sql ((stmt sql-create-view) database)
881 (with-slots (name column-list query with-check-option) stmt
882 (write-string "CREATE VIEW " *sql-stream*)
883 (output-sql name database)
884 (when column-list (write-string " " *sql-stream*)
885 (output-sql (listify column-list) database))
886 (write-string " AS " *sql-stream*)
887 (output-sql query database)
888 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
892 ;; DATABASE-OUTPUT-SQL
895 (defmethod database-output-sql ((str string) database)
896 (declare (optimize (speed 3) (safety 1)
897 #+cmu (extensions:inhibit-warnings 3)))
898 (let ((len (length str)))
899 (declare (type fixnum len))
902 ((and (null (position #\' str))
903 (null (position #\\ str)))
904 (concatenate 'string "'" str "'"))
906 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
907 (declare (simple-string buf))
910 ((= i len) (subseq buf 0 (1+ j)))
911 (declare (type fixnum i j))
912 (let ((char (aref str i)))
913 (declare (character char))
914 (cond ((char= char #\')
915 (setf (aref buf j) #\')
917 (setf (aref buf j) #\'))
918 ((and (char= char #\\)
919 ;; MTP: only escape backslash with pgsql/mysql
920 (member (database-underlying-type database)
921 '(:postgresql :mysql)
923 (setf (aref buf j) #\\)
925 (setf (aref buf j) #\\))
927 (setf (aref buf j) char))))))))))
929 (let ((keyword-package (symbol-package :foo)))
930 (defmethod database-output-sql ((sym symbol) database)
933 (if (equal (symbol-package sym) keyword-package)
934 (concatenate 'string "'" (string sym) "'")
935 (symbol-name sym)))))
937 (defmethod database-output-sql ((tee (eql t)) database)
939 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
942 (string (format nil "'~A'" val))
943 (integer (format nil "~A" val)))))
946 #+nil(defmethod database-output-sql ((tee (eql t)) database)
947 (declare (ignore database))
950 (defmethod database-output-sql ((num number) database)
951 (declare (ignore database))
952 (number-to-sql-string num))
954 (defmethod database-output-sql ((arg list) database)
957 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
958 (sql-output val database))
961 (defmethod database-output-sql ((arg vector) database)
962 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
963 (sql-output val database))
966 (defmethod output-sql-hash-key ((arg vector) database)
967 (list 'vector (map 'list (lambda (arg)
968 (or (output-sql-hash-key arg database)
969 (return-from output-sql-hash-key nil)))
972 (defmethod database-output-sql ((self wall-time) database)
973 (declare (ignore database))
974 (db-timestring self))
976 (defmethod database-output-sql ((self date) database)
977 (declare (ignore database))
978 (db-datestring self))
980 (defmethod database-output-sql ((self duration) database)
981 (declare (ignore database))
982 (format nil "'~a'" (duration-timestring self)))
985 (defmethod database-output-sql ((self money) database)
986 (database-output-sql (slot-value self 'odcl::units) database))
988 (defmethod database-output-sql (thing database)
992 (error 'sql-user-error
995 "No type conversion to SQL for ~A is defined for DB ~A."
996 (type-of thing) (type-of database)))))
1000 ;; Column constraint types and conversion to SQL
1003 (defparameter *constraint-types*
1005 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
1006 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
1007 (cons (symbol-name-default-case "NOT") "NOT")
1008 (cons (symbol-name-default-case "NULL") "NULL")
1009 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
1010 (cons (symbol-name-default-case "KEY") "KEY")
1011 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
1012 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
1013 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
1014 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
1015 (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;Added Identity for MS-SQLServer support
1018 (defmethod database-constraint-statement (constraint-list database)
1019 (declare (ignore database))
1020 (make-constraints-description constraint-list))
1022 (defun make-constraints-description (constraint-list)
1025 (do ((constraint constraint-list (cdr constraint)))
1026 ((null constraint) string)
1027 (let ((output (assoc (symbol-name (car constraint))
1031 (error 'sql-user-error
1032 :message (format nil "unsupported column constraint '~A'"
1034 (setq string (concatenate 'string string (cdr output))))
1035 (if (< 1 (length constraint))
1036 (setq string (concatenate 'string string " "))))))))