r9259: fix typo in var name
[clsql.git] / sql / classes.lisp
index df84cd4b8d5ff9dad158ecb7c1d03c7404839515..9e2338cb9731bfd292713afcb4bb38f8d1727299 100644 (file)
@@ -13,7 +13,7 @@
 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
 ;;;; *************************************************************************
 
-(in-package #:clsql-sys)
+(in-package #:clsql)
 
 (defvar +empty-string+ "''")
 
     (if (and (not qualifier) (not type))
        (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
+      ;;; should not be output in SQL statements
+      #+ignore
       (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
              (when qualifier
-                 (convert-to-db-default-case (sql-escape qualifier) database))
+               (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))))
+               (convert-to-db-default-case (symbol-name type) database)))
+      (format *sql-stream* "~@[~A.~]~A"
+             (when qualifier
+               (convert-to-db-default-case (sql-escape qualifier) database))
+             (sql-escape (convert-to-db-default-case name database))))
     t))
 
 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
     :initform nil)
    (order-by-descending
     :initarg :order-by-descending
+    :initform nil)
+   (inner-join
+    :initarg :inner-join
+    :initform nil)
+   (on
+    :initarg :on
     :initform nil))
   (:documentation "An SQL SELECT query."))
 
+(defclass sql-object-query (%sql-expression)
+  ((objects
+    :initarg :objects
+    :initform nil)
+   (flatp
+    :initarg :flatp
+    :initform nil)
+   (exp
+    :initarg :exp
+    :initform nil)
+   (refresh
+    :initarg :refresh
+    :initform nil)))
+
 (defmethod collect-table-refs ((sql sql-query))
   (remove-duplicates (collect-table-refs (slot-value sql 'where))
                      :test (lambda (tab1 tab2)
 
 (defvar *select-arguments*
   '(:all :database :distinct :flatp :from :group-by :having :order-by
-    :order-by-descending :set-operation :where :offset :limit))
+    :order-by-descending :set-operation :where :offset :limit
+    :inner-join :on))
 
 (defun query-arg-p (sym)
   (member sym *select-arguments*))
@@ -436,28 +464,40 @@ uninclusive, and the args from that keyword to the end."
         select-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
-                              group-by having order-by order-by-descending
-                              offset limit &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
-                     :order-by-descending order-by-descending))))
+  (flet ((select-objects (target-args)
+           (and target-args
+                (every #'(lambda (arg)
+                           (and (symbolp arg)
+                                (find-class arg nil)))
+                       target-args))))
+    (multiple-value-bind (selections arglist)
+       (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 order-by-descending
+                                   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
+                          :order-by-descending order-by-descending
+                          :inner-join inner-join :on on))))))
 
 (defvar *in-subselect* nil)
 
 (defmethod output-sql ((query sql-query) database)
   (with-slots (distinct selections from where group-by having order-by
-                        order-by-descending limit offset)
+                        order-by-descending limit offset inner-join on)
       query
     (when *in-subselect*
       (write-string "(" *sql-stream*))
@@ -469,10 +509,17 @@ uninclusive, and the args from that keyword to the end."
         (output-sql distinct database)
         (write-char #\Space *sql-stream*)))
     (output-sql (apply #'vector selections) database)
-    (write-string " FROM " *sql-stream*)
-    (if (listp from)
-        (output-sql (apply #'vector from) database)
-        (output-sql from database))
+    (when from
+      (write-string " FROM " *sql-stream*)
+      (if (listp from)
+         (output-sql (apply #'vector from) database)
+       (output-sql from database)))
+    (when inner-join
+      (write-string " INNER JOIN " *sql-stream*)
+      (output-sql inner-join database))
+    (when on
+      (write-string " ON " *sql-stream*)
+      (output-sql on database))
     (when where
       (write-string " WHERE " *sql-stream*)
       (let ((*in-subselect* t))
@@ -512,6 +559,13 @@ uninclusive, and the args from that keyword to the end."
       (write-string ")" *sql-stream*)))
   t)
 
+(defmethod output-sql ((query sql-object-query) database)
+  (with-slots (objects)
+      query
+    (when objects
+      (format *sql-stream* "(~{~A~^ ~})" objects))))
+
+
 ;; INSERT
 
 (defclass sql-insert (%sql-expression)