X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=sql%2Fexpressions.lisp;h=cd1a6d397bc9a2665bf0472a80d5d14aa563e602;hp=88293b30d360e24207e187e85c54e12bac7d99dd;hb=78489032c6f66ce666ffe5e2e726503b61b94616;hpb=aa707c93f55554dc5447c58f24e1b3ad4c01f0b9 diff --git a/sql/expressions.lisp b/sql/expressions.lisp index 88293b3..cd1a6d3 100644 --- a/sql/expressions.lisp +++ b/sql/expressions.lisp @@ -112,7 +112,7 @@ (defmethod output-sql ((expr sql-ident) database) (with-slots (name) expr (write-string - (convert-to-db-default-case + (convert-to-db-default-case (etypecase name (string name) (symbol (symbol-name name))) @@ -157,9 +157,9 @@ (string (write-string name *sql-stream*)) (symbol - (write-string (sql-escape (convert-to-db-default-case + (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*))) - + ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it ;;; should not be output in SQL statements #+ignore @@ -171,9 +171,9 @@ (convert-to-db-default-case (symbol-name type) database))) (format *sql-stream* "~@[~A.~]~A" (when qualifier - (typecase qualifier + (typecase qualifier (string (format nil "~s" qualifier)) - (t (convert-to-db-default-case (sql-escape qualifier) + (t (convert-to-db-default-case (sql-escape qualifier) database)))) (sql-escape (convert-to-db-default-case name database)))) t)) @@ -198,14 +198,20 @@ `(make-instance 'sql-ident-table :name ',name :table-alias ',alias))) (defmethod output-sql ((expr sql-ident-table) database) - (with-slots (name alias) - expr - (if (null alias) - (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*) - (progn - (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*) - (write-char #\Space *sql-stream*) - (format *sql-stream* "~s" alias)))) + (with-slots (name alias) expr + (let ((namestr (if (symbolp name) + (symbol-name name) + name))) + (if (null alias) + (write-string + (sql-escape (convert-to-db-default-case namestr database)) + *sql-stream*) + (progn + (write-string + (sql-escape (convert-to-db-default-case namestr database)) + *sql-stream*) + (write-char #\Space *sql-stream*) + (format *sql-stream* "~s" alias))))) t) (defmethod output-sql-hash-key ((expr sql-ident-table) database) @@ -223,6 +229,11 @@ :initform nil)) (:documentation "An SQL relational expression.")) +(defmethod make-load-form ((self sql-relational-exp) &optional environment) + (make-load-form-saving-slots self + :slot-names '(operator sub-expressions) + :environment environment)) + (defmethod collect-table-refs ((sql sql-relational-exp)) (let ((tabs nil)) (dolist (exp (slot-value sql 'sub-expressions)) @@ -258,7 +269,7 @@ (defclass sql-upcase-like (sql-relational-exp) () (:documentation "An SQL 'like' that upcases its arguments.")) - + (defmethod output-sql ((expr sql-upcase-like) database) (flet ((write-term (term) (write-string "upper(" *sql-stream*) @@ -375,12 +386,12 @@ (defclass sql-between-exp (sql-function-exp) - () + () (:documentation "An SQL between expression.")) (defmethod output-sql ((expr sql-between-exp) database) (with-slots (name args) - expr + expr (output-sql (first args) database) (write-string " BETWEEN " *sql-stream*) (output-sql (second args) database) @@ -388,7 +399,7 @@ (output-sql (third args) database)) t) -(defclass sql-query-modifier-exp (%sql-expression) +(defclass sql-query-modifier-exp (%sql-expression) ((modifier :initarg :modifier :initform nil) (components :initarg :components :initform nil)) (:documentation "An SQL query modifier expression.")) @@ -399,8 +410,8 @@ (output-sql modifier database) (write-string " " *sql-stream*) (output-sql (car components) database) - (when components - (mapc #'(lambda (comp) + (when components + (mapc #'(lambda (comp) (write-string ", " *sql-stream*) (output-sql comp database)) (cdr components)))) @@ -534,13 +545,13 @@ uninclusive, and the args from that keyword to the end." target-args)))) (multiple-value-bind (selections arglist) (query-get-selections args) - (if (select-objects selections) + (if (select-objects selections) (destructuring-bind (&key flatp refresh &allow-other-keys) arglist (make-instance 'sql-object-query :objects selections :flatp flatp :refresh refresh :exp arglist)) (destructuring-bind (&key all flatp set-operation distinct from where - group-by having order-by + group-by having order-by offset limit inner-join on &allow-other-keys) arglist (if (null selections) @@ -556,12 +567,12 @@ uninclusive, and the args from that keyword to the end." (defmethod output-sql ((query sql-query) database) (with-slots (distinct selections from where group-by having order-by - limit offset inner-join on all set-operation) + limit offset inner-join on all set-operation) query (when *in-subselect* (write-string "(" *sql-stream*)) (write-string "SELECT " *sql-stream*) - (when all + (when all (write-string "ALL " *sql-stream*)) (when (and distinct (not all)) (write-string "DISTINCT " *sql-stream*) @@ -572,21 +583,22 @@ uninclusive, and the args from that keyword to the end." (output-sql (apply #'vector selections) database) (when from (write-string " FROM " *sql-stream*) - (flet ((ident-table-equal (a b) + (flet ((ident-table-equal (a b) (and (if (and (eql (type-of a) 'sql-ident-table) (eql (type-of b) 'sql-ident-table)) (string-equal (slot-value a 'alias) (slot-value b 'alias)) t) - (string-equal (symbol-name (slot-value a 'name)) - (symbol-name (slot-value b 'name)))))) - (typecase from - (list (output-sql (apply #'vector - (remove-duplicates from + (string-equal (sql-escape (slot-value a 'name)) + (sql-escape (slot-value b 'name)))))) + (typecase from + (list (output-sql (apply #'vector + (remove-duplicates from :test #'ident-table-equal)) database)) (string (write-string from *sql-stream*)) - (t (output-sql from database))))) + (t (let ((*in-subselect* t)) + (output-sql from database)))))) (when inner-join (write-string " INNER JOIN " *sql-stream*) (output-sql inner-join database)) @@ -599,7 +611,19 @@ uninclusive, and the args from that keyword to the end." (output-sql where database))) (when group-by (write-string " GROUP BY " *sql-stream*) - (output-sql group-by database)) + (if (listp group-by) + (do ((order group-by (cdr order))) + ((null order)) + (let ((item (car order))) + (typecase item + (cons + (output-sql (car item) database) + (format *sql-stream* " ~A" (cadr item))) + (t + (output-sql item database))) + (when (cdr order) + (write-char #\, *sql-stream*)))) + (output-sql group-by database))) (when having (write-string " HAVING " *sql-stream*) (output-sql having database)) @@ -609,11 +633,11 @@ uninclusive, and the args from that keyword to the end." (do ((order order-by (cdr order))) ((null order)) (let ((item (car order))) - (typecase item - (cons + (typecase item + (cons (output-sql (car item) database) (format *sql-stream* " ~A" (cadr item))) - (t + (t (output-sql item database))) (when (cdr order) (write-char #\, *sql-stream*)))) @@ -626,7 +650,7 @@ uninclusive, and the args from that keyword to the end." (output-sql offset database)) (when *in-subselect* (write-string ")" *sql-stream*)) - (when set-operation + (when set-operation (write-char #\Space *sql-stream*) (output-sql set-operation database))) t) @@ -661,10 +685,10 @@ uninclusive, and the args from that keyword to the end." (with-slots (into attributes values query) ins (write-string "INSERT INTO " *sql-stream*) - (output-sql + (output-sql (typecase into (string (sql-expression :attribute into)) - (t into)) + (t into)) database) (when attributes (write-char #\Space *sql-stream*) @@ -694,7 +718,7 @@ uninclusive, and the args from that keyword to the end." stmt (write-string "DELETE FROM " *sql-stream*) (typecase from - (symbol (write-string (sql-escape from) *sql-stream*)) + ((or symbol string) (write-string (sql-escape from) *sql-stream*)) (t (output-sql from database))) (when where (write-string " WHERE " *sql-stream*) @@ -774,7 +798,7 @@ uninclusive, and the args from that keyword to the end." (database-get-type-specifier (car type) (cdr type) database (database-underlying-type database))) *sql-stream*) - (let ((constraints (database-constraint-statement + (let ((constraints (database-constraint-statement (if (and db-type (symbolp db-type)) (cons db-type constraints) constraints) @@ -801,7 +825,7 @@ uninclusive, and the args from that keyword to the end." (when (and (eq :mysql (database-underlying-type database)) transactions (db-type-transaction-capable? :mysql database)) - (write-string " Type=InnoDB" *sql-stream*)))) + (write-string " Type=InnoDB" *sql-stream*)))) t) @@ -826,13 +850,12 @@ uninclusive, and the args from that keyword to the end." ;; -;; DATABASE-OUTPUT-SQL -;; +;; DATABASE-OUTPUT-SQL +;; (defmethod database-output-sql ((str string) database) (declare (optimize (speed 3) (safety 1) - #+cmu (extensions:inhibit-warnings 3)) - (simple-string str)) + #+cmu (extensions:inhibit-warnings 3))) (let ((len (length str))) (declare (type fixnum len)) (cond ((zerop len) @@ -842,7 +865,8 @@ uninclusive, and the args from that keyword to the end." (concatenate 'string "'" str "'")) (t (let ((buf (make-string (+ (* len 2) 2) :initial-element #\'))) - (do* ((i 0 (incf i)) + (declare (simple-string buf)) + (do* ((i 0 (incf i)) (j 1 (incf j))) ((= i len) (subseq buf 0 (1+ j))) (declare (type fixnum i j)) @@ -853,8 +877,8 @@ uninclusive, and the args from that keyword to the end." (incf j) (setf (aref buf j) #\')) ((and (char= char #\\) - ;; MTP: only escape backslash with pgsql/mysql - (member (database-underlying-type database) + ;; MTP: only escape backslash with pgsql/mysql + (member (database-underlying-type database) '(:postgresql :mysql) :test #'eq)) (setf (aref buf j) #\\) @@ -865,8 +889,8 @@ uninclusive, and the args from that keyword to the end." (let ((keyword-package (symbol-package :foo))) (defmethod database-output-sql ((sym symbol) database) - (if (null sym) - +null-string+ + (if (null sym) + +null-string+ (convert-to-db-default-case (if (equal (symbol-package sym) keyword-package) (concatenate 'string "'" (string sym) "'") @@ -879,11 +903,11 @@ uninclusive, and the args from that keyword to the end." (defmethod database-output-sql ((num number) database) (declare (ignore database)) - (princ-to-string num)) + (number-to-sql-string num)) (defmethod database-output-sql ((arg list) database) - (if (null arg) - +null-string+ + (if (null arg) + +null-string+ (format nil "(~{~A~^,~})" (mapcar #'(lambda (val) (sql-output val database)) arg)))) @@ -903,11 +927,15 @@ uninclusive, and the args from that keyword to the end." (declare (ignore database)) (db-timestring self)) +(defmethod database-output-sql ((self date) database) + (declare (ignore database)) + (db-datestring self)) + (defmethod database-output-sql ((self duration) database) (declare (ignore database)) (format nil "'~a'" (duration-timestring self))) -#+ignore +#+ignore (defmethod database-output-sql ((self money) database) (database-output-sql (slot-value self 'odcl::units) database)) @@ -923,26 +951,26 @@ uninclusive, and the args from that keyword to the end." ;; -;; Column constraint types and conversion to SQL +;; Column constraint types and conversion to SQL ;; (defparameter *constraint-types* - (list - (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") + (list + (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY") - (cons (symbol-name-default-case "NOT") "NOT") - (cons (symbol-name-default-case "NULL") "NULL") - (cons (symbol-name-default-case "PRIMARY") "PRIMARY") + (cons (symbol-name-default-case "NOT") "NOT") + (cons (symbol-name-default-case "NULL") "NULL") + (cons (symbol-name-default-case "PRIMARY") "PRIMARY") (cons (symbol-name-default-case "KEY") "KEY") - (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED") - (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL") + (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED") + (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL") (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT") (cons (symbol-name-default-case "UNIQUE") "UNIQUE"))) (defmethod database-constraint-statement (constraint-list database) (declare (ignore database)) (make-constraints-description constraint-list)) - + (defun make-constraints-description (constraint-list) (if constraint-list (let ((string ""))