projects
/
clsql.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Pulled a flet out into a method (select-table-sql-expr) which can be
[clsql.git]
/
sql
/
expressions.lisp
diff --git
a/sql/expressions.lisp
b/sql/expressions.lisp
index 7389d1c06470690d04943ffa8b077d8bf1cb08e2..c7fd033da646d6c0a17520a64f129f79fb37ad34 100644
(file)
--- a/
sql/expressions.lisp
+++ b/
sql/expressions.lisp
@@
-192,15
+192,25
@@
sql
`(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
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
(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)
t)
(defmethod output-sql-hash-key ((expr sql-ident-table) database)
@@
-411,7
+421,7
@@
(defmethod output-sql ((expr sql-query-modifier-exp) database)
(with-slots (modifier components)
expr
(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*)
(output-sql (car components) database)
(when components
@@
-577,13
+587,17
@@
uninclusive, and the args from that keyword to the end."
(write-string "(" *sql-stream*))
(write-string "SELECT " *sql-stream*)
(when all
(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))
(when (and distinct (not all))
- (write-string "DISTINCT " *sql-stream*)
+ (write-string "
DISTINCT " *sql-stream*)
(unless (eql t distinct)
(unless (eql t distinct)
- (write-string "ON " *sql-stream*)
+ (write-string "
ON " *sql-stream*)
(output-sql distinct database)
(write-char #\Space *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
(let ((*in-subselect* t))
(output-sql (apply #'vector selections) database))
(when from
@@
-647,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 (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
(write-string " LIMIT " *sql-stream*)
(output-sql limit database))
(when offset
@@
-787,9
+801,9
@@
uninclusive, and the args from that keyword to the end."
(declaim (inline listify))
(defun listify (x)
(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)
(defmethod output-sql ((stmt sql-create-table) database)
(flet ((output-column (column-spec)
@@
-980,7
+994,10
@@
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 "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))
(defmethod database-constraint-statement (constraint-list database)
(declare (ignore database))
@@
-999,6
+1016,9
@@
uninclusive, and the args from that keyword to the end."
:message (format nil "unsupported column constraint '~A'"
constraint))
(setq string (concatenate 'string string (cdr output))))
: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 " "))))))))
(if (< 1 (length constraint))
(setq string (concatenate 'string string " "))))))))