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 (symbol-name name) database)) *sql-stream*)
151 (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
152 (if qualifier (sql-escape qualifier) qualifier)
153 (sql-escape (convert-to-db-default-case name database))
157 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
158 (declare (ignore database))
159 (with-slots (qualifier name type params)
161 (list 'sql-ident-attribute qualifier name type params)))
163 ;; For SQL Identifiers for tables
164 (defclass sql-ident-table (sql-ident)
166 :initarg :table-alias :initform nil))
167 (:documentation "An SQL table identifier."))
169 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
170 (declare (ignore environment))
171 (with-slots (alias name)
173 `(make-instance 'sql-ident-table :name name :alias ',alias)))
175 (defun generate-sql (expr database)
176 (let ((*sql-stream* (make-string-output-stream)))
177 (output-sql expr database)
178 (get-output-stream-string *sql-stream*)))
180 (defmethod output-sql ((expr sql-ident-table) database)
181 (with-slots (name alias)
184 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
186 (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
187 (write-char #\Space *sql-stream*)
188 (format *sql-stream* "~s" alias))))
192 (defmethod database-output-sql ((self duration) database)
193 (declare (ignore database))
194 (format nil "'~a'" (duration-timestring self)))
196 (defmethod database-output-sql ((self money) database)
197 (database-output-sql (slot-value self 'odcl::units) database))
201 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
202 (declare (ignore database))
203 (with-slots (name alias)
205 (list 'sql-ident-table name alias)))
207 (defclass sql-relational-exp (%sql-expression)
212 :initarg :sub-expressions
214 (:documentation "An SQL relational expression."))
216 (defmethod collect-table-refs ((sql sql-relational-exp))
218 (dolist (exp (slot-value sql 'sub-expressions))
219 (let ((refs (collect-table-refs exp)))
220 (if refs (setf tabs (append refs tabs)))))
221 (remove-duplicates tabs
222 :test (lambda (tab1 tab2)
223 (equal (slot-value tab1 'name)
224 (slot-value tab2 'name))))))
229 ;; Write SQL for relational operators (like 'AND' and 'OR').
230 ;; should do arity checking of subexpressions
232 (defmethod output-sql ((expr sql-relational-exp) database)
233 (with-slots (operator sub-expressions)
235 (let ((subs (if (consp (car sub-expressions))
236 (car sub-expressions)
238 (write-char #\( *sql-stream*)
239 (do ((sub subs (cdr sub)))
240 ((null (cdr sub)) (output-sql (car sub) database))
241 (output-sql (car sub) database)
242 (write-char #\Space *sql-stream*)
243 (output-sql operator database)
244 (write-char #\Space *sql-stream*))
245 (write-char #\) *sql-stream*)))
248 (defclass sql-upcase-like (sql-relational-exp)
250 (:documentation "An SQL 'like' that upcases its arguments."))
252 ;; Write SQL for relational operators (like 'AND' and 'OR').
253 ;; should do arity checking of subexpressions
255 (defmethod output-sql ((expr sql-upcase-like) database)
256 (flet ((write-term (term)
257 (write-string "upper(" *sql-stream*)
258 (output-sql term database)
259 (write-char #\) *sql-stream*)))
260 (with-slots (sub-expressions)
262 (let ((subs (if (consp (car sub-expressions))
263 (car sub-expressions)
265 (write-char #\( *sql-stream*)
266 (do ((sub subs (cdr sub)))
267 ((null (cdr sub)) (write-term (car sub)))
268 (write-term (car sub))
269 (write-string " LIKE " *sql-stream*))
270 (write-char #\) *sql-stream*))))
273 (defclass sql-assignment-exp (sql-relational-exp)
275 (:documentation "An SQL Assignment expression."))
278 (defmethod output-sql ((expr sql-assignment-exp) database)
279 (with-slots (operator sub-expressions)
281 (do ((sub sub-expressions (cdr sub)))
282 ((null (cdr sub)) (output-sql (car sub) database))
283 (output-sql (car sub) database)
284 (write-char #\Space *sql-stream*)
285 (output-sql operator database)
286 (write-char #\Space *sql-stream*)))
289 (defclass sql-value-exp (%sql-expression)
297 "An SQL value expression.")
300 (defmethod collect-table-refs ((sql sql-value-exp))
302 (if (listp (slot-value sql 'components))
304 (dolist (exp (slot-value sql 'components))
305 (let ((refs (collect-table-refs exp)))
306 (if refs (setf tabs (append refs tabs)))))
307 (remove-duplicates tabs
308 :test (lambda (tab1 tab2)
309 (equal (slot-value tab1 'name)
310 (slot-value tab2 'name)))))
315 (defmethod output-sql ((expr sql-value-exp) database)
316 (with-slots (modifier components)
320 (write-char #\( *sql-stream*)
321 (output-sql modifier database)
322 (write-char #\Space *sql-stream*)
323 (output-sql components database)
324 (write-char #\) *sql-stream*))
325 (output-sql components database))))
327 (defclass sql-typecast-exp (sql-value-exp)
329 (:documentation "An SQL typecast expression."))
331 (defmethod output-sql ((expr sql-typecast-exp) database)
332 (database-output-sql expr database))
334 (defmethod database-output-sql ((expr sql-typecast-exp) database)
335 (with-slots (components)
337 (output-sql components database)))
340 (defmethod collect-table-refs ((sql sql-typecast-exp))
341 (when (slot-value sql 'components)
342 (collect-table-refs (slot-value sql 'components))))
344 (defclass sql-function-exp (%sql-expression)
352 "An SQL function expression."))
354 (defmethod collect-table-refs ((sql sql-function-exp))
356 (dolist (exp (slot-value sql 'components))
357 (let ((refs (collect-table-refs exp)))
358 (if refs (setf tabs (append refs tabs)))))
359 (remove-duplicates tabs
360 :test (lambda (tab1 tab2)
361 (equal (slot-value tab1 'name)
362 (slot-value tab2 'name))))))
364 (defmethod output-sql ((expr sql-function-exp) database)
365 (with-slots (name args)
367 (output-sql name database)
368 (when args (output-sql args database)))
371 (defclass sql-query (%sql-expression)
382 :initarg :set-operation
409 :initarg :order-by-descending
411 (:documentation "An SQL SELECT query."))
413 (defmethod collect-table-refs ((sql sql-query))
414 (remove-duplicates (collect-table-refs (slot-value sql 'where))
415 :test (lambda (tab1 tab2)
416 (equal (slot-value tab1 'name)
417 (slot-value tab2 'name)))))
419 (defvar *select-arguments*
420 '(:all :database :distinct :flatp :from :group-by :having :order-by
421 :order-by-descending :set-operation :where :offset :limit))
423 (defun query-arg-p (sym)
424 (member sym *select-arguments*))
426 (defun query-get-selections (select-args)
427 "Return two values: the list of select-args up to the first keyword,
428 uninclusive, and the args from that keyword to the end."
429 (let ((first-key-arg (position-if #'query-arg-p select-args)))
431 (values (subseq select-args 0 first-key-arg)
432 (subseq select-args first-key-arg))
435 (defmethod make-query (&rest args)
436 (multiple-value-bind (selections arglist)
437 (query-get-selections args)
438 (destructuring-bind (&key all flatp set-operation distinct from where
439 group-by having order-by order-by-descending
440 offset limit &allow-other-keys)
442 (if (null selections)
443 (error "No target columns supplied to select statement."))
445 (error "No source tables supplied to select statement."))
446 (make-instance 'sql-query :selections selections
447 :all all :flatp flatp :set-operation set-operation
448 :distinct distinct :from from :where where
449 :limit limit :offset offset
450 :group-by group-by :having having :order-by order-by
451 :order-by-descending order-by-descending))))
453 (defvar *in-subselect* nil)
455 (defmethod output-sql ((query sql-query) database)
456 (with-slots (distinct selections from where group-by having order-by
457 order-by-descending limit offset)
460 (write-string "(" *sql-stream*))
461 (write-string "SELECT " *sql-stream*)
463 (write-string "DISTINCT " *sql-stream*)
464 (unless (eql t distinct)
465 (write-string "ON " *sql-stream*)
466 (output-sql distinct database)
467 (write-char #\Space *sql-stream*)))
468 (output-sql (apply #'vector selections) database)
469 (write-string " FROM " *sql-stream*)
471 (output-sql (apply #'vector from) database)
472 (output-sql from database))
474 (write-string " WHERE " *sql-stream*)
475 (let ((*in-subselect* t))
476 (output-sql where database)))
478 (write-string " GROUP BY " *sql-stream*)
479 (output-sql group-by database))
481 (write-string " HAVING " *sql-stream*)
482 (output-sql having database))
484 (write-string " ORDER BY " *sql-stream*)
486 (do ((order order-by (cdr order)))
488 (output-sql (car order) database)
490 (write-char #\, *sql-stream*)))
491 (output-sql order-by database)))
492 (when order-by-descending
493 (write-string " ORDER BY " *sql-stream*)
494 (if (listp order-by-descending)
495 (do ((order order-by-descending (cdr order)))
497 (output-sql (car order) database)
499 (write-char #\, *sql-stream*)))
500 (output-sql order-by-descending database))
501 (write-string " DESC " *sql-stream*))
503 (write-string " LIMIT " *sql-stream*)
504 (output-sql limit database))
506 (write-string " OFFSET " *sql-stream*)
507 (output-sql offset database))
509 (write-string ")" *sql-stream*)))
514 (defclass sql-insert (%sql-expression)
528 "An SQL INSERT statement."))
530 (defmethod output-sql ((ins sql-insert) database)
531 (with-slots (into attributes values query)
533 (write-string "INSERT INTO " *sql-stream*)
534 (output-sql into database)
536 (write-char #\Space *sql-stream*)
537 (output-sql attributes database))
539 (write-string " VALUES " *sql-stream*)
540 (output-sql values database))
542 (write-char #\Space *sql-stream*)
543 (output-sql query database)))
548 (defclass sql-delete (%sql-expression)
556 "An SQL DELETE statement."))
558 (defmethod output-sql ((stmt sql-delete) database)
559 (with-slots (from where)
561 (write-string "DELETE FROM " *sql-stream*)
563 (symbol (write-string (sql-escape from) *sql-stream*))
564 (t (output-sql from database)))
566 (write-string " WHERE " *sql-stream*)
567 (output-sql where database)))
572 (defclass sql-update (%sql-expression)
585 (:documentation "An SQL UPDATE statement."))
587 (defmethod output-sql ((expr sql-update) database)
588 (with-slots (table where attributes values)
590 (flet ((update-assignments ()
591 (mapcar #'(lambda (a b)
592 (make-instance 'sql-assignment-exp
594 :sub-expressions (list a b)))
596 (write-string "UPDATE " *sql-stream*)
597 (output-sql table database)
598 (write-string " SET " *sql-stream*)
599 (output-sql (apply #'vector (update-assignments)) database)
601 (write-string " WHERE " *sql-stream*)
602 (output-sql where database))))
607 (defclass sql-create-table (%sql-expression)
618 :initarg :transactions
621 "An SQL CREATE TABLE statement."))
623 ;; Here's a real warhorse of a function!
630 (defmethod output-sql ((stmt sql-create-table) database)
631 (flet ((output-column (column-spec)
632 (destructuring-bind (name type &optional db-type &rest constraints)
634 (let ((type (listify type)))
635 (output-sql name database)
636 (write-char #\Space *sql-stream*)
638 (if (stringp db-type) db-type ; override definition
639 (database-get-type-specifier (car type) (cdr type) database))
642 (database-constraint-statement constraints database)))
644 (write-string " " *sql-stream*)
645 (write-string constraints *sql-stream*)))))))
646 (with-slots (name columns modifiers transactions)
648 (write-string "CREATE TABLE " *sql-stream*)
649 (output-sql name database)
650 (write-string " (" *sql-stream*)
651 (do ((column columns (cdr column)))
653 (output-column (car column)))
654 (output-column (car column))
655 (write-string ", " *sql-stream*))
657 (do ((modifier (listify modifiers) (cdr modifier)))
659 (write-string ", " *sql-stream*)
660 (write-string (car modifier) *sql-stream*)))
661 (write-char #\) *sql-stream*)
662 (when (and (eq :mysql (database-underlying-type database))
664 (db-type-transaction-capable? :mysql database))
665 (write-string " Type=InnoDB" *sql-stream*))))
671 (defclass sql-create-view (%sql-expression)
672 ((name :initarg :name :initform nil)
673 (column-list :initarg :column-list :initform nil)
674 (query :initarg :query :initform nil)
675 (with-check-option :initarg :with-check-option :initform nil))
676 (:documentation "An SQL CREATE VIEW statement."))
678 (defmethod output-sql ((stmt sql-create-view) database)
679 (with-slots (name column-list query with-check-option) stmt
680 (write-string "CREATE VIEW " *sql-stream*)
681 (output-sql name database)
682 (when column-list (write-string " " *sql-stream*)
683 (output-sql (listify column-list) database))
684 (write-string " AS " *sql-stream*)
685 (output-sql query database)
686 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
690 ;; Column constraint types
692 (defparameter *constraint-types*
693 '(("NOT-NULL" . "NOT NULL")
694 ("PRIMARY-KEY" . "PRIMARY KEY")))
697 ;; Convert type spec to sql syntax
700 (defmethod database-constraint-description (constraint database)
701 (declare (ignore database))
702 (let ((output (assoc (symbol-name constraint) *constraint-types*
705 (error 'clsql-sql-syntax-error
706 :reason (format nil "unsupported column constraint '~a'"
710 (defmethod database-constraint-statement (constraint-list database)
711 (declare (ignore database))
712 (make-constraints-description constraint-list))
714 (defun make-constraints-description (constraint-list)
717 (do ((constraint constraint-list (cdr constraint)))
718 ((null constraint) string)
719 (let ((output (assoc (symbol-name (car constraint))
723 (error 'clsql-sql-syntax-error
724 :reason (format nil "unsupported column constraint '~a'"
726 (setq string (concatenate 'string string (cdr output))))
727 (if (< 1 (length constraint))
728 (setq string (concatenate 'string string " "))))))))