X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Fexpressions.lisp;h=9335ae0e0a5e0995357da7cabaa331de00778950;hb=2c69ef4d171d214a7d54b598184dadb864704852;hp=bee6faffafa22dfc543255d3892890a2ce4736cd;hpb=d86f73be9a261b9c071ab905aeff5d1ee30a3f31;p=clsql.git diff --git a/sql/expressions.lisp b/sql/expressions.lisp index bee6faf..9335ae0 100644 --- a/sql/expressions.lisp +++ b/sql/expressions.lisp @@ -139,11 +139,9 @@ (defvar *output-hash* - #+sbcl - (make-hash-table :test #'equal :synchronized T :weakness :key-and-value) - #-sbcl - (make-hash-table :test #'equal ) - "For caching generated SQL strings.") + (make-weak-hash-table :test #'equal) + "For caching generated SQL strings, set to NIL to disable." + ) (defmethod output-sql :around ((sql t) database) (if (null *output-hash*) @@ -675,6 +673,20 @@ uninclusive, and the args from that keyword to the end." :group-by group-by :having having :order-by order-by :inner-join inner-join :on on)))))) +(defun output-sql-where-clause (where database) + "ensure that we do not output a \"where\" sql keyword when we will + not output a clause. Also sets *in-subselect* to use SQL + parentheticals as needed." + (when where + (let ((where-out (string-trim + '(#\newline #\space #\tab #\return) + (with-output-to-string (*sql-stream*) + (let ((*in-subselect* t)) + (output-sql where database)))))) + (when (> (length where-out) 0) + (write-string " WHERE " *sql-stream*) + (write-string where-out *sql-stream*))))) + (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) @@ -714,15 +726,7 @@ uninclusive, and the args from that keyword to the end." (when on (write-string " ON " *sql-stream*) (output-sql on database)) - (when where - (let ((where-out (string-trim - '(#\newline #\space #\tab #\return) - (with-output-to-string (*sql-stream*) - (let ((*in-subselect* t)) - (output-sql where database)))))) - (when (> (length where-out) 0) - (write-string " WHERE " *sql-stream*) - (write-string where-out *sql-stream*)))) + (output-sql-where-clause where database) (when group-by (write-string " GROUP BY " *sql-stream*) (if (listp group-by) @@ -834,9 +838,7 @@ uninclusive, and the args from that keyword to the end." (typecase from ((or symbol string) (write-string (sql-escape from) *sql-stream*)) (t (output-sql from database))) - (when where - (write-string " WHERE " *sql-stream*) - (output-sql where database))) + (output-sql-where-clause where database)) t) ;; UPDATE @@ -869,9 +871,7 @@ uninclusive, and the args from that keyword to the end." (output-sql table database) (write-string " SET " *sql-stream*) (output-sql (apply #'vector (update-assignments)) database) - (when where - (write-string " WHERE " *sql-stream*) - (output-sql where database)))) + (output-sql-where-clause where database))) t) ;; CREATE TABLE