;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
-;;;; ======================================================================
-;;;; $Id: $
-;;;; ======================================================================
+;;;; *************************************************************************
;;;;
-;;;; Description ==========================================================
-;;;; ======================================================================
+;;;; $Id$
;;;;
;;;; Classes defining SQL expressions and methods for formatting the
;;;; appropriate SQL commands.
;;;;
-;;;; ======================================================================
+;;;; This file is part of CLSQL.
+;;;;
+;;;; CLSQL users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the Lisp Lesser GNU Public License
+;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
+;;;; *************************************************************************
(in-package #:clsql-sys)
-
(defvar +empty-string+ "''")
(defvar +null-string+ "NULL")
(defvar *sql-stream* nil
"stream which accumulates SQL output")
-(defvar *default-schema* "UNCOMMONSQL")
-
-(defvar *object-schemas* (make-hash-table :test #'equal)
- "Hash of schema name to class constituent lists.")
-
-(defun in-schema (schemaname)
- (setf *default-schema* schemaname))
-
(defun sql-output (sql-expr &optional database)
(progv '(*sql-stream*)
`(,(make-string-output-stream))
(defclass %sql-expression ()
())
-(defmethod output-sql ((expr %sql-expression) &optional
- (database *default-database*))
+(defmethod output-sql ((expr %sql-expression) database)
(declare (ignore database))
(write-string +null-string+ *sql-stream*))
sql
`(make-instance 'sql :string ',text)))
-(defmethod output-sql ((expr sql) &optional (database *default-database*))
+(defmethod output-sql ((expr sql) database)
(declare (ignore database))
(write-string (slot-value expr 'text) *sql-stream*)
t)
(defmethod print-object ((ident sql) stream)
(format stream "#<~S \"~A\">"
(type-of ident)
- (sql-output ident)))
+ (sql-output ident nil)))
;; For SQL Identifiers of generic type
(defclass sql-ident (%sql-expression)
(defvar *output-hash* (make-hash-table :test #'equal))
-(defmethod output-sql-hash-key (expr &optional (database *default-database*))
+(defmethod output-sql-hash-key (expr database)
(declare (ignore expr database))
nil)
-(defmethod output-sql :around ((sql t) &optional (database *default-database*))
- (declare (ignore database))
- (let* ((hash-key (output-sql-hash-key sql))
+#+ignore
+(defmethod output-sql :around ((sql t) database)
+ (let* ((hash-key (output-sql-hash-key sql database))
(hash-value (when hash-key (gethash hash-key *output-hash*))))
(cond ((and hash-key hash-value)
(write-string hash-value *sql-stream*))
(t
(call-next-method)))))
-(defmethod output-sql ((expr sql-ident) &optional
- (database *default-database*))
- (declare (ignore database))
+(defmethod output-sql ((expr sql-ident) database)
(with-slots (name)
- expr
- (etypecase name
- (string
- (write-string name *sql-stream*))
- (symbol
- (write-string (symbol-name name) *sql-stream*)))
- t))
+ expr
+ (write-string
+ (convert-to-db-default-case
+ (etypecase name
+ (string name)
+ (symbol (symbol-name name)))
+ database)
+ *sql-stream*))
+ t)
;; For SQL Identifiers for attributes
:qualifier ',qualifier
:type ',type)))
-(defmethod output-sql ((expr sql-ident-attribute) &optional
- (database *default-database*))
- (declare (ignore database))
+(defmethod output-sql ((expr sql-ident-attribute) database)
(with-slots (qualifier name type params)
- expr
- (if (and name (not qualifier) (not type))
- (write-string (sql-escape (symbol-name name)) *sql-stream*)
- (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
- (if qualifier (sql-escape qualifier) qualifier)
- (sql-escape name)
- type))
+ expr
+ (if (and (not qualifier) (not type))
+ (write-string (sql-escape (convert-to-db-default-case
+ (symbol-name name) database)) *sql-stream*)
+ (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
+ (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))))
t))
-(defmethod output-sql-hash-key ((expr sql-ident-attribute) &optional
- (database *default-database*))
+(defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
(declare (ignore database))
(with-slots (qualifier name type params)
expr
(declare (ignore environment))
(with-slots (alias name)
sql
- `(make-instance 'sql-ident-table :name name :alias ',alias)))
+ `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
-(defun generate-sql (expr)
+(defun generate-sql (expr database)
(let ((*sql-stream* (make-string-output-stream)))
- (output-sql expr)
+ (output-sql expr database)
(get-output-stream-string *sql-stream*)))
-(defmethod output-sql ((expr sql-ident-table) &optional
- (database *default-database*))
- (declare (ignore database))
+(defmethod output-sql ((expr sql-ident-table) database)
(with-slots (name alias)
expr
(if (null alias)
- (write-string (sql-escape (symbol-name name)) *sql-stream*)
+ (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
(progn
- (write-string (sql-escape (symbol-name name)) *sql-stream*)
+ (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))))
t)
-(defmethod output-sql-hash-key ((expr sql-ident-table) &optional
- (database *default-database*))
+#|
+(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 output-sql-hash-key ((expr sql-ident-table) database)
(declare (ignore database))
(with-slots (name alias)
expr
;; Write SQL for relational operators (like 'AND' and 'OR').
;; should do arity checking of subexpressions
-(defmethod output-sql ((expr sql-relational-exp) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-relational-exp) database)
(with-slots (operator sub-expressions)
expr
(let ((subs (if (consp (car sub-expressions))
;; Write SQL for relational operators (like 'AND' and 'OR').
;; should do arity checking of subexpressions
-(defmethod output-sql ((expr sql-upcase-like) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-upcase-like) database)
(flet ((write-term (term)
(write-string "upper(" *sql-stream*)
(output-sql term database)
(:documentation "An SQL Assignment expression."))
-(defmethod output-sql ((expr sql-assignment-exp) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-assignment-exp) database)
(with-slots (operator sub-expressions)
expr
(do ((sub sub-expressions (cdr sub)))
-(defmethod output-sql ((expr sql-value-exp) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-value-exp) database)
(with-slots (modifier components)
expr
(if modifier
()
(:documentation "An SQL typecast expression."))
-(defmethod output-sql ((expr sql-typecast-exp) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-typecast-exp) database)
(database-output-sql expr database))
(defmethod database-output-sql ((expr sql-typecast-exp) database)
(equal (slot-value tab1 'name)
(slot-value tab2 'name))))))
-(defmethod output-sql ((expr sql-function-exp) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-function-exp) database)
(with-slots (name args)
expr
(output-sql name database)
(subseq select-args first-key-arg))
select-args)))
-(defmethod make-query (&rest args)
+(defun make-query (&rest args)
(multiple-value-bind (selections arglist)
(query-get-selections args)
(destructuring-bind (&key all flatp set-operation distinct from where
(defvar *in-subselect* nil)
-(defmethod output-sql ((query sql-query) &optional
- (database *default-database*))
+(defmethod output-sql ((query sql-query) database)
(with-slots (distinct selections from where group-by having order-by
order-by-descending limit offset)
query
(:documentation
"An SQL INSERT statement."))
-(defmethod output-sql ((ins sql-insert) &optional
- (database *default-database*))
+(defmethod output-sql ((ins sql-insert) database)
(with-slots (into attributes values query)
ins
(write-string "INSERT INTO " *sql-stream*)
(:documentation
"An SQL DELETE statement."))
-(defmethod output-sql ((stmt sql-delete) &optional
- (database *default-database*))
+(defmethod output-sql ((stmt sql-delete) database)
(with-slots (from where)
stmt
(write-string "DELETE FROM " *sql-stream*)
:initform nil))
(:documentation "An SQL UPDATE statement."))
-(defmethod output-sql ((expr sql-update) &optional
- (database *default-database*))
+(defmethod output-sql ((expr sql-update) database)
(with-slots (table where attributes values)
expr
(flet ((update-assignments ()
:initform nil)
(modifiers
:initarg :modifiers
+ :initform nil)
+ (transactions
+ :initarg :transactions
:initform nil))
(:documentation
"An SQL CREATE TABLE statement."))
(list x)
x))
-(defmethod output-sql ((stmt sql-create-table) &optional
- (database *default-database*))
+(defmethod output-sql ((stmt sql-create-table) database)
(flet ((output-column (column-spec)
- (destructuring-bind (name type &rest constraints)
+ (destructuring-bind (name type &optional db-type &rest constraints)
column-spec
(let ((type (listify type)))
(output-sql name database)
(write-char #\Space *sql-stream*)
(write-string
- (database-get-type-specifier (car type) (cdr type) database)
+ (if (stringp db-type) db-type ; override definition
+ (database-get-type-specifier (car type) (cdr type) database))
*sql-stream*)
(let ((constraints
(database-constraint-statement constraints database)))
(when constraints
(write-string " " *sql-stream*)
(write-string constraints *sql-stream*)))))))
- (with-slots (name columns modifiers)
+ (with-slots (name columns modifiers transactions)
stmt
(write-string "CREATE TABLE " *sql-stream*)
(output-sql name database)
((null modifier))
(write-string ", " *sql-stream*)
(write-string (car modifier) *sql-stream*)))
- (write-char #\) *sql-stream*)))
+ (write-char #\) *sql-stream*)
+ (when (and (eq :mysql (database-underlying-type database))
+ transactions
+ (db-type-transaction-capable? :mysql database))
+ (write-string " Type=InnoDB" *sql-stream*))))
t)
(with-check-option :initarg :with-check-option :initform nil))
(:documentation "An SQL CREATE VIEW statement."))
-(defmethod output-sql ((stmt sql-create-view) &optional database)
+(defmethod output-sql ((stmt sql-create-view) database)
(with-slots (name column-list query with-check-option) stmt
(write-string "CREATE VIEW " *sql-stream*)
(output-sql name database)
;; Column constraint types
;;
(defparameter *constraint-types*
- '(("NOT-NULL" . "NOT NULL")
- ("PRIMARY-KEY" . "PRIMARY KEY")))
+ (list
+ (cons (symbol-name-default-case "NOT-NULL") "NOT NULL")
+ (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")))
;;
;; Convert type spec to sql syntax