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)
106 (convert-to-db-default-case
109 (symbol (symbol-name name)))
114 ;; For SQL Identifiers for attributes
116 (defclass sql-ident-attribute (sql-ident)
126 (:documentation "An SQL Attribute identifier."))
128 (defmethod collect-table-refs (sql)
129 (declare (ignore sql))
132 (defmethod collect-table-refs ((sql sql-ident-attribute))
133 (let ((qual (slot-value sql 'qualifier)))
134 (if (and qual (symbolp (slot-value sql 'qualifier)))
135 (list (make-instance 'sql-ident-table :name
136 (slot-value sql 'qualifier))))))
138 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
139 (declare (ignore environment))
140 (with-slots (qualifier type name)
142 `(make-instance 'sql-ident-attribute :name ',name
143 :qualifier ',qualifier
146 (defmethod output-sql ((expr sql-ident-attribute) database)
147 (with-slots (qualifier name type params)
149 (if (and (not qualifier) (not type))
150 (write-string (sql-escape (convert-to-db-default-case
151 (symbol-name name) database)) *sql-stream*)
152 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
153 ;;; should not be output in SQL statements
155 (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
157 (convert-to-db-default-case (sql-escape qualifier) database))
158 (sql-escape (convert-to-db-default-case name database))
160 (convert-to-db-default-case (symbol-name type) database)))
161 (format *sql-stream* "~@[~A.~]~A"
164 (string (format nil "~s" qualifier))
165 (t (convert-to-db-default-case (sql-escape qualifier)
167 (sql-escape (convert-to-db-default-case name database))))
170 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
171 (declare (ignore database))
172 (with-slots (qualifier name type params)
174 (list 'sql-ident-attribute qualifier name type params)))
176 ;; For SQL Identifiers for tables
177 (defclass sql-ident-table (sql-ident)
179 :initarg :table-alias :initform nil))
180 (:documentation "An SQL table identifier."))
182 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
183 (declare (ignore environment))
184 (with-slots (alias name)
186 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
188 (defun generate-sql (expr database)
189 (let ((*sql-stream* (make-string-output-stream)))
190 (output-sql expr database)
191 (get-output-stream-string *sql-stream*)))
193 (defmethod output-sql ((expr sql-ident-table) database)
194 (with-slots (name alias)
197 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
199 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
200 (write-char #\Space *sql-stream*)
201 (format *sql-stream* "~s" alias))))
205 (defmethod database-output-sql ((self duration) database)
206 (declare (ignore database))
207 (format nil "'~a'" (duration-timestring self)))
209 (defmethod database-output-sql ((self money) database)
210 (database-output-sql (slot-value self 'odcl::units) database))
214 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
215 (declare (ignore database))
216 (with-slots (name alias)
218 (list 'sql-ident-table name alias)))
220 (defclass sql-relational-exp (%sql-expression)
225 :initarg :sub-expressions
227 (:documentation "An SQL relational expression."))
229 (defmethod collect-table-refs ((sql sql-relational-exp))
231 (dolist (exp (slot-value sql 'sub-expressions))
232 (let ((refs (collect-table-refs exp)))
233 (if refs (setf tabs (append refs tabs)))))
234 (remove-duplicates tabs
235 :test (lambda (tab1 tab2)
236 (equal (slot-value tab1 'name)
237 (slot-value tab2 'name))))))
242 ;; Write SQL for relational operators (like 'AND' and 'OR').
243 ;; should do arity checking of subexpressions
245 (defmethod output-sql ((expr sql-relational-exp) database)
246 (with-slots (operator 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)))
253 ((null (cdr sub)) (output-sql (car sub) database))
254 (output-sql (car sub) database)
255 (write-char #\Space *sql-stream*)
256 (output-sql operator database)
257 (write-char #\Space *sql-stream*))
258 (write-char #\) *sql-stream*)))
261 (defclass sql-upcase-like (sql-relational-exp)
263 (:documentation "An SQL 'like' that upcases its arguments."))
265 ;; Write SQL for relational operators (like 'AND' and 'OR').
266 ;; should do arity checking of subexpressions
268 (defmethod output-sql ((expr sql-upcase-like) database)
269 (flet ((write-term (term)
270 (write-string "upper(" *sql-stream*)
271 (output-sql term database)
272 (write-char #\) *sql-stream*)))
273 (with-slots (sub-expressions)
275 (let ((subs (if (consp (car sub-expressions))
276 (car sub-expressions)
278 (write-char #\( *sql-stream*)
279 (do ((sub subs (cdr sub)))
280 ((null (cdr sub)) (write-term (car sub)))
281 (write-term (car sub))
282 (write-string " LIKE " *sql-stream*))
283 (write-char #\) *sql-stream*))))
286 (defclass sql-assignment-exp (sql-relational-exp)
288 (:documentation "An SQL Assignment expression."))
291 (defmethod output-sql ((expr sql-assignment-exp) database)
292 (with-slots (operator sub-expressions)
294 (do ((sub sub-expressions (cdr sub)))
295 ((null (cdr sub)) (output-sql (car sub) database))
296 (output-sql (car sub) database)
297 (write-char #\Space *sql-stream*)
298 (output-sql operator database)
299 (write-char #\Space *sql-stream*)))
302 (defclass sql-value-exp (%sql-expression)
310 "An SQL value expression.")
313 (defmethod collect-table-refs ((sql sql-value-exp))
315 (if (listp (slot-value sql 'components))
317 (dolist (exp (slot-value sql 'components))
318 (let ((refs (collect-table-refs exp)))
319 (if refs (setf tabs (append refs tabs)))))
320 (remove-duplicates tabs
321 :test (lambda (tab1 tab2)
322 (equal (slot-value tab1 'name)
323 (slot-value tab2 'name)))))
328 (defmethod output-sql ((expr sql-value-exp) database)
329 (with-slots (modifier components)
333 (write-char #\( *sql-stream*)
334 (output-sql modifier database)
335 (write-char #\Space *sql-stream*)
336 (output-sql components database)
337 (write-char #\) *sql-stream*))
338 (output-sql components database))))
340 (defclass sql-typecast-exp (sql-value-exp)
342 (:documentation "An SQL typecast expression."))
344 (defmethod output-sql ((expr sql-typecast-exp) database)
345 (database-output-sql expr database))
347 (defmethod database-output-sql ((expr sql-typecast-exp) database)
348 (with-slots (components)
350 (output-sql components database)))
353 (defmethod collect-table-refs ((sql sql-typecast-exp))
354 (when (slot-value sql 'components)
355 (collect-table-refs (slot-value sql 'components))))
357 (defclass sql-function-exp (%sql-expression)
365 "An SQL function expression."))
367 (defmethod collect-table-refs ((sql sql-function-exp))
369 (dolist (exp (slot-value sql 'components))
370 (let ((refs (collect-table-refs exp)))
371 (if refs (setf tabs (append refs tabs)))))
372 (remove-duplicates tabs
373 :test (lambda (tab1 tab2)
374 (equal (slot-value tab1 'name)
375 (slot-value tab2 'name))))))
377 (defmethod output-sql ((expr sql-function-exp) database)
378 (with-slots (name args)
380 (output-sql name database)
381 (when args (output-sql args database)))
385 (defclass sql-between-exp (sql-function-exp)
387 (:documentation "An SQL between expression."))
389 (defmethod output-sql ((expr sql-between-exp) database)
390 (with-slots (name args)
392 (output-sql (first args) database)
393 (write-string " BETWEEN " *sql-stream*)
394 (output-sql (second args) database)
395 (write-string " AND " *sql-stream*)
396 (output-sql (third args) database))
399 (defclass sql-query-modifier-exp (%sql-expression)
400 ((modifier :initarg :modifier :initform nil)
401 (components :initarg :components :initform nil))
402 (:documentation "An SQL query modifier expression."))
404 (defmethod output-sql ((expr sql-query-modifier-exp) database)
405 (with-slots (modifier components)
407 (output-sql modifier database)
408 (write-string " " *sql-stream*)
409 (output-sql (car components) database)
411 (mapc #'(lambda (comp)
412 (write-string ", " *sql-stream*)
413 (output-sql comp database))
417 (defclass sql-set-exp (%sql-expression)
422 :initarg :sub-expressions
424 (:documentation "An SQL set expression."))
426 (defmethod collect-table-refs ((sql sql-set-exp))
428 (dolist (exp (slot-value sql 'sub-expressions))
429 (let ((refs (collect-table-refs exp)))
430 (if refs (setf tabs (append refs tabs)))))
431 (remove-duplicates tabs
432 :test (lambda (tab1 tab2)
433 (equal (slot-value tab1 'name)
434 (slot-value tab2 'name))))))
436 (defmethod output-sql ((expr sql-set-exp) database)
437 (with-slots (operator sub-expressions)
439 (let ((subs (if (consp (car sub-expressions))
440 (car sub-expressions)
442 (when (= (length subs) 1)
443 (output-sql operator database)
444 (write-char #\Space *sql-stream*))
445 (do ((sub subs (cdr sub)))
446 ((null (cdr sub)) (output-sql (car sub) database))
447 (output-sql (car sub) database)
448 (write-char #\Space *sql-stream*)
449 (output-sql operator database)
450 (write-char #\Space *sql-stream*))))
453 (defclass sql-query (%sql-expression)
464 :initarg :set-operation
491 :initarg :order-by-descending
499 (:documentation "An SQL SELECT query."))
501 (defclass sql-object-query (%sql-expression)
515 (defmethod collect-table-refs ((sql sql-query))
516 (remove-duplicates (collect-table-refs (slot-value sql 'where))
517 :test (lambda (tab1 tab2)
518 (equal (slot-value tab1 'name)
519 (slot-value tab2 'name)))))
521 (defvar *select-arguments*
522 '(:all :database :distinct :flatp :from :group-by :having :order-by
523 :order-by-descending :set-operation :where :offset :limit
525 ;; below keywords are not a SQL argument, but these keywords may terminate select
528 (defun query-arg-p (sym)
529 (member sym *select-arguments*))
531 (defun query-get-selections (select-args)
532 "Return two values: the list of select-args up to the first keyword,
533 uninclusive, and the args from that keyword to the end."
534 (let ((first-key-arg (position-if #'query-arg-p select-args)))
536 (values (subseq select-args 0 first-key-arg)
537 (subseq select-args first-key-arg))
540 (defun make-query (&rest args)
541 (flet ((select-objects (target-args)
543 (every #'(lambda (arg)
545 (find-class arg nil)))
547 (multiple-value-bind (selections arglist)
548 (query-get-selections args)
549 (if (select-objects selections)
550 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
551 (make-instance 'sql-object-query :objects selections
552 :flatp flatp :refresh refresh
554 (destructuring-bind (&key all flatp set-operation distinct from where
555 group-by having order-by order-by-descending
556 offset limit inner-join on &allow-other-keys)
558 (if (null selections)
559 (error "No target columns supplied to select statement."))
561 (error "No source tables supplied to select statement."))
562 (make-instance 'sql-query :selections selections
563 :all all :flatp flatp :set-operation set-operation
564 :distinct distinct :from from :where where
565 :limit limit :offset offset
566 :group-by group-by :having having :order-by order-by
567 :order-by-descending order-by-descending
568 :inner-join inner-join :on on))))))
570 (defvar *in-subselect* nil)
572 (defmethod output-sql ((query sql-query) database)
573 (with-slots (distinct selections from where group-by having order-by
574 order-by-descending limit offset inner-join on
578 (write-string "(" *sql-stream*))
579 (write-string "SELECT " *sql-stream*)
581 (write-string "ALL " *sql-stream*))
582 (when (and distinct (not all))
583 (write-string "DISTINCT " *sql-stream*)
584 (unless (eql t distinct)
585 (write-string "ON " *sql-stream*)
586 (output-sql distinct database)
587 (write-char #\Space *sql-stream*)))
588 (output-sql (apply #'vector selections) database)
590 (write-string " FROM " *sql-stream*)
592 (list (output-sql (apply #'vector from) database))
593 (string (write-string
595 (convert-to-db-default-case from database)) *sql-stream*))
596 (t (output-sql from database))))
598 (write-string " INNER JOIN " *sql-stream*)
599 (output-sql inner-join database))
601 (write-string " ON " *sql-stream*)
602 (output-sql on database))
604 (write-string " WHERE " *sql-stream*)
605 (let ((*in-subselect* t))
606 (output-sql where database)))
608 (write-string " GROUP BY " *sql-stream*)
609 (output-sql group-by database))
611 (write-string " HAVING " *sql-stream*)
612 (output-sql having database))
614 (write-string " ORDER BY " *sql-stream*)
616 (do ((order order-by (cdr order)))
618 (let ((item (car order)))
621 (output-sql (car item) database)
622 (format *sql-stream* " ~A" (cadr item)))
624 (output-sql item database)))
626 (write-char #\, *sql-stream*))))
627 (output-sql order-by database)))
628 (when order-by-descending
629 (write-string " ORDER BY " *sql-stream*)
630 (if (listp order-by-descending)
631 (do ((order order-by-descending (cdr order)))
633 (output-sql (car order) database)
635 (write-char #\, *sql-stream*)))
636 (output-sql order-by-descending database))
637 (write-string " DESC " *sql-stream*))
639 (write-string " LIMIT " *sql-stream*)
640 (output-sql limit database))
642 (write-string " OFFSET " *sql-stream*)
643 (output-sql offset database))
645 (write-string ")" *sql-stream*))
647 (write-char #\Space *sql-stream*)
648 (output-sql set-operation database)))
651 (defmethod output-sql ((query sql-object-query) database)
652 (declare (ignore database))
653 (with-slots (objects)
656 (format *sql-stream* "(~{~A~^ ~})" objects))))
661 (defclass sql-insert (%sql-expression)
675 "An SQL INSERT statement."))
677 (defmethod output-sql ((ins sql-insert) database)
678 (with-slots (into attributes values query)
680 (write-string "INSERT INTO " *sql-stream*)
681 (output-sql into database)
683 (write-char #\Space *sql-stream*)
684 (output-sql attributes database))
686 (write-string " VALUES " *sql-stream*)
687 (output-sql values database))
689 (write-char #\Space *sql-stream*)
690 (output-sql query database)))
695 (defclass sql-delete (%sql-expression)
703 "An SQL DELETE statement."))
705 (defmethod output-sql ((stmt sql-delete) database)
706 (with-slots (from where)
708 (write-string "DELETE FROM " *sql-stream*)
710 (symbol (write-string (sql-escape from) *sql-stream*))
711 (t (output-sql from database)))
713 (write-string " WHERE " *sql-stream*)
714 (output-sql where database)))
719 (defclass sql-update (%sql-expression)
732 (:documentation "An SQL UPDATE statement."))
734 (defmethod output-sql ((expr sql-update) database)
735 (with-slots (table where attributes values)
737 (flet ((update-assignments ()
738 (mapcar #'(lambda (a b)
739 (make-instance 'sql-assignment-exp
741 :sub-expressions (list a b)))
743 (write-string "UPDATE " *sql-stream*)
744 (output-sql table database)
745 (write-string " SET " *sql-stream*)
746 (output-sql (apply #'vector (update-assignments)) database)
748 (write-string " WHERE " *sql-stream*)
749 (output-sql where database))))
754 (defclass sql-create-table (%sql-expression)
765 :initarg :transactions
768 "An SQL CREATE TABLE statement."))
770 ;; Here's a real warhorse of a function!
772 (declaim (inline listify))
778 (defmethod output-sql ((stmt sql-create-table) database)
779 (flet ((output-column (column-spec)
780 (destructuring-bind (name type &optional db-type &rest constraints)
782 (let ((type (listify type)))
783 (output-sql name database)
784 (write-char #\Space *sql-stream*)
786 (if (stringp db-type) db-type ; override definition
787 (database-get-type-specifier (car type) (cdr type) database))
790 (database-constraint-statement constraints database)))
792 (write-string " " *sql-stream*)
793 (write-string constraints *sql-stream*)))))))
794 (with-slots (name columns modifiers transactions)
796 (write-string "CREATE TABLE " *sql-stream*)
797 (output-sql name database)
798 (write-string " (" *sql-stream*)
799 (do ((column columns (cdr column)))
801 (output-column (car column)))
802 (output-column (car column))
803 (write-string ", " *sql-stream*))
805 (do ((modifier (listify modifiers) (cdr modifier)))
807 (write-string ", " *sql-stream*)
808 (write-string (car modifier) *sql-stream*)))
809 (write-char #\) *sql-stream*)
810 (when (and (eq :mysql (database-underlying-type database))
812 (db-type-transaction-capable? :mysql database))
813 (write-string " Type=InnoDB" *sql-stream*))))
819 (defclass sql-create-view (%sql-expression)
820 ((name :initarg :name :initform nil)
821 (column-list :initarg :column-list :initform nil)
822 (query :initarg :query :initform nil)
823 (with-check-option :initarg :with-check-option :initform nil))
824 (:documentation "An SQL CREATE VIEW statement."))
826 (defmethod output-sql ((stmt sql-create-view) database)
827 (with-slots (name column-list query with-check-option) stmt
828 (write-string "CREATE VIEW " *sql-stream*)
829 (output-sql name database)
830 (when column-list (write-string " " *sql-stream*)
831 (output-sql (listify column-list) database))
832 (write-string " AS " *sql-stream*)
833 (output-sql query database)
834 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
838 ;; Column constraint types
840 (defparameter *constraint-types*
842 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
843 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")))
846 ;; Convert type spec to sql syntax
849 (defmethod database-constraint-description (constraint database)
850 (declare (ignore database))
851 (let ((output (assoc (symbol-name constraint) *constraint-types*
854 (error 'clsql-sql-syntax-error
855 :reason (format nil "unsupported column constraint '~a'"
859 (defmethod database-constraint-statement (constraint-list database)
860 (declare (ignore database))
861 (make-constraints-description constraint-list))
863 (defun make-constraints-description (constraint-list)
866 (do ((constraint constraint-list (cdr constraint)))
867 ((null constraint) string)
868 (let ((output (assoc (symbol-name (car constraint))
872 (error 'clsql-sql-syntax-error
873 :reason (format nil "unsupported column constraint '~a'"
875 (setq string (concatenate 'string string (cdr output))))
876 (if (< 1 (length constraint))
877 (setq string (concatenate 'string string " "))))))))