changed how where clauses are output to ensure that we do not output a
[clsql.git] / sql / expressions.lisp
index d09b154e0ef99907195f6b41f078f420e7c95283..efb54bb650f00338d4b17590b9e6d346060aa3ef 100644 (file)
 (defmethod output-sql ((expr sql-query-modifier-exp) database)
   (with-slots (modifier components)
       expr
-    (output-sql modifier database)
+    (%write-operator modifier database)
     (write-string " " *sql-stream*)
     (output-sql (car components) database)
     (when components
@@ -586,10 +586,6 @@ uninclusive, and the args from that keyword to the end."
     (when *in-subselect*
       (write-string "(" *sql-stream*))
     (write-string "SELECT " *sql-stream*)
-    (when (and limit (eql :mssql (database-underlying-type database)))
-      (write-string " TOP " *sql-stream*)
-      (output-sql limit database)
-      (write-string " " *sql-stream*))
     (when all
       (write-string " ALL " *sql-stream*))
     (when (and distinct (not all))
@@ -598,6 +594,10 @@ uninclusive, and the args from that keyword to the end."
         (write-string " ON " *sql-stream*)
         (output-sql distinct database)
         (write-char #\Space *sql-stream*)))
+    (when (and limit (eql :mssql (database-underlying-type database)))
+      (write-string " TOP " *sql-stream*)
+      (output-sql limit database)
+      (write-string " " *sql-stream*))
     (let ((*in-subselect* t))
       (output-sql (apply #'vector selections) database))
     (when from
@@ -625,9 +625,14 @@ uninclusive, and the args from that keyword to the end."
       (write-string " ON " *sql-stream*)
       (output-sql on database))
     (when where
-      (write-string " WHERE " *sql-stream*)
-      (let ((*in-subselect* t))
-        (output-sql where database)))
+      (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*))))
     (when group-by
       (write-string " GROUP BY " *sql-stream*)
       (if (listp group-by)
@@ -801,9 +806,9 @@ uninclusive, and the args from that keyword to the end."
 
 (declaim (inline listify))
 (defun listify (x)
-  (if (atom x)
-      (list x)
-      x))
+  (if (listp x)
+      x
+      (list x)))
 
 (defmethod output-sql ((stmt sql-create-table) database)
   (flet ((output-column (column-spec)