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)
26 (progv '(*sql-stream*)
27 `(,(make-string-output-stream))
28 (output-sql sql-expr database)
29 (get-output-stream-string *sql-stream*)))
32 (defclass %sql-expression ()
35 (defmethod output-sql ((expr %sql-expression) database)
36 (declare (ignore database))
37 (write-string +null-string+ *sql-stream*))
39 (defmethod print-object ((self %sql-expression) stream)
40 (print-unreadable-object
42 (write-string (sql-output self) stream)))
44 ;; For straight up strings
46 (defclass sql (%sql-expression)
50 (:documentation "A literal SQL expression."))
52 (defmethod make-load-form ((sql sql) &optional environment)
53 (declare (ignore environment))
56 `(make-instance 'sql :string ',text)))
58 (defmethod output-sql ((expr sql) database)
59 (declare (ignore database))
60 (write-string (slot-value expr 'text) *sql-stream*)
63 (defmethod print-object ((ident sql) stream)
64 (format stream "#<~S \"~A\">"
66 (sql-output ident nil)))
68 ;; For SQL Identifiers of generic type
69 (defclass sql-ident (%sql-expression)
73 (:documentation "An SQL identifer."))
75 (defmethod make-load-form ((sql sql-ident) &optional environment)
76 (declare (ignore environment))
79 `(make-instance 'sql-ident :name ',name)))
81 (defvar *output-hash* (make-hash-table :test #'equal))
83 (defmethod output-sql-hash-key (expr database)
84 (declare (ignore expr database))
88 (defmethod output-sql :around ((sql t) database)
89 (let* ((hash-key (output-sql-hash-key sql database))
90 (hash-value (when hash-key (gethash hash-key *output-hash*))))
91 (cond ((and hash-key hash-value)
92 (write-string hash-value *sql-stream*))
94 (let ((*sql-stream* (make-string-output-stream)))
96 (setf hash-value (get-output-stream-string *sql-stream*))
97 (setf (gethash hash-key *output-hash*) hash-value))
98 (write-string hash-value *sql-stream*))
100 (call-next-method)))))
102 (defmethod output-sql ((expr sql-ident) database)
103 (with-slots (name) expr
105 (convert-to-db-default-case
108 (symbol (symbol-name name)))
113 ;; For SQL Identifiers for attributes
115 (defclass sql-ident-attribute (sql-ident)
122 (:documentation "An SQL Attribute identifier."))
124 (defmethod collect-table-refs (sql)
125 (declare (ignore sql))
128 (defmethod collect-table-refs ((sql sql-ident-attribute))
129 (let ((qual (slot-value sql 'qualifier)))
130 (if (and qual (symbolp (slot-value sql 'qualifier)))
131 (list (make-instance 'sql-ident-table :name
132 (slot-value sql 'qualifier))))))
134 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
135 (declare (ignore environment))
136 (with-slots (qualifier type name)
138 `(make-instance 'sql-ident-attribute :name ',name
139 :qualifier ',qualifier
142 (defmethod output-sql ((expr sql-ident-attribute) database)
143 (with-slots (qualifier name type) expr
144 (if (and (not qualifier) (not type))
146 ;; Honor care of name
148 (write-string name *sql-stream*))
150 (write-string (sql-escape (convert-to-db-default-case
151 (symbol-name name) database)) *sql-stream*)))
153 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
154 ;;; should not be output in SQL statements
156 (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
158 (convert-to-db-default-case (sql-escape qualifier) database))
159 (sql-escape (convert-to-db-default-case name database))
161 (convert-to-db-default-case (symbol-name type) database)))
162 (format *sql-stream* "~@[~A.~]~A"
165 (string (format nil "~s" qualifier))
166 (t (convert-to-db-default-case (sql-escape qualifier)
168 (sql-escape (convert-to-db-default-case name database))))
171 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
172 (declare (ignore database))
173 (with-slots (qualifier name type)
175 (list 'sql-ident-attribute qualifier name type)))
177 ;; For SQL Identifiers for tables
178 (defclass sql-ident-table (sql-ident)
180 :initarg :table-alias :initform nil))
181 (:documentation "An SQL table identifier."))
183 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
184 (declare (ignore environment))
185 (with-slots (alias name)
187 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
189 (defmethod output-sql ((expr sql-ident-table) database)
190 (with-slots (name alias)
193 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
195 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
196 (write-char #\Space *sql-stream*)
197 (format *sql-stream* "~s" alias))))
201 (defmethod database-output-sql ((self duration) database)
202 (declare (ignore database))
203 (format nil "'~a'" (duration-timestring self)))
205 (defmethod database-output-sql ((self money) database)
206 (database-output-sql (slot-value self 'odcl::units) database))
210 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
211 (declare (ignore database))
212 (with-slots (name alias)
214 (list 'sql-ident-table name alias)))
216 (defclass sql-relational-exp (%sql-expression)
221 :initarg :sub-expressions
223 (:documentation "An SQL relational expression."))
225 (defmethod collect-table-refs ((sql sql-relational-exp))
227 (dolist (exp (slot-value sql 'sub-expressions))
228 (let ((refs (collect-table-refs exp)))
229 (if refs (setf tabs (append refs tabs)))))
230 (remove-duplicates tabs
231 :test (lambda (tab1 tab2)
232 (equal (slot-value tab1 'name)
233 (slot-value tab2 'name))))))
238 ;; Write SQL for relational operators (like 'AND' and 'OR').
239 ;; should do arity checking of subexpressions
241 (defmethod output-sql ((expr sql-relational-exp) database)
242 (with-slots (operator sub-expressions)
244 (let ((subs (if (consp (car sub-expressions))
245 (car sub-expressions)
247 (write-char #\( *sql-stream*)
248 (do ((sub subs (cdr sub)))
249 ((null (cdr sub)) (output-sql (car sub) database))
250 (output-sql (car sub) database)
251 (write-char #\Space *sql-stream*)
252 (output-sql operator database)
253 (write-char #\Space *sql-stream*))
254 (write-char #\) *sql-stream*)))
257 (defclass sql-upcase-like (sql-relational-exp)
259 (:documentation "An SQL 'like' that upcases its arguments."))
261 ;; Write SQL for relational operators (like 'AND' and 'OR').
262 ;; should do arity checking of subexpressions
264 (defmethod output-sql ((expr sql-upcase-like) database)
265 (flet ((write-term (term)
266 (write-string "upper(" *sql-stream*)
267 (output-sql term database)
268 (write-char #\) *sql-stream*)))
269 (with-slots (sub-expressions)
271 (let ((subs (if (consp (car sub-expressions))
272 (car sub-expressions)
274 (write-char #\( *sql-stream*)
275 (do ((sub subs (cdr sub)))
276 ((null (cdr sub)) (write-term (car sub)))
277 (write-term (car sub))
278 (write-string " LIKE " *sql-stream*))
279 (write-char #\) *sql-stream*))))
282 (defclass sql-assignment-exp (sql-relational-exp)
284 (:documentation "An SQL Assignment expression."))
287 (defmethod output-sql ((expr sql-assignment-exp) database)
288 (with-slots (operator sub-expressions)
290 (do ((sub sub-expressions (cdr sub)))
291 ((null (cdr sub)) (output-sql (car sub) database))
292 (output-sql (car sub) database)
293 (write-char #\Space *sql-stream*)
294 (output-sql operator database)
295 (write-char #\Space *sql-stream*)))
298 (defclass sql-value-exp (%sql-expression)
306 "An SQL value expression.")
309 (defmethod collect-table-refs ((sql sql-value-exp))
311 (if (listp (slot-value sql 'components))
313 (dolist (exp (slot-value sql 'components))
314 (let ((refs (collect-table-refs exp)))
315 (if refs (setf tabs (append refs tabs)))))
316 (remove-duplicates tabs
317 :test (lambda (tab1 tab2)
318 (equal (slot-value tab1 'name)
319 (slot-value tab2 'name)))))
324 (defmethod output-sql ((expr sql-value-exp) database)
325 (with-slots (modifier components)
329 (write-char #\( *sql-stream*)
330 (output-sql modifier database)
331 (write-char #\Space *sql-stream*)
332 (output-sql components database)
333 (write-char #\) *sql-stream*))
334 (output-sql components database))))
336 (defclass sql-typecast-exp (sql-value-exp)
338 (:documentation "An SQL typecast expression."))
340 (defmethod output-sql ((expr sql-typecast-exp) database)
341 (database-output-sql expr database))
343 (defmethod database-output-sql ((expr sql-typecast-exp) database)
344 (with-slots (components)
346 (output-sql components database)))
349 (defmethod collect-table-refs ((sql sql-typecast-exp))
350 (when (slot-value sql 'components)
351 (collect-table-refs (slot-value sql 'components))))
353 (defclass sql-function-exp (%sql-expression)
361 "An SQL function expression."))
363 (defmethod collect-table-refs ((sql sql-function-exp))
365 (dolist (exp (slot-value sql 'components))
366 (let ((refs (collect-table-refs exp)))
367 (if refs (setf tabs (append refs tabs)))))
368 (remove-duplicates tabs
369 :test (lambda (tab1 tab2)
370 (equal (slot-value tab1 'name)
371 (slot-value tab2 'name))))))
372 (defvar *in-subselect* nil)
374 (defmethod output-sql ((expr sql-function-exp) database)
375 (with-slots (name args)
377 (output-sql name database)
378 (let ((*in-subselect* nil)) ;; aboid double parens
379 (when args (output-sql args database))))
383 (defclass sql-between-exp (sql-function-exp)
385 (:documentation "An SQL between expression."))
387 (defmethod output-sql ((expr sql-between-exp) database)
388 (with-slots (name args)
390 (output-sql (first args) database)
391 (write-string " BETWEEN " *sql-stream*)
392 (output-sql (second args) database)
393 (write-string " AND " *sql-stream*)
394 (output-sql (third args) database))
397 (defclass sql-query-modifier-exp (%sql-expression)
398 ((modifier :initarg :modifier :initform nil)
399 (components :initarg :components :initform nil))
400 (:documentation "An SQL query modifier expression."))
402 (defmethod output-sql ((expr sql-query-modifier-exp) database)
403 (with-slots (modifier components)
405 (output-sql modifier database)
406 (write-string " " *sql-stream*)
407 (output-sql (car components) database)
409 (mapc #'(lambda (comp)
410 (write-string ", " *sql-stream*)
411 (output-sql comp database))
415 (defclass sql-set-exp (%sql-expression)
420 :initarg :sub-expressions
422 (:documentation "An SQL set expression."))
424 (defmethod collect-table-refs ((sql sql-set-exp))
426 (dolist (exp (slot-value sql 'sub-expressions))
427 (let ((refs (collect-table-refs exp)))
428 (if refs (setf tabs (append refs tabs)))))
429 (remove-duplicates tabs
430 :test (lambda (tab1 tab2)
431 (equal (slot-value tab1 'name)
432 (slot-value tab2 'name))))))
434 (defmethod output-sql ((expr sql-set-exp) database)
435 (with-slots (operator sub-expressions)
437 (let ((subs (if (consp (car sub-expressions))
438 (car sub-expressions)
440 (when (= (length subs) 1)
441 (output-sql operator database)
442 (write-char #\Space *sql-stream*))
443 (do ((sub subs (cdr sub)))
444 ((null (cdr sub)) (output-sql (car sub) database))
445 (output-sql (car sub) database)
446 (write-char #\Space *sql-stream*)
447 (output-sql operator database)
448 (write-char #\Space *sql-stream*))))
451 (defclass sql-query (%sql-expression)
462 :initarg :set-operation
494 (:documentation "An SQL SELECT query."))
496 (defclass sql-object-query (%sql-expression)
510 (defmethod collect-table-refs ((sql sql-query))
511 (remove-duplicates (collect-table-refs (slot-value sql 'where))
512 :test (lambda (tab1 tab2)
513 (equal (slot-value tab1 'name)
514 (slot-value tab2 'name)))))
516 (defvar *select-arguments*
517 '(:all :database :distinct :flatp :from :group-by :having :order-by
518 :set-operation :where :offset :limit :inner-join :on
519 ;; below keywords are not a SQL argument, but these keywords may terminate select
522 (defun query-arg-p (sym)
523 (member sym *select-arguments*))
525 (defun query-get-selections (select-args)
526 "Return two values: the list of select-args up to the first keyword,
527 uninclusive, and the args from that keyword to the end."
528 (let ((first-key-arg (position-if #'query-arg-p select-args)))
530 (values (subseq select-args 0 first-key-arg)
531 (subseq select-args first-key-arg))
534 (defun make-query (&rest args)
535 (flet ((select-objects (target-args)
537 (every #'(lambda (arg)
539 (find-class arg nil)))
541 (multiple-value-bind (selections arglist)
542 (query-get-selections args)
543 (if (select-objects selections)
544 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
545 (make-instance 'sql-object-query :objects selections
546 :flatp flatp :refresh refresh
548 (destructuring-bind (&key all flatp set-operation distinct from where
549 group-by having order-by
550 offset limit inner-join on &allow-other-keys)
552 (if (null selections)
553 (error "No target columns supplied to select statement."))
555 (error "No source tables supplied to select statement."))
556 (make-instance 'sql-query :selections selections
557 :all all :flatp flatp :set-operation set-operation
558 :distinct distinct :from from :where where
559 :limit limit :offset offset
560 :group-by group-by :having having :order-by order-by
561 :inner-join inner-join :on on))))))
563 (defmethod output-sql ((query sql-query) database)
564 (with-slots (distinct selections from where group-by having order-by
565 limit offset inner-join on all set-operation)
568 (write-string "(" *sql-stream*))
569 (write-string "SELECT " *sql-stream*)
571 (write-string "ALL " *sql-stream*))
572 (when (and distinct (not all))
573 (write-string "DISTINCT " *sql-stream*)
574 (unless (eql t distinct)
575 (write-string "ON " *sql-stream*)
576 (output-sql distinct database)
577 (write-char #\Space *sql-stream*)))
578 (output-sql (apply #'vector selections) database)
580 (write-string " FROM " *sql-stream*)
581 (flet ((ident-table-equal (a b)
582 (and (if (and (eql (type-of a) 'sql-ident-table)
583 (eql (type-of b) 'sql-ident-table))
584 (string-equal (slot-value a 'alias)
585 (slot-value b 'alias))
587 (string-equal (symbol-name (slot-value a 'name))
588 (symbol-name (slot-value b 'name))))))
590 (list (output-sql (apply #'vector
591 (remove-duplicates from
592 :test #'ident-table-equal))
594 (string (write-string from *sql-stream*))
595 (t (output-sql from database)))))
597 (write-string " INNER JOIN " *sql-stream*)
598 (output-sql inner-join database))
600 (write-string " ON " *sql-stream*)
601 (output-sql on database))
603 (write-string " WHERE " *sql-stream*)
604 (let ((*in-subselect* t))
605 (output-sql where database)))
607 (write-string " GROUP BY " *sql-stream*)
608 (output-sql group-by database))
610 (write-string " HAVING " *sql-stream*)
611 (output-sql having database))
613 (write-string " ORDER BY " *sql-stream*)
615 (do ((order order-by (cdr order)))
617 (let ((item (car order)))
620 (output-sql (car item) database)
621 (format *sql-stream* " ~A" (cadr item)))
623 (output-sql item database)))
625 (write-char #\, *sql-stream*))))
626 (output-sql order-by database)))
628 (write-string " LIMIT " *sql-stream*)
629 (output-sql limit database))
631 (write-string " OFFSET " *sql-stream*)
632 (output-sql offset database))
634 (write-string ")" *sql-stream*))
636 (write-char #\Space *sql-stream*)
637 (output-sql set-operation database)))
640 (defmethod output-sql ((query sql-object-query) database)
641 (declare (ignore database))
642 (with-slots (objects)
645 (format *sql-stream* "(~{~A~^ ~})" objects))))
650 (defclass sql-insert (%sql-expression)
664 "An SQL INSERT statement."))
666 (defmethod output-sql ((ins sql-insert) database)
667 (with-slots (into attributes values query)
669 (write-string "INSERT INTO " *sql-stream*)
672 (string (sql-expression :attribute into))
676 (write-char #\Space *sql-stream*)
677 (output-sql attributes database))
679 (write-string " VALUES " *sql-stream*)
680 (output-sql values database))
682 (write-char #\Space *sql-stream*)
683 (output-sql query database)))
688 (defclass sql-delete (%sql-expression)
696 "An SQL DELETE statement."))
698 (defmethod output-sql ((stmt sql-delete) database)
699 (with-slots (from where)
701 (write-string "DELETE FROM " *sql-stream*)
703 (symbol (write-string (sql-escape from) *sql-stream*))
704 (t (output-sql from database)))
706 (write-string " WHERE " *sql-stream*)
707 (output-sql where database)))
712 (defclass sql-update (%sql-expression)
725 (:documentation "An SQL UPDATE statement."))
727 (defmethod output-sql ((expr sql-update) database)
728 (with-slots (table where attributes values)
730 (flet ((update-assignments ()
731 (mapcar #'(lambda (a b)
732 (make-instance 'sql-assignment-exp
734 :sub-expressions (list a b)))
736 (write-string "UPDATE " *sql-stream*)
737 (output-sql table database)
738 (write-string " SET " *sql-stream*)
739 (output-sql (apply #'vector (update-assignments)) database)
741 (write-string " WHERE " *sql-stream*)
742 (output-sql where database))))
747 (defclass sql-create-table (%sql-expression)
758 :initarg :transactions
761 "An SQL CREATE TABLE statement."))
763 ;; Here's a real warhorse of a function!
765 (declaim (inline listify))
771 (defmethod output-sql ((stmt sql-create-table) database)
772 (flet ((output-column (column-spec)
773 (destructuring-bind (name type &optional db-type &rest constraints)
775 (let ((type (listify type)))
776 (output-sql name database)
777 (write-char #\Space *sql-stream*)
779 (if (stringp db-type) db-type ; override definition
780 (database-get-type-specifier (car type) (cdr type) database
781 (database-underlying-type database)))
783 (let ((constraints (database-constraint-statement
784 (if (and db-type (symbolp db-type))
785 (cons db-type constraints)
789 (write-string " " *sql-stream*)
790 (write-string constraints *sql-stream*)))))))
791 (with-slots (name columns modifiers transactions)
793 (write-string "CREATE TABLE " *sql-stream*)
794 (output-sql name database)
795 (write-string " (" *sql-stream*)
796 (do ((column columns (cdr column)))
798 (output-column (car column)))
799 (output-column (car column))
800 (write-string ", " *sql-stream*))
802 (do ((modifier (listify modifiers) (cdr modifier)))
804 (write-string ", " *sql-stream*)
805 (write-string (car modifier) *sql-stream*)))
806 (write-char #\) *sql-stream*)
807 (when (and (eq :mysql (database-underlying-type database))
809 (db-type-transaction-capable? :mysql database))
810 (write-string " Type=InnoDB" *sql-stream*))))
816 (defclass sql-create-view (%sql-expression)
817 ((name :initarg :name :initform nil)
818 (column-list :initarg :column-list :initform nil)
819 (query :initarg :query :initform nil)
820 (with-check-option :initarg :with-check-option :initform nil))
821 (:documentation "An SQL CREATE VIEW statement."))
823 (defmethod output-sql ((stmt sql-create-view) database)
824 (with-slots (name column-list query with-check-option) stmt
825 (write-string "CREATE VIEW " *sql-stream*)
826 (output-sql name database)
827 (when column-list (write-string " " *sql-stream*)
828 (output-sql (listify column-list) database))
829 (write-string " AS " *sql-stream*)
830 (output-sql query database)
831 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
835 ;; Column constraint types
837 (defparameter *constraint-types*
839 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
840 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
841 (cons (symbol-name-default-case "NOT") "NOT")
842 (cons (symbol-name-default-case "NULL") "NULL")
843 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
844 (cons (symbol-name-default-case "KEY") "KEY")))
847 ;; Convert type spec to sql syntax
850 (defmethod database-constraint-statement (constraint-list database)
851 (declare (ignore database))
852 (make-constraints-description constraint-list))
854 (defun make-constraints-description (constraint-list)
857 (do ((constraint constraint-list (cdr constraint)))
858 ((null constraint) string)
859 (let ((output (assoc (symbol-name (car constraint))
863 (error 'sql-user-error
864 :message (format nil "unsupported column constraint '~A'"
866 (setq string (concatenate 'string string (cdr output))))
867 (if (< 1 (length constraint))
868 (setq string (concatenate 'string string " "))))))))