Remove CVS $Id$ keyword
[clsql.git] / sql / expressions.lisp
index df9cfc08b55da8a5195310172e23402a8ea29c43..7389d1c06470690d04943ffa8b077d8bf1cb08e2 100644 (file)
@@ -1,8 +1,6 @@
 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
 ;;;; *************************************************************************
 ;;;;
-;;;; $Id$
-;;;;
 ;;;; Classes defining SQL expressions and methods for formatting the
 ;;;; appropriate SQL commands.
 ;;;;
 (defmethod output-sql ((expr sql-ident) database)
   (with-slots (name) expr
     (write-string
-     (convert-to-db-default-case
-      (etypecase name
-        (string name)
-        (symbol (symbol-name name)))
-      database)
+     (etypecase name
+       (string name)
+       (symbol (symbol-name name)))
      *sql-stream*))
   t)
 
 
 (defmethod collect-table-refs ((sql sql-ident-attribute))
   (let ((qual (slot-value sql 'qualifier)))
-    (if (and qual (symbolp (slot-value sql 'qualifier)))
-        (list (make-instance 'sql-ident-table :name
-                             (slot-value sql 'qualifier))))))
+    (when qual
+      (list (make-instance 'sql-ident-table :name qual)))))
 
 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
   (declare (ignore environment))
   (with-slots (qualifier name type) expr
     (if (and (not qualifier) (not type))
         (etypecase name
-          ;; Honor care of name
           (string
            (write-string name *sql-stream*))
           (symbol
-           (write-string (sql-escape (convert-to-db-default-case
-                                      (symbol-name name) database)) *sql-stream*)))
+           (write-string
+            (sql-escape (symbol-name name)) *sql-stream*)))
 
         ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
       ;;; should not be output in SQL statements
       #+ignore
       (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
               (when qualifier
-                (convert-to-db-default-case (sql-escape qualifier) database))
-              (sql-escape (convert-to-db-default-case name database))
+                (sql-escape qualifier))
+              (sql-escape name)
               (when type
-                (convert-to-db-default-case (symbol-name type) database)))
+                (symbol-name type)))
       (format *sql-stream* "~@[~A.~]~A"
               (when qualifier
                 (typecase qualifier
                   (string (format nil "~s" qualifier))
-                  (t (convert-to-db-default-case (sql-escape qualifier)
-                                                 database))))
-              (sql-escape (convert-to-db-default-case name database))))
+                  (t (sql-escape qualifier))))
+              (typecase name
+                (string (format nil "~s" (sql-escape name)))
+                (t (sql-escape name)))))
     t))
 
 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
 
 (defmethod output-sql ((expr sql-ident-table) database)
   (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)))))
+    (etypecase name
+      (string
+       (format *sql-stream* "~s" (sql-escape name)))
+      (symbol
+       (write-string (sql-escape name) *sql-stream*)))
+    (when alias
+      (format *sql-stream* " ~s" alias)))
   t)
 
 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
 ;; should do arity checking of subexpressions
 
 (defmethod output-sql ((expr sql-relational-exp) database)
-  (with-slots (operator sub-expressions)
-    expr
-    (let ((subs (if (consp (car sub-expressions))
-                    (car sub-expressions)
-                    sub-expressions)))
-      (write-char #\( *sql-stream*)
-      (do ((sub subs (cdr sub)))
-          ((null (cdr sub)) (output-sql (car sub) database))
-        (output-sql (car sub) database)
-        (write-char #\Space *sql-stream*)
-        (output-sql operator database)
-        (write-char #\Space *sql-stream*))
-      (write-char #\) *sql-stream*)))
+  (with-slots (operator sub-expressions) expr
+     ;; we do this as two runs so as not to emit confusing superflous parentheses
+     ;; The first loop renders all the child outputs so that we can skip anding with
+     ;; empty output (which causes sql errors)
+     ;; the next loop simply emits each sub-expression with the appropriate number of
+     ;; parens and operators
+     (flet ((trim (sub)
+             (string-trim +whitespace-chars+
+                          (with-output-to-string (*sql-stream*)
+                            (output-sql sub database)))))
+       (let ((str-subs (loop for sub in sub-expressions
+                            for str-sub = (trim sub)
+                          when (and str-sub (> (length str-sub) 0))
+                            collect str-sub)))
+        (case (length str-subs)
+          (0 nil)
+          (1 (write-string (first str-subs) *sql-stream*))
+          (t
+             (write-char #\( *sql-stream*)
+             (write-string (first str-subs) *sql-stream*)
+             (loop for str-sub in (rest str-subs)
+                   do
+                (write-char #\Space *sql-stream*)
+                (output-sql operator database)
+                (write-char #\Space *sql-stream*)
+                (write-string str-sub *sql-stream*))
+             (write-char #\) *sql-stream*))
+          ))))
   t)
 
 (defclass sql-upcase-like (sql-relational-exp)
@@ -580,7 +584,8 @@ uninclusive, and the args from that keyword to the end."
         (write-string "ON " *sql-stream*)
         (output-sql distinct database)
         (write-char #\Space *sql-stream*)))
-    (output-sql (apply #'vector selections) database)
+    (let ((*in-subselect* t))
+      (output-sql (apply #'vector selections) database))
     (when from
       (write-string " FROM " *sql-stream*)
       (flet ((ident-table-equal (a b)
@@ -596,7 +601,7 @@ uninclusive, and the args from that keyword to the end."
                                    (remove-duplicates from
                                                       :test #'ident-table-equal))
                             database))
-          (string (write-string from *sql-stream*))
+          (string (format *sql-stream* "~s" (sql-escape from)))
           (t (let ((*in-subselect* t))
                (output-sql from database))))))
     (when inner-join
@@ -687,7 +692,7 @@ uninclusive, and the args from that keyword to the end."
     (write-string "INSERT INTO " *sql-stream*)
     (output-sql
      (typecase into
-       (string (sql-expression :attribute into))
+       (string (sql-expression :table into))
        (t into))
      database)
     (when attributes
@@ -809,7 +814,10 @@ uninclusive, and the args from that keyword to the end."
     (with-slots (name columns modifiers transactions)
       stmt
       (write-string "CREATE TABLE " *sql-stream*)
-      (output-sql name database)
+      (etypecase name
+          (string (format *sql-stream* "~s" (sql-escape name)))
+          (symbol (write-string (sql-escape name) *sql-stream*))
+          (sql-ident (output-sql name database)))
       (write-string " (" *sql-stream*)
       (do ((column columns (cdr column)))
           ((null (cdr column))
@@ -891,11 +899,9 @@ uninclusive, and the args from that keyword to the end."
   (defmethod database-output-sql ((sym symbol) database)
   (if (null sym)
       +null-string+
-      (convert-to-db-default-case
-       (if (equal (symbol-package sym) keyword-package)
-           (concatenate 'string "'" (string sym) "'")
-           (symbol-name sym))
-       database))))
+    (if (equal (symbol-package sym) keyword-package)
+        (concatenate 'string "'" (string sym) "'")
+      (symbol-name sym)))))
 
 (defmethod database-output-sql ((tee (eql t)) database)
   (if database