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)
247 (when sub-expressions
248 (let ((subs (if (consp (car sub-expressions))
249 (car sub-expressions)
251 (write-char #\( *sql-stream*)
252 (do ((sub subs (cdr sub)))
254 (output-sql (car sub) database))
255 (output-sql (car sub) database)
256 (write-char #\Space *sql-stream*)
257 (output-sql operator database)
258 (write-char #\Space *sql-stream*))
259 (write-char #\) *sql-stream*))))
262 (defclass sql-array-exp (sql-relational-exp)
264 (:documentation "An SQL relational expression."))
266 (defmethod output-sql ((expr sql-array-exp) database)
267 (with-slots (operator sub-expressions)
269 (let ((subs (if (consp (car sub-expressions))
270 (car sub-expressions)
272 (write-char #\( *sql-stream*)
273 (output-sql operator database)
274 (write-char #\[ *sql-stream*)
275 (do ((sub subs (cdr sub)))
276 ((null (cdr sub)) (output-sql (car sub) database))
277 (output-sql (car sub) database)
278 (write-char #\, *sql-stream*)
279 (write-char #\Space *sql-stream*))
280 (write-char #\] *sql-stream*)
281 (write-char #\) *sql-stream*)))
284 (defclass sql-upcase-like (sql-relational-exp)
286 (:documentation "An SQL 'like' that upcases its arguments."))
288 (defmethod output-sql ((expr sql-upcase-like) database)
289 (flet ((write-term (term)
290 (write-string "upper(" *sql-stream*)
291 (output-sql term database)
292 (write-char #\) *sql-stream*)))
293 (with-slots (sub-expressions)
295 (let ((subs (if (consp (car sub-expressions))
296 (car sub-expressions)
298 (write-char #\( *sql-stream*)
299 (do ((sub subs (cdr sub)))
300 ((null (cdr sub)) (write-term (car sub)))
301 (write-term (car sub))
302 (write-string " LIKE " *sql-stream*))
303 (write-char #\) *sql-stream*))))
306 (defclass sql-assignment-exp (sql-relational-exp)
308 (:documentation "An SQL Assignment expression."))
311 (defmethod output-sql ((expr sql-assignment-exp) database)
312 (with-slots (operator sub-expressions)
314 (do ((sub sub-expressions (cdr sub)))
315 ((null (cdr sub)) (output-sql (car sub) database))
316 (output-sql (car sub) database)
317 (write-char #\Space *sql-stream*)
318 (output-sql operator database)
319 (write-char #\Space *sql-stream*)))
322 (defclass sql-value-exp (%sql-expression)
330 "An SQL value expression.")
333 (defmethod collect-table-refs ((sql sql-value-exp))
335 (if (listp (slot-value sql 'components))
337 (dolist (exp (slot-value sql 'components))
338 (let ((refs (collect-table-refs exp)))
339 (if refs (setf tabs (append refs tabs)))))
340 (remove-duplicates tabs
341 :test (lambda (tab1 tab2)
342 (equal (slot-value tab1 'name)
343 (slot-value tab2 'name)))))
348 (defmethod output-sql ((expr sql-value-exp) database)
349 (with-slots (modifier components)
353 (write-char #\( *sql-stream*)
354 (output-sql modifier database)
355 (write-char #\Space *sql-stream*)
356 (output-sql components database)
357 (write-char #\) *sql-stream*))
358 (output-sql components database))))
360 (defclass sql-typecast-exp (sql-value-exp)
362 (:documentation "An SQL typecast expression."))
364 (defmethod output-sql ((expr sql-typecast-exp) database)
365 (with-slots (components)
367 (output-sql components database)))
369 (defmethod collect-table-refs ((sql sql-typecast-exp))
370 (when (slot-value sql 'components)
371 (collect-table-refs (slot-value sql 'components))))
373 (defclass sql-function-exp (%sql-expression)
381 "An SQL function expression."))
383 (defmethod collect-table-refs ((sql sql-function-exp))
385 (dolist (exp (slot-value sql 'args))
386 (let ((refs (collect-table-refs exp)))
387 (if refs (setf tabs (append refs tabs)))))
388 (remove-duplicates tabs
389 :test (lambda (tab1 tab2)
390 (equal (slot-value tab1 'name)
391 (slot-value tab2 'name))))))
392 (defvar *in-subselect* nil)
394 (defmethod output-sql ((expr sql-function-exp) database)
395 (with-slots (name args)
397 (output-sql name database)
398 (let ((*in-subselect* nil)) ;; aboid double parens
399 (when args (output-sql args database))))
403 (defclass sql-between-exp (sql-function-exp)
405 (:documentation "An SQL between expression."))
407 (defmethod output-sql ((expr sql-between-exp) database)
410 (output-sql (first args) database)
411 (write-string " BETWEEN " *sql-stream*)
412 (output-sql (second args) database)
413 (write-string " AND " *sql-stream*)
414 (output-sql (third args) database))
417 (defclass sql-query-modifier-exp (%sql-expression)
418 ((modifier :initarg :modifier :initform nil)
419 (components :initarg :components :initform nil))
420 (:documentation "An SQL query modifier expression."))
422 (defmethod output-sql ((expr sql-query-modifier-exp) database)
423 (with-slots (modifier components)
425 (output-sql modifier database)
426 (write-string " " *sql-stream*)
427 (output-sql (car components) database)
429 (mapc #'(lambda (comp)
430 (write-string ", " *sql-stream*)
431 (output-sql comp database))
435 (defclass sql-set-exp (%sql-expression)
440 :initarg :sub-expressions
442 (:documentation "An SQL set expression."))
444 (defmethod collect-table-refs ((sql sql-set-exp))
446 (dolist (exp (slot-value sql 'sub-expressions))
447 (let ((refs (collect-table-refs exp)))
448 (if refs (setf tabs (append refs tabs)))))
449 (remove-duplicates tabs
450 :test (lambda (tab1 tab2)
451 (equal (slot-value tab1 'name)
452 (slot-value tab2 'name))))))
454 (defmethod output-sql ((expr sql-set-exp) database)
455 (with-slots (operator sub-expressions)
457 (let ((subs (if (consp (car sub-expressions))
458 (car sub-expressions)
460 (when (= (length subs) 1)
461 (output-sql operator database)
462 (write-char #\Space *sql-stream*))
463 (do ((sub subs (cdr sub)))
464 ((null (cdr sub)) (output-sql (car sub) database))
465 (output-sql (car sub) database)
466 (write-char #\Space *sql-stream*)
467 (output-sql operator database)
468 (write-char #\Space *sql-stream*))))
471 (defclass sql-query (%sql-expression)
482 :initarg :set-operation
514 (:documentation "An SQL SELECT query."))
516 (defclass sql-object-query (%sql-expression)
530 (defmethod collect-table-refs ((sql sql-query))
531 (remove-duplicates (collect-table-refs (slot-value sql 'where))
532 :test (lambda (tab1 tab2)
533 (equal (slot-value tab1 'name)
534 (slot-value tab2 'name)))))
536 (defvar *select-arguments*
537 '(:all :database :distinct :flatp :from :group-by :having :order-by
538 :set-operation :where :offset :limit :inner-join :on
539 ;; below keywords are not a SQL argument, but these keywords may terminate select
542 (defun query-arg-p (sym)
543 (member sym *select-arguments*))
545 (defun query-get-selections (select-args)
546 "Return two values: the list of select-args up to the first keyword,
547 uninclusive, and the args from that keyword to the end."
548 (let ((first-key-arg (position-if #'query-arg-p select-args)))
550 (values (subseq select-args 0 first-key-arg)
551 (subseq select-args first-key-arg))
554 (defun make-query (&rest args)
555 (flet ((select-objects (target-args)
557 (every #'(lambda (arg)
559 (find-class arg nil)))
561 (multiple-value-bind (selections arglist)
562 (query-get-selections args)
563 (if (select-objects selections)
564 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
565 (make-instance 'sql-object-query :objects selections
566 :flatp flatp :refresh refresh
568 (destructuring-bind (&key all flatp set-operation distinct from where
569 group-by having order-by
570 offset limit inner-join on &allow-other-keys)
572 (if (null selections)
573 (error "No target columns supplied to select statement."))
575 (error "No source tables supplied to select statement."))
576 (make-instance 'sql-query :selections selections
577 :all all :flatp flatp :set-operation set-operation
578 :distinct distinct :from from :where where
579 :limit limit :offset offset
580 :group-by group-by :having having :order-by order-by
581 :inner-join inner-join :on on))))))
583 (defmethod output-sql ((query sql-query) database)
584 (with-slots (distinct selections from where group-by having order-by
585 limit offset inner-join on all set-operation)
588 (write-string "(" *sql-stream*))
589 (write-string "SELECT " *sql-stream*)
591 (write-string "ALL " *sql-stream*))
592 (when (and limit (eq :odbc (database-type database)))
593 (write-string " TOP " *sql-stream*)
594 (output-sql limit database))
595 (when (and distinct (not all))
596 (write-string "DISTINCT " *sql-stream*)
597 (unless (eql t distinct)
598 (write-string "ON " *sql-stream*)
599 (output-sql distinct database)
600 (write-char #\Space *sql-stream*)))
601 (let ((*in-subselect* t))
602 (output-sql (apply #'vector selections) database))
604 (write-string " FROM " *sql-stream*)
605 (flet ((ident-table-equal (a b)
606 (and (if (and (eql (type-of a) 'sql-ident-table)
607 (eql (type-of b) 'sql-ident-table))
608 (string-equal (slot-value a 'alias)
609 (slot-value b 'alias))
611 (string-equal (sql-escape (slot-value a 'name))
612 (sql-escape (slot-value b 'name))))))
614 (list (output-sql (apply #'vector
615 (remove-duplicates from
616 :test #'ident-table-equal))
618 (string (format *sql-stream* "~s" (sql-escape from)))
619 (t (let ((*in-subselect* t))
620 (output-sql from database))))))
622 (write-string " INNER JOIN " *sql-stream*)
623 (output-sql inner-join database))
625 (write-string " ON " *sql-stream*)
626 (output-sql on database))
628 (let ((where-out (string-trim '(#\newline #\space #\tab #\return)
629 (with-output-to-string (*sql-stream*)
630 (let ((*in-subselect* t))
631 (output-sql where database))))))
632 (when (> (length where-out) 0)
633 (write-string " WHERE " *sql-stream*)
634 (write-string where-out *sql-stream*))))
636 (write-string " GROUP BY " *sql-stream*)
638 (do ((order group-by (cdr order)))
640 (let ((item (car order)))
643 (output-sql (car item) database)
644 (format *sql-stream* " ~A" (cadr item)))
646 (output-sql item database)))
648 (write-char #\, *sql-stream*))))
649 (output-sql group-by database)))
651 (write-string " HAVING " *sql-stream*)
652 (output-sql having database))
654 (write-string " ORDER BY " *sql-stream*)
656 (do ((order order-by (cdr order)))
658 (let ((item (car order)))
661 (output-sql (car item) database)
662 (format *sql-stream* " ~A" (cadr item)))
664 (output-sql item database)))
666 (write-char #\, *sql-stream*))))
667 (output-sql order-by database)))
668 (when (and limit (not (eq :odbc (database-type database))))
669 (write-string " LIMIT " *sql-stream*)
670 (output-sql limit database))
672 (write-string " OFFSET " *sql-stream*)
673 (output-sql offset database))
675 (write-string ")" *sql-stream*))
677 (write-char #\Space *sql-stream*)
678 (output-sql set-operation database)))
681 (defmethod output-sql ((query sql-object-query) database)
682 (declare (ignore database))
683 (with-slots (objects)
686 (format *sql-stream* "(~{~A~^ ~})" objects))))
691 (defclass sql-insert (%sql-expression)
705 "An SQL INSERT statement."))
707 (defmethod output-sql ((ins sql-insert) database)
708 (with-slots (into attributes values query)
710 (write-string "INSERT INTO " *sql-stream*)
713 (string (sql-expression :table into))
717 (write-char #\Space *sql-stream*)
718 (output-sql attributes database))
720 (write-string " VALUES " *sql-stream*)
721 (output-sql values database))
723 (write-char #\Space *sql-stream*)
724 (output-sql query database)))
729 (defclass sql-delete (%sql-expression)
737 "An SQL DELETE statement."))
739 (defmethod output-sql ((stmt sql-delete) database)
740 (with-slots (from where)
742 (write-string "DELETE FROM " *sql-stream*)
744 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
745 (t (output-sql from database)))
747 (write-string " WHERE " *sql-stream*)
748 (output-sql where database)))
753 (defclass sql-update (%sql-expression)
766 (:documentation "An SQL UPDATE statement."))
768 (defmethod output-sql ((expr sql-update) database)
769 (with-slots (table where attributes values)
771 (flet ((update-assignments ()
772 (mapcar #'(lambda (a b)
773 (make-instance 'sql-assignment-exp
775 :sub-expressions (list a b)))
777 (write-string "UPDATE " *sql-stream*)
778 (output-sql table database)
779 (write-string " SET " *sql-stream*)
780 (output-sql (apply #'vector (update-assignments)) database)
782 (write-string " WHERE " *sql-stream*)
783 (output-sql where database))))
788 (defclass sql-create-table (%sql-expression)
799 :initarg :transactions
802 "An SQL CREATE TABLE statement."))
804 ;; Here's a real warhorse of a function!
806 (declaim (inline listify))
812 (defmethod output-sql ((stmt sql-create-table) database)
813 (flet ((output-column (column-spec)
814 (destructuring-bind (name type &optional db-type &rest constraints)
816 (let ((type (listify type)))
817 (output-sql name database)
818 (write-char #\Space *sql-stream*)
820 (if (stringp db-type) db-type ; override definition
821 (database-get-type-specifier (car type) (cdr type) database
822 (database-underlying-type database)))
824 (let ((constraints (database-constraint-statement
825 (if (and db-type (symbolp db-type))
826 (cons db-type constraints)
830 (write-string " " *sql-stream*)
831 (write-string constraints *sql-stream*)))))))
832 (with-slots (name columns modifiers transactions)
834 (write-string "CREATE TABLE " *sql-stream*)
836 (string (format *sql-stream* "~s" (sql-escape name)))
837 (symbol (write-string (sql-escape name) *sql-stream*))
838 (sql-ident (output-sql name database)))
839 (write-string " (" *sql-stream*)
840 (do ((column columns (cdr column)))
842 (output-column (car column)))
843 (output-column (car column))
844 (write-string ", " *sql-stream*))
846 (do ((modifier (listify modifiers) (cdr modifier)))
848 (write-string ", " *sql-stream*)
849 (write-string (car modifier) *sql-stream*)))
850 (write-char #\) *sql-stream*)
851 (when (and (eq :mysql (database-underlying-type database))
853 (db-type-transaction-capable? :mysql database))
854 (write-string " Type=InnoDB" *sql-stream*))))
860 (defclass sql-create-view (%sql-expression)
861 ((name :initarg :name :initform nil)
862 (column-list :initarg :column-list :initform nil)
863 (query :initarg :query :initform nil)
864 (with-check-option :initarg :with-check-option :initform nil))
865 (:documentation "An SQL CREATE VIEW statement."))
867 (defmethod output-sql ((stmt sql-create-view) database)
868 (with-slots (name column-list query with-check-option) stmt
869 (write-string "CREATE VIEW " *sql-stream*)
870 (output-sql name database)
871 (when column-list (write-string " " *sql-stream*)
872 (output-sql (listify column-list) database))
873 (write-string " AS " *sql-stream*)
874 (output-sql query database)
875 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
879 ;; DATABASE-OUTPUT-SQL
882 (defmethod database-output-sql ((str string) database)
883 (declare (optimize (speed 3) (safety 1)
884 #+cmu (extensions:inhibit-warnings 3)))
885 (let ((len (length str)))
886 (declare (type fixnum len))
889 ((and (null (position #\' str))
890 (null (position #\\ str)))
891 (concatenate 'string "'" str "'"))
893 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
894 (declare (simple-string buf))
897 ((= i len) (subseq buf 0 (1+ j)))
898 (declare (type fixnum i j))
899 (let ((char (aref str i)))
900 (declare (character char))
901 (cond ((char= char #\')
902 (setf (aref buf j) #\')
904 (setf (aref buf j) #\'))
905 ((and (char= char #\\)
906 ;; MTP: only escape backslash with pgsql/mysql
907 (member (database-underlying-type database)
908 '(:postgresql :mysql)
910 (setf (aref buf j) #\\)
912 (setf (aref buf j) #\\))
914 (setf (aref buf j) char))))))))))
916 (let ((keyword-package (symbol-package :foo)))
917 (defmethod database-output-sql ((sym symbol) database)
920 (if (equal (symbol-package sym) keyword-package)
921 (concatenate 'string "'" (string sym) "'")
922 (symbol-name sym)))))
924 (defmethod database-output-sql ((tee (eql t)) database)
926 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
929 (string (format nil "'~A'" val))
930 (integer (format nil "~A" val)))))
933 #+nil(defmethod database-output-sql ((tee (eql t)) database)
934 (declare (ignore database))
937 (defmethod database-output-sql ((num number) database)
938 (declare (ignore database))
939 (number-to-sql-string num))
941 (defmethod database-output-sql ((arg list) database)
944 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
945 (sql-output val database))
948 (defmethod database-output-sql ((arg vector) database)
949 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
950 (sql-output val database))
953 (defmethod output-sql-hash-key ((arg vector) database)
954 (list 'vector (map 'list (lambda (arg)
955 (or (output-sql-hash-key arg database)
956 (return-from output-sql-hash-key nil)))
959 (defmethod database-output-sql ((self wall-time) database)
960 (declare (ignore database))
961 (db-timestring self))
963 (defmethod database-output-sql ((self date) database)
964 (declare (ignore database))
965 (db-datestring self))
967 (defmethod database-output-sql ((self duration) database)
968 (declare (ignore database))
969 (format nil "'~a'" (duration-timestring self)))
972 (defmethod database-output-sql ((self money) database)
973 (database-output-sql (slot-value self 'odcl::units) database))
975 (defmethod database-output-sql (thing database)
979 (error 'sql-user-error
982 "No type conversion to SQL for ~A is defined for DB ~A."
983 (type-of thing) (type-of database)))))
987 ;; Column constraint types and conversion to SQL
990 (defparameter *constraint-types*
992 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
993 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
994 (cons (symbol-name-default-case "NOT") "NOT")
995 (cons (symbol-name-default-case "NULL") "NULL")
996 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
997 (cons (symbol-name-default-case "KEY") "KEY")
998 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
999 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
1000 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
1001 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
1002 (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;Added Identity for MS-SQLServer support
1005 (defmethod database-constraint-statement (constraint-list database)
1006 (declare (ignore database))
1007 (make-constraints-description constraint-list))
1009 (defun make-constraints-description (constraint-list)
1012 (do ((constraint constraint-list (cdr constraint)))
1013 ((null constraint) string)
1014 (let ((output (assoc (symbol-name (car constraint))
1018 (error 'sql-user-error
1019 :message (format nil "unsupported column constraint '~A'"
1021 (setq string (concatenate 'string string (cdr output))))
1022 (if (< 1 (length constraint))
1023 (setq string (concatenate 'string string " "))))))))