X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Fexpressions.lisp;h=d7a95ef74f83a11cd85ab96e69ed87af14cfe9ad;hb=539444af449f3769b75f71b32f10204c40d011f4;hp=91a46d7fb2c6c30592056c6ea98c2c25e260aee7;hpb=5ed1f05543cbd24b3f2bb735f2cfc03ea85e51ec;p=clsql.git diff --git a/sql/expressions.lisp b/sql/expressions.lisp index 91a46d7..d7a95ef 100644 --- a/sql/expressions.lisp +++ b/sql/expressions.lisp @@ -110,6 +110,7 @@ `(make-instance 'sql-ident :name ',name))) (defmethod output-sql ((expr sql-ident) database) + (with-slots (name) expr (write-string (etypecase name @@ -147,33 +148,32 @@ :type ',type))) (defmethod output-sql ((expr sql-ident-attribute) database) - (with-slots (qualifier name type) expr - (if (and (not qualifier) (not type)) - (etypecase name - (string - (write-string name *sql-stream*)) - (symbol - (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 - (sql-escape qualifier)) - (sql-escape name) - (when type - (symbol-name type))) - (format *sql-stream* "~@[~A.~]~A" - (when qualifier - (typecase qualifier - (string (format nil "~s" qualifier)) - (t (sql-escape qualifier)))) - (typecase name - (string (format nil "~s" (sql-escape name))) - (t (sql-escape name))))) - t)) +;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it +;;; should not be output in SQL statements + (let ((*print-pretty* nil)) + (labels ((quoted-string-p (inp) + (and (char-equal #\" (elt inp 0)) + (char-equal #\" (elt inp (1- (length inp)))))) + (safety-first (inp) + "do our best not to output sql that we can guarantee is invalid. + if the ident has a space or quote in it, instead output a quoted + identifier containing those chars" + (when (and (not (quoted-string-p inp)) + (find-if + (lambda (x) (member x '(#\space #\' #\") + :test #'char-equal)) inp)) + (setf inp (format nil "~s" (substitute "\\\"" "\"" inp :test #'string-equal)))) + inp)) + (with-slots (qualifier name type) expr + (format *sql-stream* "~@[~a.~]~a" + (typecase qualifier + (null nil) ; nil is a symbol + (string (format nil "~s" qualifier)) + (symbol (safety-first (sql-escape qualifier)))) + (typecase name ;; could never get this to be nil without getting another error first + (string (format nil "~s" name)) + (symbol (safety-first (sql-escape name))))) + t)))) (defmethod output-sql-hash-key ((expr sql-ident-attribute) database) (with-slots (qualifier name type) @@ -242,18 +242,42 @@ ;; should do arity checking of subexpressions (defmethod output-sql ((expr sql-relational-exp) database) + (with-slots (operator sub-expressions) + expr + (when sub-expressions + (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*)))) + t) + +(defclass sql-array-exp (sql-relational-exp) + () + (:documentation "An SQL relational expression.")) + +(defmethod output-sql ((expr sql-array-exp) database) (with-slots (operator sub-expressions) expr (let ((subs (if (consp (car sub-expressions)) (car sub-expressions) sub-expressions))) (write-char #\( *sql-stream*) + (output-sql operator database) + (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 #\, *sql-stream*) (write-char #\Space *sql-stream*)) + (write-char #\] *sql-stream*) (write-char #\) *sql-stream*))) t) @@ -565,13 +589,17 @@ uninclusive, and the args from that keyword to the end." (write-string "SELECT " *sql-stream*) (when all (write-string "ALL " *sql-stream*)) + (when (and limit (eq :odbc (database-type database))) + (write-string " TOP " *sql-stream*) + (output-sql limit database)) (when (and distinct (not all)) (write-string "DISTINCT " *sql-stream*) (unless (eql t distinct) (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) @@ -633,7 +661,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 (eq :odbc (database-type database)))) (write-string " LIMIT " *sql-stream*) (output-sql limit database)) (when offset @@ -966,7 +994,9 @@ uninclusive, and the args from that keyword to the end." (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED") (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL") (cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT") - (cons (symbol-name-default-case "UNIQUE") "UNIQUE"))) + (cons (symbol-name-default-case "UNIQUE") "UNIQUE") + (cons (symbol-name-default-case "IDENTITY") "IDENTITY (1,1)") ;Added Identity for MS-SQLServer support + )) (defmethod database-constraint-statement (constraint-list database) (declare (ignore database))