"Top-level call for generating SQL strings. Returns an SQL
string appropriate for DATABASE which corresponds to the
supplied lisp expression SQL-EXPR."
"Top-level call for generating SQL strings. Returns an SQL
string appropriate for DATABASE which corresponds to the
supplied lisp expression SQL-EXPR."
(defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
(declare (ignore environment))
(defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
(declare (ignore environment))
(defmethod output-sql ((expr sql-ident-attribute) database)
(with-slots (qualifier name type) expr
(if (and (not qualifier) (not type))
(defmethod output-sql ((expr sql-ident-attribute) database)
(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*)))
-
- ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
+ (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
- (when qualifier
- (convert-to-db-default-case (sql-escape qualifier) database))
- (sql-escape (convert-to-db-default-case name database))
- (when type
- (convert-to-db-default-case (symbol-name type) database)))
+ (when qualifier
+ (sql-escape qualifier))
+ (sql-escape name)
+ (when type
+ (symbol-name type)))
- (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)))))
`(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
(defmethod output-sql ((expr sql-ident-table) database)
`(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
(defmethod output-sql ((expr sql-ident-table) database)
- (with-slots (name alias)
- expr
- (if (null alias)
- (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
- (progn
- (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
- (write-char #\Space *sql-stream*)
- (format *sql-stream* "~s" alias))))
+ (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)))
(defmethod collect-table-refs ((sql sql-relational-exp))
(let ((tabs nil))
(dolist (exp (slot-value sql 'sub-expressions))
(defmethod collect-table-refs ((sql sql-relational-exp))
(let ((tabs nil))
(dolist (exp (slot-value sql 'sub-expressions))
(defclass sql-upcase-like (sql-relational-exp)
()
(:documentation "An SQL 'like' that upcases its arguments."))
(defclass sql-upcase-like (sql-relational-exp)
()
(:documentation "An SQL 'like' that upcases its arguments."))
(defmethod output-sql ((expr sql-upcase-like) database)
(flet ((write-term (term)
(write-string "upper(" *sql-stream*)
(defmethod output-sql ((expr sql-upcase-like) database)
(flet ((write-term (term)
(write-string "upper(" *sql-stream*)
(:documentation "An SQL between expression."))
(defmethod output-sql ((expr sql-between-exp) database)
(:documentation "An SQL between expression."))
(defmethod output-sql ((expr sql-between-exp) database)
(output-sql (first args) database)
(write-string " BETWEEN " *sql-stream*)
(output-sql (second args) database)
(output-sql (first args) database)
(write-string " BETWEEN " *sql-stream*)
(output-sql (second args) database)
((modifier :initarg :modifier :initform nil)
(components :initarg :components :initform nil))
(:documentation "An SQL query modifier expression."))
((modifier :initarg :modifier :initform nil)
(components :initarg :components :initform nil))
(:documentation "An SQL query modifier expression."))
(output-sql modifier database)
(write-string " " *sql-stream*)
(output-sql (car components) database)
(output-sql modifier database)
(write-string " " *sql-stream*)
(output-sql (car components) database)
- (when components
- (mapc #'(lambda (comp)
- (write-string ", " *sql-stream*)
- (output-sql comp database))
- (cdr components))))
+ (when components
+ (mapc #'(lambda (comp)
+ (write-string ", " *sql-stream*)
+ (output-sql comp database))
+ (cdr components))))
- (query-get-selections args)
- (if (select-objects selections)
- (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
- (make-instance 'sql-object-query :objects selections
- :flatp flatp :refresh refresh
- :exp arglist))
- (destructuring-bind (&key all flatp set-operation distinct from where
- group-by having order-by
- offset limit inner-join on &allow-other-keys)
- arglist
- (if (null selections)
- (error "No target columns supplied to select statement."))
- (if (null from)
- (error "No source tables supplied to select statement."))
- (make-instance 'sql-query :selections selections
- :all all :flatp flatp :set-operation set-operation
- :distinct distinct :from from :where where
- :limit limit :offset offset
- :group-by group-by :having having :order-by order-by
- :inner-join inner-join :on on))))))
+ (query-get-selections args)
+ (if (select-objects selections)
+ (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
+ (make-instance 'sql-object-query :objects selections
+ :flatp flatp :refresh refresh
+ :exp arglist))
+ (destructuring-bind (&key all flatp set-operation distinct from where
+ group-by having order-by
+ offset limit inner-join on &allow-other-keys)
+ arglist
+ (if (null selections)
+ (error "No target columns supplied to select statement."))
+ (if (null from)
+ (error "No source tables supplied to select statement."))
+ (make-instance 'sql-query :selections selections
+ :all all :flatp flatp :set-operation set-operation
+ :distinct distinct :from from :where where
+ :limit limit :offset offset
+ :group-by group-by :having having :order-by order-by
+ :inner-join inner-join :on on))))))
(defmethod output-sql ((query sql-query) database)
(with-slots (distinct selections from where group-by having order-by
(defmethod output-sql ((query sql-query) database)
(with-slots (distinct selections from where group-by having order-by
(write-string "ALL " *sql-stream*))
(when (and distinct (not all))
(write-string "DISTINCT " *sql-stream*)
(write-string "ALL " *sql-stream*))
(when (and distinct (not all))
(write-string "DISTINCT " *sql-stream*)
(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)
(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 (symbol-name (slot-value a 'name))
- (symbol-name (slot-value b 'name))))))
- (typecase from
- (list (output-sql (apply #'vector
- (remove-duplicates from
+ (string-equal (sql-escape (slot-value a 'name))
+ (sql-escape (slot-value b 'name))))))
+ (typecase from
+ (list (output-sql (apply #'vector
+ (remove-duplicates from
- (string (write-string from *sql-stream*))
- (t (output-sql from database)))))
+ (string (format *sql-stream* "~s" (sql-escape from)))
+ (t (let ((*in-subselect* t))
+ (output-sql from database))))))
- (output-sql group-by database))
+ (if (listp group-by)
+ (do ((order group-by (cdr order)))
+ ((null order))
+ (let ((item (car order)))
+ (typecase item
+ (cons
+ (output-sql (car item) database)
+ (format *sql-stream* " ~A" (cadr item)))
+ (t
+ (output-sql item database)))
+ (when (cdr order)
+ (write-char #\, *sql-stream*))))
+ (output-sql group-by database)))
(output-sql item database)))
(when (cdr order)
(write-char #\, *sql-stream*))))
(output-sql item database)))
(when (cdr order)
(write-char #\, *sql-stream*))))
- (database-get-type-specifier (car type) (cdr type) database
- (database-underlying-type database)))
+ (database-get-type-specifier (car type) (cdr type) database
+ (database-underlying-type database)))
(write-string (car modifier) *sql-stream*)))
(write-char #\) *sql-stream*)
(when (and (eq :mysql (database-underlying-type database))
(write-string (car modifier) *sql-stream*)))
(write-char #\) *sql-stream*)
(when (and (eq :mysql (database-underlying-type database))
(do* ((i 0 (incf i))
(j 1 (incf j)))
((= i len) (subseq buf 0 (1+ j)))
(declare (type fixnum i j))
(let ((char (aref str i)))
(do* ((i 0 (incf i))
(j 1 (incf j)))
((= i len) (subseq buf 0 (1+ j)))
(declare (type fixnum i j))
(let ((char (aref str i)))
(let ((keyword-package (symbol-package :foo)))
(defmethod database-output-sql ((sym symbol) database)
(let ((keyword-package (symbol-package :foo)))
(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 (null sym)
+ +null-string+
+ (if (equal (symbol-package sym) keyword-package)
+ (concatenate 'string "'" (string sym) "'")
+ (symbol-name sym)))))
+ (if database
+ (let ((val (database-output-sql-as-type 'boolean t database (database-type database))))
+ (when val
+ (typecase val
+ (string (format nil "'~A'" val))
+ (integer (format nil "~A" val)))))
+ "'Y'"))
+
+#+nil(defmethod database-output-sql ((tee (eql t)) database)
(declare (ignore database))
"'Y'")
(defmethod database-output-sql ((num number) database)
(declare (ignore database))
(declare (ignore database))
"'Y'")
(defmethod database-output-sql ((num number) database)
(declare (ignore database))
(format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
(sql-output val database))
arg))))
(defmethod database-output-sql ((arg vector) database)
(format nil "~{~A~^,~}" (map 'list #'(lambda (val)
(format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
(sql-output val database))
arg))))
(defmethod database-output-sql ((arg vector) database)
(format nil "~{~A~^,~}" (map 'list #'(lambda (val)
(defmethod database-output-sql ((self duration) database)
(declare (ignore database))
(format nil "'~a'" (duration-timestring self)))
(defmethod database-output-sql ((self duration) database)
(declare (ignore database))
(format nil "'~a'" (duration-timestring self)))
(defmethod database-output-sql ((self money) database)
(database-output-sql (slot-value self 'odcl::units) database))
(defmethod database-output-sql (thing database)
(if (or (null thing)
(defmethod database-output-sql ((self money) database)
(database-output-sql (slot-value self 'odcl::units) database))
(defmethod database-output-sql (thing database)
(if (or (null thing)
- (cons (symbol-name-default-case "NOT") "NOT")
- (cons (symbol-name-default-case "NULL") "NULL")
- (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
+ (cons (symbol-name-default-case "NOT") "NOT")
+ (cons (symbol-name-default-case "NULL") "NULL")
+ (cons (symbol-name-default-case "PRIMARY") "PRIMARY")
- (cons (symbol-name-default-case "UNSIGNED") "UNSIGNED")
- (cons (symbol-name-default-case "ZEROFILL") "ZEROFILL")
+ (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")))
(defmethod database-constraint-statement (constraint-list database)
(declare (ignore database))
(make-constraints-description constraint-list))
(cons (symbol-name-default-case "AUTO-INCREMENT") "AUTO_INCREMENT")
(cons (symbol-name-default-case "UNIQUE") "UNIQUE")))
(defmethod database-constraint-statement (constraint-list database)
(declare (ignore database))
(make-constraints-description constraint-list))
(setq string (concatenate 'string string (cdr output))))
(if (< 1 (length constraint))
(setq string (concatenate 'string string " "))))))))
(setq string (concatenate 'string string (cdr output))))
(if (< 1 (length constraint))
(setq string (concatenate 'string string " "))))))))