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*)
589 (when (and limit (eql :mssql (database-underlying-type database)))
590 (write-string " TOP " *sql-stream*)
591 (output-sql limit database)
592 (write-string " " *sql-stream*))
594 (write-string " ALL " *sql-stream*))
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 (write-string " WHERE " *sql-stream*)
629 (let ((*in-subselect* t))
630 (output-sql where database)))
632 (write-string " GROUP BY " *sql-stream*)
634 (do ((order group-by (cdr order)))
636 (let ((item (car order)))
639 (output-sql (car item) database)
640 (format *sql-stream* " ~A" (cadr item)))
642 (output-sql item database)))
644 (write-char #\, *sql-stream*))))
645 (output-sql group-by database)))
647 (write-string " HAVING " *sql-stream*)
648 (output-sql having database))
650 (write-string " ORDER BY " *sql-stream*)
652 (do ((order order-by (cdr order)))
654 (let ((item (car order)))
657 (output-sql (car item) database)
658 (format *sql-stream* " ~A" (cadr item)))
660 (output-sql item database)))
662 (write-char #\, *sql-stream*))))
663 (output-sql order-by database)))
664 (when (and limit (not (eql :mssql (database-underlying-type database))))
665 (write-string " LIMIT " *sql-stream*)
666 (output-sql limit database))
668 (write-string " OFFSET " *sql-stream*)
669 (output-sql offset database))
671 (write-string ")" *sql-stream*))
673 (write-char #\Space *sql-stream*)
674 (output-sql set-operation database)))
677 (defmethod output-sql ((query sql-object-query) database)
678 (declare (ignore database))
679 (with-slots (objects)
682 (format *sql-stream* "(~{~A~^ ~})" objects))))
687 (defclass sql-insert (%sql-expression)
701 "An SQL INSERT statement."))
703 (defmethod output-sql ((ins sql-insert) database)
704 (with-slots (into attributes values query)
706 (write-string "INSERT INTO " *sql-stream*)
709 (string (sql-expression :table into))
713 (write-char #\Space *sql-stream*)
714 (output-sql attributes database))
716 (write-string " VALUES " *sql-stream*)
717 (output-sql values database))
719 (write-char #\Space *sql-stream*)
720 (output-sql query database)))
725 (defclass sql-delete (%sql-expression)
733 "An SQL DELETE statement."))
735 (defmethod output-sql ((stmt sql-delete) database)
736 (with-slots (from where)
738 (write-string "DELETE FROM " *sql-stream*)
740 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
741 (t (output-sql from database)))
743 (write-string " WHERE " *sql-stream*)
744 (output-sql where database)))
749 (defclass sql-update (%sql-expression)
762 (:documentation "An SQL UPDATE statement."))
764 (defmethod output-sql ((expr sql-update) database)
765 (with-slots (table where attributes values)
767 (flet ((update-assignments ()
768 (mapcar #'(lambda (a b)
769 (make-instance 'sql-assignment-exp
771 :sub-expressions (list a b)))
773 (write-string "UPDATE " *sql-stream*)
774 (output-sql table database)
775 (write-string " SET " *sql-stream*)
776 (output-sql (apply #'vector (update-assignments)) database)
778 (write-string " WHERE " *sql-stream*)
779 (output-sql where database))))
784 (defclass sql-create-table (%sql-expression)
795 :initarg :transactions
798 "An SQL CREATE TABLE statement."))
800 ;; Here's a real warhorse of a function!
802 (declaim (inline listify))
808 (defmethod output-sql ((stmt sql-create-table) database)
809 (flet ((output-column (column-spec)
810 (destructuring-bind (name type &optional db-type &rest constraints)
812 (let ((type (listify type)))
813 (output-sql name database)
814 (write-char #\Space *sql-stream*)
816 (if (stringp db-type) db-type ; override definition
817 (database-get-type-specifier (car type) (cdr type) database
818 (database-underlying-type database)))
820 (let ((constraints (database-constraint-statement
821 (if (and db-type (symbolp db-type))
822 (cons db-type constraints)
826 (write-string " " *sql-stream*)
827 (write-string constraints *sql-stream*)))))))
828 (with-slots (name columns modifiers transactions)
830 (write-string "CREATE TABLE " *sql-stream*)
832 (string (format *sql-stream* "~s" (sql-escape name)))
833 (symbol (write-string (sql-escape name) *sql-stream*))
834 (sql-ident (output-sql name database)))
835 (write-string " (" *sql-stream*)
836 (do ((column columns (cdr column)))
838 (output-column (car column)))
839 (output-column (car column))
840 (write-string ", " *sql-stream*))
842 (do ((modifier (listify modifiers) (cdr modifier)))
844 (write-string ", " *sql-stream*)
845 (write-string (car modifier) *sql-stream*)))
846 (write-char #\) *sql-stream*)
847 (when (and (eq :mysql (database-underlying-type database))
849 (db-type-transaction-capable? :mysql database))
850 (write-string " Type=InnoDB" *sql-stream*))))
856 (defclass sql-create-view (%sql-expression)
857 ((name :initarg :name :initform nil)
858 (column-list :initarg :column-list :initform nil)
859 (query :initarg :query :initform nil)
860 (with-check-option :initarg :with-check-option :initform nil))
861 (:documentation "An SQL CREATE VIEW statement."))
863 (defmethod output-sql ((stmt sql-create-view) database)
864 (with-slots (name column-list query with-check-option) stmt
865 (write-string "CREATE VIEW " *sql-stream*)
866 (output-sql name database)
867 (when column-list (write-string " " *sql-stream*)
868 (output-sql (listify column-list) database))
869 (write-string " AS " *sql-stream*)
870 (output-sql query database)
871 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
875 ;; DATABASE-OUTPUT-SQL
878 (defmethod database-output-sql ((str string) database)
879 (declare (optimize (speed 3) (safety 1)
880 #+cmu (extensions:inhibit-warnings 3)))
881 (let ((len (length str)))
882 (declare (type fixnum len))
885 ((and (null (position #\' str))
886 (null (position #\\ str)))
887 (concatenate 'string "'" str "'"))
889 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
890 (declare (simple-string buf))
893 ((= i len) (subseq buf 0 (1+ j)))
894 (declare (type fixnum i j))
895 (let ((char (aref str i)))
896 (declare (character char))
897 (cond ((char= char #\')
898 (setf (aref buf j) #\')
900 (setf (aref buf j) #\'))
901 ((and (char= char #\\)
902 ;; MTP: only escape backslash with pgsql/mysql
903 (member (database-underlying-type database)
904 '(:postgresql :mysql)
906 (setf (aref buf j) #\\)
908 (setf (aref buf j) #\\))
910 (setf (aref buf j) char))))))))))
912 (let ((keyword-package (symbol-package :foo)))
913 (defmethod database-output-sql ((sym symbol) database)
916 (if (equal (symbol-package sym) keyword-package)
917 (concatenate 'string "'" (string sym) "'")
918 (symbol-name sym)))))
920 (defmethod database-output-sql ((tee (eql t)) database)
922 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
925 (string (format nil "'~A'" val))
926 (integer (format nil "~A" val)))))
929 #+nil(defmethod database-output-sql ((tee (eql t)) database)
930 (declare (ignore database))
933 (defmethod database-output-sql ((num number) database)
934 (declare (ignore database))
935 (number-to-sql-string num))
937 (defmethod database-output-sql ((arg list) database)
940 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
941 (sql-output val database))
944 (defmethod database-output-sql ((arg vector) database)
945 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
946 (sql-output val database))
949 (defmethod output-sql-hash-key ((arg vector) database)
950 (list 'vector (map 'list (lambda (arg)
951 (or (output-sql-hash-key arg database)
952 (return-from output-sql-hash-key nil)))
955 (defmethod database-output-sql ((self wall-time) database)
956 (declare (ignore database))
957 (db-timestring self))
959 (defmethod database-output-sql ((self date) database)
960 (declare (ignore database))
961 (db-datestring self))
963 (defmethod database-output-sql ((self duration) database)
964 (declare (ignore database))
965 (format nil "'~a'" (duration-timestring self)))
968 (defmethod database-output-sql ((self money) database)
969 (database-output-sql (slot-value self 'odcl::units) database))
971 (defmethod database-output-sql (thing database)
975 (error 'sql-user-error
978 "No type conversion to SQL for ~A is defined for DB ~A."
979 (type-of thing) (type-of database)))))
983 ;; Column constraint types and conversion to SQL
986 (defparameter *constraint-types*
988 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
989 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
990 (cons (symbol-name-default-case "NOT") "NOT")
991 (cons (symbol-name-default-case "NULL") "NULL")
992 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
993 (cons (symbol-name-default-case "KEY") "KEY")
994 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
995 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
996 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
997 (cons (symbol-name-default-case "DEFAULT") "DEFAULT")
998 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
999 (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;; added for sql-server support
1002 (defmethod database-constraint-statement (constraint-list database)
1003 (declare (ignore database))
1004 (make-constraints-description constraint-list))
1006 (defun make-constraints-description (constraint-list)
1009 (do ((constraint constraint-list (cdr constraint)))
1010 ((null constraint) string)
1011 (let ((output (assoc (symbol-name (car constraint))
1015 (error 'sql-user-error
1016 :message (format nil "unsupported column constraint '~A'"
1018 (setq string (concatenate 'string string (cdr output))))
1019 (when (equal (symbol-name (car constraint)) "DEFAULT")
1020 (setq constraint (cdr constraint))
1021 (setq string (concatenate 'string string " " (car constraint))))
1022 (if (< 1 (length constraint))
1023 (setq string (concatenate 'string string " "))))))))