made the datetest table have a key column so that update-records-from-* works again
[clsql.git] / sql / expressions.lisp
index 7071227a4b0964c1be2a6ec971fd30cdbec59c67..983d4a526bb5667e1eb6fa17f2c6090c15f93823 100644 (file)
                             #\^ #\& #\* #\| #\( #\) #\- #\+ #\< #\>
                             #\{ #\}))))
 
+(defun special-cased-symbol-p (sym)
+  "Should the symbols case be preserved, or should we convert to default casing"
+  (let ((name (symbol-name sym)))
+    (case (readtable-case *readtable*)
+      (:upcase (not (string= (string-upcase name) name)))
+      (:downcase (not (string= (string-downcase name) name)))
+      (t t))))
+
 (defun %make-database-identifier (inp &optional database)
   "We want to quote an identifier if it came to us as a string or if it has special characters
    in it."
       (symbol
        (let ((s (sql-escape inp)))
          (if (and (not (eql '* inp)) (special-char-p s))
-             (%escape-identifier (convert-to-db-default-case s database) inp)
-             (make-instance '%database-identifier :escaped s :unescaped inp)))))))
+             (%escape-identifier
+              (if (special-cased-symbol-p inp)
+                  s
+                  (convert-to-db-default-case s database)) inp)
+             (make-instance '%database-identifier :escaped s :unescaped inp))
+         )))))
 
 (defun combine-database-identifiers (ids &optional (database clsql-sys:*default-database*)
                                      &aux res all-sym? pkg)
   "Top-level call for generating SQL strings. Returns an SQL
   string appropriate for DATABASE which corresponds to the
   supplied lisp expression SQL-EXPR."
-  (progv '(*sql-stream*)
-      `(,(make-string-output-stream))
-    (output-sql sql-expr database)
-    (get-output-stream-string *sql-stream*)))
+  (with-output-to-string (*sql-stream*)
+    (output-sql sql-expr database)))
 
 (defmethod output-sql (expr database)
   (write-string (database-output-sql expr database) *sql-stream*)
@@ -1118,7 +1128,7 @@ uninclusive, and the args from that keyword to the end."
 
 (defmethod database-identifier ( name  &optional database find-class-p
                                  &aux cls)
-  "A function that takes whatever you give it, recurively coerces it,
+  "A function that takes whatever you give it, recursively coerces it,
    and returns a database-identifier.
 
    (escaped-database-identifiers *any-reasonable-object*) should be called to
@@ -1141,6 +1151,7 @@ uninclusive, and the args from that keyword to the end."
             a new db-id with that string as escaped"
            (let ((s (sql-output id database)))
              (make-instance '%database-identifier :escaped s :unescaped s))))
+    (setf name (dequote name))
     (etypecase name
       (null nil)
       (string (%make-database-identifier name database))