;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;;
-;;;; $Id$
-;;;;
;;;; Classes defining SQL expressions and methods for formatting the
;;;; appropriate SQL commands.
;;;;
(write-string
(etypecase name
(string name)
- (symbol (symbol-name name) database))
+ (symbol (symbol-name name)))
*sql-stream*))
t)
sql
`(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
+(defun special-char-p (s)
+ "Check if a string has any special characters"
+ (loop for char across s
+ thereis (find char '(#\space #\, #\. #\! #\@ #\# #\$ #\%
+ #\^ #\& #\* #\| #\( #\) #\- #\+))))
+
(defmethod output-sql ((expr sql-ident-table) database)
(with-slots (name alias) expr
- (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)))
+ (flet ((p (s) ;; the etypecase is in sql-escape too
+ (let ((sym? (symbolp s))
+ (s (sql-escape s)))
+ (format *sql-stream*
+ (if (and sym? (not (special-char-p s)))
+ "~a" "~s")
+ s))))
+ (p name)
+ (when alias
+ (princ #\space *sql-stream*)
+ (p 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)
(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
(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
(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)
(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
(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)
(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 "DEFAULT") "DEFAULT")
+ (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))
:message (format nil "unsupported column constraint '~A'"
constraint))
(setq string (concatenate 'string string (cdr output))))
+ (when (equal (symbol-name (car constraint)) "DEFAULT")
+ (setq constraint (cdr constraint))
+ (setq string (concatenate 'string string " " (car constraint))))
(if (< 1 (length constraint))
(setq string (concatenate 'string string " "))))))))