fixed :from clauses to not throw errors when presented with a string or string expression
[clsql.git] / sql / expressions.lisp
index 45d4631b5eaccf70b878b49a6b79907b9d4930c4..b80806439c0ac71f13ab77e0a379e4e46cd0afbd 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
@@ -587,25 +587,31 @@ uninclusive, and the args from that keyword to the end."
       (write-string "(" *sql-stream*))
     (write-string "SELECT " *sql-stream*)
     (when all
-      (write-string "ALL " *sql-stream*))
+      (write-string " ALL " *sql-stream*))
     (when (and distinct (not all))
-      (write-string "DISTINCT " *sql-stream*)
+      (write-string " DISTINCT " *sql-stream*)
       (unless (eql t distinct)
-        (write-string "ON " *sql-stream*)
+        (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
       (write-string " FROM " *sql-stream*)
-      (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 (sql-escape (slot-value a 'name))
-                                  (sql-escape (slot-value b 'name))))))
+      (labels ((ident-string-val (a)
+                 (typecase a
+                   (sql-ident
+                    (or (ignore-errors (slot-value a 'alias))
+                        (ignore-errors (slot-value a 'name))))
+                   (string a)))
+               (ident-table-equal (a b)
+                 ;; The things should be type compatable
+                 (string-equal (ident-string-val a)
+                               (ident-string-val b))))
         (typecase from
           (list (output-sql (apply #'vector
                                    (remove-duplicates from
@@ -621,9 +627,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)
@@ -657,7 +668,7 @@ uninclusive, and the args from that keyword to the end."
               (when (cdr order)
                 (write-char #\, *sql-stream*))))
           (output-sql order-by database)))
-    (when limit
+    (when (and limit (not (eql :mssql (database-underlying-type database))))
       (write-string " LIMIT " *sql-stream*)
       (output-sql limit database))
     (when offset
@@ -797,9 +808,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)
@@ -991,7 +1002,9 @@ uninclusive, and the args from that keyword to the end."
    (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
    (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
    (cons (symbol-name-default-case "DEFAULT") "DEFAULT")
-   (cons (symbol-name-default-case "UNIQUE") "UNIQUE")))
+   (cons (symbol-name-default-case "UNIQUE") "UNIQUE")
+   (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;; added for sql-server support
+   ))
 
 (defmethod database-constraint-statement (constraint-list database)
   (declare (ignore database))