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 (defclass %database-identifier ()
24 ((escaped :accessor escaped :initarg :escaped :initform nil)
25 (unescaped :accessor unescaped :initarg :unescaped :initform nil))
27 "A database identifier represents a string/symbol ready to be spliced
28 into a sql string. It keeps references to both the escaped and
29 unescaped versions so that unescaped versions can be compared to the
30 results of list-tables/views/attributes etc. It also allows you to be
31 sure that an identifier is escaped only once.
33 (escaped-database-identifiers *any-reasonable-object*) should be called to
34 produce a string that is safe to splice directly into sql strings.
36 (unescaped-database-identifier *any-reasonable-object*) is generally what
37 you pass to it with the exception that symbols have been
38 clsql-sys:sql-escape which converts to a string and changes - to _ (so
39 that unescaped can be compared to the results of eg: list-tables)
42 (defmethod escaped ((it null)) it)
43 (defmethod unescaped ((it null)) it)
45 (defun database-identifier-equal (i1 i2 &optional (database clsql-sys:*default-database*))
46 (setf i1 (database-identifier i1 database)
47 i2 (database-identifier i2 database))
49 (if (symbolp (unescaped i))
50 (sql-escape (unescaped i))
52 (or ;; check for an exact match
53 (equal (escaped-database-identifier i1)
54 (escaped-database-identifier i2))
55 ;; check for an inexact match if we had symbols in the mix
56 (string-equal (cast i1) (cast i2)))))
58 (defun delistify-dsd (list)
59 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
60 (if (and (listp list) (null (cdr list)))
64 (defun special-char-p (s)
65 "Check if a string has any special characters"
66 (loop for char across s
67 thereis (find char '(#\space #\, #\. #\! #\@ #\# #\$ #\% #\' #\"
68 #\^ #\& #\* #\| #\( #\) #\- #\+ #\< #\>
71 (defun special-cased-symbol-p (sym)
72 "Should the symbols case be preserved, or should we convert to default casing"
73 (let ((name (symbol-name sym)))
74 (case (readtable-case *readtable*)
75 (:upcase (not (string= (string-upcase name) name)))
76 (:downcase (not (string= (string-downcase name) name)))
79 (defun %make-database-identifier (inp &optional database)
80 "We want to quote an identifier if it came to us as a string or if it has special characters
82 (labels ((%escape-identifier (inp &optional orig)
83 "Quote an identifier unless it is already quoted"
86 ((and (eql #\" (elt inp 0))
87 (eql #\" (elt inp (- (length inp) 1))))
88 (make-instance '%database-identifier :unescaped (or orig inp) :escaped inp))
90 '%database-identifier :unescaped (or orig inp) :escaped
92 'string "\"" (replace-all inp "\"" "\\\"") "\""))))))
94 (string (%escape-identifier inp))
95 (%database-identifier inp)
97 (let ((s (sql-escape inp)))
98 (if (and (not (eql '* inp)) (special-char-p s))
100 (if (special-cased-symbol-p inp)
102 (convert-to-db-default-case s database)) inp)
103 (make-instance '%database-identifier :escaped s :unescaped inp))
106 (defun combine-database-identifiers (ids &optional (database clsql-sys:*default-database*)
107 &aux res all-sym? pkg)
108 "Create a new database identifier by combining parts in a reasonable way
110 (setf ids (mapcar #'database-identifier ids)
111 all-sym? (every (lambda (i) (symbolp (unescaped i))) ids)
112 pkg (when all-sym? (symbol-package (unescaped (first ids)))))
116 (%database-identifier (cast (unescaped i)))
120 (convert-to-db-default-case (sql-escape i) database)))
126 (concatenate 'string (cast i1) "_" (cast i2))
128 (setf res (reduce #'comb ids))
130 (if all-sym? (intern res pkg) res)
133 (defun escaped-database-identifier (name &optional database find-class-p)
134 (escaped (database-identifier name database find-class-p)))
136 (defun unescaped-database-identifier (name &optional database find-class-p)
137 (unescaped (database-identifier name database find-class-p)))
139 (defun sql-output (sql-expr &optional (database *default-database*))
140 "Top-level call for generating SQL strings. Returns an SQL
141 string appropriate for DATABASE which corresponds to the
142 supplied lisp expression SQL-EXPR."
143 (with-output-to-string (*sql-stream*)
144 (output-sql sql-expr database)))
146 (defmethod output-sql (expr database)
147 (write-string (database-output-sql expr database) *sql-stream*)
151 (defvar *output-hash*
152 (make-weak-hash-table :test #'equal)
153 "For caching generated SQL strings, set to NIL to disable."
156 (defmethod output-sql :around ((sql t) database)
157 (if (null *output-hash*)
159 (let* ((hash-key (output-sql-hash-key sql database))
160 (hash-value (when hash-key (gethash hash-key *output-hash*))))
161 (cond ((and hash-key hash-value)
162 (write-string hash-value *sql-stream*))
164 (let ((*sql-stream* (make-string-output-stream)))
166 (setf hash-value (get-output-stream-string *sql-stream*))
167 (setf (gethash hash-key *output-hash*) hash-value))
168 (write-string hash-value *sql-stream*))
170 (call-next-method))))))
172 (defmethod output-sql-hash-key (expr database)
173 (declare (ignore expr database))
177 (defclass %sql-expression ()
180 (defmethod output-sql ((expr %sql-expression) database)
181 (declare (ignore database))
182 (write-string +null-string+ *sql-stream*))
184 (defmethod print-object ((self %sql-expression) stream)
185 (print-unreadable-object
186 (self stream :type t)
187 (write-string (sql-output self) stream))
190 ;; For straight up strings
192 (defclass sql (%sql-expression)
196 (:documentation "A literal SQL expression."))
198 (defmethod make-load-form ((sql sql) &optional environment)
199 (declare (ignore environment))
202 `(make-instance 'sql :string ',text)))
204 (defmethod output-sql ((expr sql) database)
205 (declare (ignore database))
206 (write-string (slot-value expr 'text) *sql-stream*)
209 (defmethod print-object ((ident sql) stream)
210 (format stream "#<~S \"~A\">"
212 (sql-output ident nil))
215 ;; For SQL Identifiers of generic type
217 (defclass sql-ident (%sql-expression)
220 :initform +null-string+))
221 (:documentation "An SQL identifer."))
223 (defmethod make-load-form ((sql sql-ident) &optional environment)
224 (declare (ignore environment))
227 `(make-instance 'sql-ident :name ',name)))
229 (defmethod output-sql ((expr %database-identifier) database)
230 (write-string (escaped expr) *sql-stream*))
232 (defmethod output-sql ((expr sql-ident) database)
233 (with-slots (name) expr
234 (write-string (escaped-database-identifier name database) *sql-stream*))
237 ;; For SQL Identifiers for attributes
239 (defclass sql-ident-attribute (sql-ident)
242 :initform +null-string+)
245 :initform +null-string+))
246 (:documentation "An SQL Attribute identifier."))
248 (defmethod collect-table-refs (sql)
249 (declare (ignore sql))
252 (defmethod collect-table-refs ((sql list))
254 appending (listify (collect-table-refs i))))
256 (defmethod collect-table-refs ((sql sql-ident-attribute))
257 (let ((qual (slot-value sql 'qualifier)))
259 ;; going to be used as a table, search classes
262 :name (database-identifier qual nil t))))))
264 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
265 (declare (ignore environment))
266 (with-slots (qualifier type name)
268 `(make-instance 'sql-ident-attribute :name ',name
269 :qualifier ',qualifier
272 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
273 (with-slots (qualifier name type)
275 (list (and database (database-underlying-type database))
277 (unescaped-database-identifier qualifier)
278 (unescaped-database-identifier name) type)))
280 ;; For SQL Identifiers for tables
282 (defclass sql-ident-table (sql-ident)
284 :initarg :table-alias :initform nil))
285 (:documentation "An SQL table identifier."))
287 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
288 (declare (ignore environment))
289 (with-slots (alias name)
291 `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
293 (defmethod collect-table-refs ((sql sql-ident-table))
296 (defmethod output-sql ((expr sql-ident-table) database)
297 (with-slots (name alias) expr
298 (flet ((p (s) ;; the etypecase is in sql-escape too
300 (escaped-database-identifier s database)
304 (princ #\space *sql-stream*)
308 (defmethod output-sql ((expr sql-ident-attribute) database)
309 ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
310 ;;; should not be output in SQL statements
311 (let ((*print-pretty* nil))
312 (with-slots (qualifier name type) expr
313 (format *sql-stream* "~@[~a.~]~a"
316 (escaped-database-identifier qualifier database T))
317 (escaped-database-identifier name database))
320 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
321 (with-slots (name alias)
323 (list (and database (database-underlying-type database))
325 (unescaped-database-identifier name)
326 (unescaped-database-identifier alias))))
328 (defclass sql-relational-exp (%sql-expression)
333 :initarg :sub-expressions
335 (:documentation "An SQL relational expression."))
337 (defmethod make-load-form ((self sql-relational-exp) &optional environment)
338 (make-load-form-saving-slots self
339 :slot-names '(operator sub-expressions)
340 :environment environment))
342 (defmethod collect-table-refs ((sql sql-relational-exp))
344 (dolist (exp (slot-value sql 'sub-expressions))
345 (let ((refs (collect-table-refs exp)))
346 (if refs (setf tabs (append refs tabs)))))
347 (remove-duplicates tabs :test #'database-identifier-equal)))
352 ;; Write SQL for relational operators (like 'AND' and 'OR').
353 ;; should do arity checking of subexpressions
355 (defun %write-operator (operator database)
357 (string (write-string operator *sql-stream*))
358 (symbol (write-string (symbol-name operator) *sql-stream*))
359 (T (output-sql operator database))))
361 (defmethod output-sql ((expr sql-relational-exp) database)
362 (with-slots (operator sub-expressions) expr
363 ;; we do this as two runs so as not to emit confusing superflous parentheses
364 ;; The first loop renders all the child outputs so that we can skip anding with
365 ;; empty output (which causes sql errors)
366 ;; the next loop simply emits each sub-expression with the appropriate number of
367 ;; parens and operators
369 (string-trim +whitespace-chars+
370 (with-output-to-string (*sql-stream*)
371 (output-sql sub database)))))
372 (let ((str-subs (loop for sub in sub-expressions
373 for str-sub = (trim sub)
374 when (and str-sub (> (length str-sub) 0))
376 (case (length str-subs)
378 (1 (write-string (first str-subs) *sql-stream*))
380 (write-char #\( *sql-stream*)
381 (write-string (first str-subs) *sql-stream*)
382 (loop for str-sub in (rest str-subs)
384 (write-char #\Space *sql-stream*)
385 ;; do this so that symbols can be output as database identifiers
386 ;; rather than allowing symbols to inject sql
387 (%write-operator operator database)
388 (write-char #\Space *sql-stream*)
389 (write-string str-sub *sql-stream*))
390 (write-char #\) *sql-stream*))
394 (defclass sql-upcase-like (sql-relational-exp)
396 (:documentation "An SQL 'like' that upcases its arguments."))
398 (defmethod output-sql ((expr sql-upcase-like) database)
399 (flet ((write-term (term)
400 (write-string "upper(" *sql-stream*)
401 (output-sql term database)
402 (write-char #\) *sql-stream*)))
403 (with-slots (sub-expressions)
405 (let ((subs (if (consp (car sub-expressions))
406 (car sub-expressions)
408 (write-char #\( *sql-stream*)
409 (do ((sub subs (cdr sub)))
410 ((null (cdr sub)) (write-term (car sub)))
411 (write-term (car sub))
412 (write-string " LIKE " *sql-stream*))
413 (write-char #\) *sql-stream*))))
416 (defclass sql-assignment-exp (sql-relational-exp)
418 (:documentation "An SQL Assignment expression."))
421 (defmethod output-sql ((expr sql-assignment-exp) database)
422 (with-slots (operator sub-expressions)
424 (do ((sub sub-expressions (cdr sub)))
425 ((null (cdr sub)) (output-sql (car sub) database))
426 (output-sql (car sub) database)
427 (write-char #\Space *sql-stream*)
428 (%write-operator operator database)
429 (write-char #\Space *sql-stream*)))
432 (defclass sql-value-exp (%sql-expression)
440 "An SQL value expression.")
443 (defmethod collect-table-refs ((sql sql-value-exp))
445 (if (listp (slot-value sql 'components))
447 (dolist (exp (slot-value sql 'components))
448 (let ((refs (collect-table-refs exp)))
449 (if refs (setf tabs (append refs tabs)))))
450 (remove-duplicates tabs :test #'database-identifier-equal))
455 (defmethod output-sql ((expr sql-value-exp) database)
456 (with-slots (modifier components)
460 (write-char #\( *sql-stream*)
462 ((sql-operator modifier)
463 (%write-operator modifier database))
464 ((or (stringp modifier) (symbolp modifier))
466 (escaped-database-identifier modifier)
468 (t (output-sql modifier database)))
469 (write-char #\Space *sql-stream*)
470 (output-sql components database)
471 (write-char #\) *sql-stream*))
472 (output-sql components database))))
474 (defclass sql-typecast-exp (sql-value-exp)
476 (:documentation "An SQL typecast expression."))
478 (defmethod output-sql ((expr sql-typecast-exp) database)
479 (with-slots (components)
481 (output-sql components database)))
483 (defmethod collect-table-refs ((sql sql-typecast-exp))
484 (when (slot-value sql 'components)
485 (collect-table-refs (slot-value sql 'components))))
487 (defclass sql-function-exp (%sql-expression)
495 "An SQL function expression."))
497 (defmethod collect-table-refs ((sql sql-function-exp))
499 (dolist (exp (slot-value sql 'args))
500 (let ((refs (collect-table-refs exp)))
501 (if refs (setf tabs (append refs tabs)))))
502 (remove-duplicates tabs :test #'database-identifier-equal)))
503 (defvar *in-subselect* nil)
505 (defmethod output-sql ((expr sql-function-exp) database)
506 (with-slots (name args)
510 (write-string (escaped-database-identifier name) *sql-stream*))
511 (t (output-sql name database)))
512 (let ((*in-subselect* nil)) ;; aboid double parens
513 (when args (output-sql args database))))
517 (defclass sql-between-exp (sql-function-exp)
519 (:documentation "An SQL between expression."))
521 (defmethod output-sql ((expr sql-between-exp) database)
524 (output-sql (first args) database)
525 (write-string " BETWEEN " *sql-stream*)
526 (output-sql (second args) database)
527 (write-string " AND " *sql-stream*)
528 (output-sql (third args) database))
531 (defclass sql-query-modifier-exp (%sql-expression)
532 ((modifier :initarg :modifier :initform nil)
533 (components :initarg :components :initform nil))
534 (:documentation "An SQL query modifier expression."))
536 (defmethod output-sql ((expr sql-query-modifier-exp) database)
537 (with-slots (modifier components)
539 (%write-operator modifier database)
540 (write-string " " *sql-stream*)
541 (%write-operator (car components) database)
543 (mapc #'(lambda (comp)
544 (write-string ", " *sql-stream*)
545 (output-sql comp database))
549 (defclass sql-set-exp (%sql-expression)
554 :initarg :sub-expressions
556 (:documentation "An SQL set expression."))
558 (defmethod collect-table-refs ((sql sql-set-exp))
560 (dolist (exp (slot-value sql 'sub-expressions))
561 (let ((refs (collect-table-refs exp)))
562 (if refs (setf tabs (append refs tabs)))))
563 (remove-duplicates tabs :test #'database-identifier-equal)))
565 (defmethod output-sql ((expr sql-set-exp) database)
566 (with-slots (operator sub-expressions)
568 (let ((subs (if (consp (car sub-expressions))
569 (car sub-expressions)
571 (when (= (length subs) 1)
572 (%write-operator operator database)
573 (write-char #\Space *sql-stream*))
574 (do ((sub subs (cdr sub)))
575 ((null (cdr sub)) (output-sql (car sub) database))
576 (output-sql (car sub) database)
577 (write-char #\Space *sql-stream*)
578 (%write-operator operator database)
579 (write-char #\Space *sql-stream*))))
582 (defclass sql-query (%sql-expression)
593 :initarg :set-operation
625 (:documentation "An SQL SELECT query."))
627 (defclass sql-object-query (%sql-expression)
641 (defmethod collect-table-refs ((sql sql-query))
643 (collect-table-refs (slot-value sql 'where))
644 :test #'database-identifier-equal))
646 (defvar *select-arguments*
647 '(:all :database :distinct :flatp :from :group-by :having :order-by
648 :set-operation :where :offset :limit :inner-join :on
649 ;; below keywords are not a SQL argument, but these keywords may terminate select
652 (defun query-arg-p (sym)
653 (member sym *select-arguments*))
655 (defun query-get-selections (select-args)
656 "Return two values: the list of select-args up to the first keyword,
657 uninclusive, and the args from that keyword to the end."
658 (let ((first-key-arg (position-if #'query-arg-p select-args)))
660 (values (subseq select-args 0 first-key-arg)
661 (subseq select-args first-key-arg))
664 (defun make-query (&rest args)
665 (flet ((select-objects (target-args)
667 (every #'(lambda (arg)
669 (find-class arg nil)))
671 (multiple-value-bind (selections arglist)
672 (query-get-selections args)
673 (if (select-objects selections)
674 (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
675 (make-instance 'sql-object-query :objects selections
676 :flatp flatp :refresh refresh
678 (destructuring-bind (&key all flatp set-operation distinct from where
679 group-by having order-by
680 offset limit inner-join on &allow-other-keys)
682 (if (null selections)
683 (error "No target columns supplied to select statement."))
685 (error "No source tables supplied to select statement."))
686 (make-instance 'sql-query :selections selections
687 :all all :flatp flatp :set-operation set-operation
688 :distinct distinct :from from :where where
689 :limit limit :offset offset
690 :group-by group-by :having having :order-by order-by
691 :inner-join inner-join :on on))))))
693 (defun output-sql-where-clause (where database)
694 "ensure that we do not output a \"where\" sql keyword when we will
695 not output a clause. Also sets *in-subselect* to use SQL
696 parentheticals as needed."
698 (let ((where-out (string-trim
699 '(#\newline #\space #\tab #\return)
700 (with-output-to-string (*sql-stream*)
701 (let ((*in-subselect* t))
702 (output-sql where database))))))
703 (when (> (length where-out) 0)
704 (write-string " WHERE " *sql-stream*)
705 (write-string where-out *sql-stream*)))))
707 (defmethod output-sql ((query sql-query) database)
708 (with-slots (distinct selections from where group-by having order-by
709 limit offset inner-join on all set-operation)
712 (write-string "(" *sql-stream*))
713 (write-string "SELECT " *sql-stream*)
715 (write-string " ALL " *sql-stream*))
716 (when (and distinct (not all))
717 (write-string " DISTINCT " *sql-stream*)
718 (unless (eql t distinct)
719 (write-string " ON " *sql-stream*)
720 (output-sql distinct database)
721 (write-char #\Space *sql-stream*)))
722 (when (and limit (eql :mssql (database-underlying-type database)))
723 (write-string " TOP " *sql-stream*)
724 (output-sql limit database)
725 (write-string " " *sql-stream*))
726 (let ((*in-subselect* t))
727 (output-sql (apply #'vector selections) database))
729 (write-string " FROM " *sql-stream*)
733 (remove-duplicates from :test #'database-identifier-equal))
735 (string (write-string
736 (escaped-database-identifier from database)
738 (t (let ((*in-subselect* t))
739 (output-sql from database)))))
741 (write-string " INNER JOIN " *sql-stream*)
742 (output-sql inner-join database))
744 (write-string " ON " *sql-stream*)
745 (output-sql on database))
746 (output-sql-where-clause where database)
748 (write-string " GROUP BY " *sql-stream*)
750 (do ((order group-by (cdr order)))
752 (let ((item (car order)))
755 (output-sql (car item) database)
756 (format *sql-stream* " ~A" (cadr item)))
758 (output-sql item database)))
760 (write-char #\, *sql-stream*))))
761 (output-sql group-by database)))
763 (write-string " HAVING " *sql-stream*)
764 (output-sql having database))
766 (write-string " ORDER BY " *sql-stream*)
768 (do ((order order-by (cdr order)))
770 (let ((item (car order)))
773 (output-sql (car item) database)
774 (format *sql-stream* " ~A" (cadr item)))
776 (output-sql item database)))
778 (write-char #\, *sql-stream*))))
779 (output-sql order-by database)))
780 (when (and limit (not (eql :mssql (database-underlying-type database))))
781 (write-string " LIMIT " *sql-stream*)
782 (output-sql limit database))
784 (write-string " OFFSET " *sql-stream*)
785 (output-sql offset database))
787 (write-string ")" *sql-stream*))
789 (write-char #\Space *sql-stream*)
790 (output-sql set-operation database)))
793 (defmethod output-sql ((query sql-object-query) database)
794 (declare (ignore database))
795 (with-slots (objects)
798 (format *sql-stream* "(~{~A~^ ~})" objects))))
803 (defclass sql-insert (%sql-expression)
817 "An SQL INSERT statement."))
819 (defmethod output-sql ((ins sql-insert) database)
820 (with-slots (into attributes values query)
822 (write-string "INSERT INTO " *sql-stream*)
825 (string (sql-expression :table into))
829 (write-char #\Space *sql-stream*)
830 (output-sql attributes database))
832 (write-string " VALUES " *sql-stream*)
833 (let ((clsql-sys::*in-subselect* t))
834 (output-sql values database)))
836 (write-char #\Space *sql-stream*)
837 (output-sql query database)))
842 (defclass sql-delete (%sql-expression)
850 "An SQL DELETE statement."))
852 (defmethod output-sql ((stmt sql-delete) database)
853 (with-slots (from where)
855 (write-string "DELETE FROM " *sql-stream*)
857 ((or symbol string) (write-string (sql-escape from) *sql-stream*))
858 (t (output-sql from database)))
859 (output-sql-where-clause where database))
864 (defclass sql-update (%sql-expression)
877 (:documentation "An SQL UPDATE statement."))
879 (defmethod output-sql ((expr sql-update) database)
880 (with-slots (table where attributes values)
882 (flet ((update-assignments ()
883 (mapcar #'(lambda (a b)
884 (make-instance 'sql-assignment-exp
886 :sub-expressions (list a b)))
888 (write-string "UPDATE " *sql-stream*)
889 (output-sql table database)
890 (write-string " SET " *sql-stream*)
891 (let ((clsql-sys::*in-subselect* t))
892 (output-sql (apply #'vector (update-assignments)) database))
893 (output-sql-where-clause where database)))
898 (defclass sql-create-table (%sql-expression)
909 :initarg :transactions
912 "An SQL CREATE TABLE statement."))
914 ;; Here's a real warhorse of a function!
916 (declaim (inline listify))
922 (defmethod output-sql ((stmt sql-create-table) database)
923 (flet ((output-column (column-spec)
924 (destructuring-bind (name type &optional db-type &rest constraints)
926 (let ((type (listify type)))
927 (output-sql name database)
928 (write-char #\Space *sql-stream*)
930 (if (stringp db-type) db-type ; override definition
931 (database-get-type-specifier (car type) (cdr type) database
932 (database-underlying-type database)))
934 (let ((constraints (database-constraint-statement
935 (if (and db-type (symbolp db-type))
936 (cons db-type constraints)
940 (write-string " " *sql-stream*)
941 (write-string constraints *sql-stream*)))))))
942 (with-slots (name columns modifiers transactions)
944 (write-string "CREATE TABLE " *sql-stream*)
945 (write-string (escaped-database-identifier name database) *sql-stream*)
946 (write-string " (" *sql-stream*)
947 (do ((column columns (cdr column)))
949 (output-column (car column)))
950 (output-column (car column))
951 (write-string ", " *sql-stream*))
953 (do ((modifier (listify modifiers) (cdr modifier)))
955 (write-string ", " *sql-stream*)
956 (write-string (car modifier) *sql-stream*)))
957 (write-char #\) *sql-stream*)
958 (when (and (eq :mysql (database-underlying-type database))
960 (db-type-transaction-capable? :mysql database))
961 (write-string " ENGINE=innodb" *sql-stream*))))
967 (defclass sql-create-view (%sql-expression)
968 ((name :initarg :name :initform nil)
969 (column-list :initarg :column-list :initform nil)
970 (query :initarg :query :initform nil)
971 (with-check-option :initarg :with-check-option :initform nil))
972 (:documentation "An SQL CREATE VIEW statement."))
974 (defmethod output-sql ((stmt sql-create-view) database)
975 (with-slots (name column-list query with-check-option) stmt
976 (write-string "CREATE VIEW " *sql-stream*)
977 (output-sql name database)
978 (when column-list (write-string " " *sql-stream*)
979 (output-sql (listify column-list) database))
980 (write-string " AS " *sql-stream*)
981 (output-sql query database)
982 (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
986 ;; DATABASE-OUTPUT-SQL
989 (defmethod database-output-sql ((str string) database)
990 (declare (optimize (speed 3) (safety 1)
991 #+cmu (extensions:inhibit-warnings 3)))
992 (let ((len (length str)))
993 (declare (type fixnum len))
996 ((and (null (position #\' str))
997 (null (position #\\ str)))
998 (concatenate 'string "'" str "'"))
1000 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
1001 (declare (simple-string buf))
1002 (do* ((i 0 (incf i))
1004 ((= i len) (subseq buf 0 (1+ j)))
1005 (declare (type fixnum i j))
1006 (let ((char (aref str i)))
1007 (declare (character char))
1008 (cond ((char= char #\')
1009 (setf (aref buf j) #\')
1011 (setf (aref buf j) #\'))
1012 ((and (char= char #\\)
1013 ;; MTP: only escape backslash with pgsql/mysql
1014 (member (database-underlying-type database)
1015 '(:postgresql :mysql)
1017 (setf (aref buf j) #\\)
1019 (setf (aref buf j) #\\))
1021 (setf (aref buf j) char))))))))))
1023 (let ((keyword-package (symbol-package :foo)))
1024 (defmethod database-output-sql ((sym symbol) database)
1027 (if (equal (symbol-package sym) keyword-package)
1028 (database-output-sql (symbol-name sym) database)
1029 (escaped-database-identifier sym)))))
1031 (defmethod database-output-sql ((tee (eql t)) database)
1033 (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
1036 (string (format nil "'~A'" val))
1037 (integer (format nil "~A" val)))))
1040 #+nil(defmethod database-output-sql ((tee (eql t)) database)
1041 (declare (ignore database))
1044 (defmethod database-output-sql ((num number) database)
1045 (declare (ignore database))
1046 (number-to-sql-string num))
1048 (defmethod database-output-sql ((arg list) database)
1051 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
1052 (sql-output val database))
1055 (defmethod database-output-sql ((arg vector) database)
1056 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
1057 (sql-output val database))
1060 (defmethod output-sql-hash-key ((arg vector) database)
1061 (list 'vector (map 'list (lambda (arg)
1062 (or (output-sql-hash-key arg database)
1063 (return-from output-sql-hash-key nil)))
1066 (defmethod database-output-sql ((self wall-time) database)
1067 (declare (ignore database))
1068 (db-timestring self))
1070 (defmethod database-output-sql ((self date) database)
1071 (declare (ignore database))
1072 (db-datestring self))
1074 (defmethod database-output-sql ((self duration) database)
1075 (declare (ignore database))
1076 (format nil "'~a'" (duration-timestring self)))
1079 (defmethod database-output-sql ((self money) database)
1080 (database-output-sql (slot-value self 'odcl::units) database))
1082 (defmethod database-output-sql (thing database)
1083 (if (or (null thing)
1086 (error 'sql-user-error
1089 "No type conversion to SQL for ~A is defined for DB ~A."
1090 (type-of thing) (type-of database)))))
1094 ;; Column constraint types and conversion to SQL
1097 (defparameter *constraint-types*
1099 (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
1100 (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
1101 (cons (symbol-name-default-case "NOT") "NOT")
1102 (cons (symbol-name-default-case "NULL") "NULL")
1103 (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
1104 (cons (symbol-name-default-case "KEY") "KEY")
1105 (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
1106 (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
1107 (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
1108 (cons (symbol-name-default-case "DEFAULT") "DEFAULT")
1109 (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
1110 (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;; added for sql-server support
1113 (defmethod database-constraint-statement (constraint-list database)
1114 (declare (ignore database))
1115 (make-constraints-description constraint-list))
1117 (defun make-constraints-description (constraint-list)
1120 (do ((constraint constraint-list (cdr constraint)))
1121 ((null constraint) string)
1122 (let ((output (assoc (symbol-name (car constraint))
1126 (error 'sql-user-error
1127 :message (format nil "unsupported column constraint '~A'"
1129 (setq string (concatenate 'string string (cdr output))))
1130 (when (equal (symbol-name (car constraint)) "DEFAULT")
1131 (setq constraint (cdr constraint))
1132 (setq string (concatenate 'string string " " (car constraint))))
1133 (if (< 1 (length constraint))
1134 (setq string (concatenate 'string string " "))))))))
1136 (defmethod database-identifier ( name &optional database find-class-p
1138 "A function that takes whatever you give it, recursively coerces it,
1139 and returns a database-identifier.
1141 (escaped-database-identifiers *any-reasonable-object*) should be called to
1142 produce a string that is safe to splice directly into sql strings.
1144 This function should NOT throw errors when database is nil
1146 find-class-p should be T if we want to search for classes
1147 and check their use their view table. Should be used
1148 on symbols we are sure indicate tables
1151 ;; metaclasses has further typecases of this, so that it will
1152 ;; load less painfully (try-recompiles) in SBCL
1155 (flet ((flatten-id (id)
1156 "if we have multiple pieces that we need to represent as
1157 db-id lets do that by rendering out the id, then creating
1158 a new db-id with that string as escaped"
1159 (let ((s (sql-output id database)))
1160 (make-instance '%database-identifier :escaped s :unescaped s))))
1161 (setf name (dequote name))
1164 (string (%make-database-identifier name database))
1166 ;; if this is being used as a table, we should check
1167 ;; for a class with this name and use the identifier specified
1169 (if (and find-class-p (setf cls (find-standard-db-class name)))
1170 (database-identifier cls)
1171 (%make-database-identifier name database)))
1172 (%database-identifier name)
1173 ;; we know how to deref this without further escaping
1175 (with-slots ((inner-name name) alias) name
1178 (database-identifier inner-name))))
1179 ;; if this is a single name we can derefence it
1180 (sql-ident-attribute
1181 (with-slots (qualifier (inner-name name)) name
1184 (database-identifier inner-name))))
1186 (with-slots ((inner-name name)) name
1187 (database-identifier inner-name)))
1188 ;; dont know how to handle this really :/
1189 (%sql-expression (flatten-id name))
1192 (defun %clsql-subclauses (clauses)
1193 "a helper for dealing with lists of sql clauses"
1194 (loop for c in clauses
1197 (string (clsql-sys:sql-expression :string c))
1200 (defun clsql-ands (clauses)
1201 "Correctly creates a sql 'and' expression for the clauses
1202 ignores any nil clauses
1203 returns a single child expression if there is only one
1204 returns an 'and' expression if there are many
1205 returns nil if there are no children"
1206 (let ((ex (%clsql-subclauses clauses)))
1210 (t (apply #'clsql-sys:sql-and ex))))))
1212 (defun clsql-and (&rest clauses)
1213 "Correctly creates a sql 'and' expression for the clauses
1214 ignores any nil clauses
1215 returns a single child expression if there is only one
1216 returns an 'and' expression if there are many
1217 returns nil if there are no children"
1218 (clsql-ands clauses))
1220 (defun clsql-ors (clauses)
1221 "Correctly creates a sql 'or' expression for the clauses
1222 ignores any nil clauses
1223 returns a single child expression if there is only one
1224 returns an 'or' expression if there are many
1225 returns nil if there are no children"
1226 (let ((ex (%clsql-subclauses clauses)))
1230 (t (apply #'clsql-sys:sql-or ex))))))
1232 (defun clsql-or (&rest clauses)
1233 "Correctly creates a sql 'or' expression for the clauses
1234 ignores any nil clauses
1235 returns a single child expression if there is only one
1236 returns an 'or' expression if there are many
1237 returns nil if there are no children"
1238 (clsql-ors clauses))