made select accept a parameters argument. If parameters are supplied, it builds
[clsql.git] / sql / oodml.lisp
index 30f6b0dfd82ea20aaab3526dcca657a8740f0a08..069275cefd0c9eea006ca9e8dffcfe155dabbb52 100644 (file)
                                    (db-value-from-slot slot val database))))
                        slots)))
     (cond ((and avps (view-database obj))
-           (update-records (sql-expression :table vct)
-                           :av-pairs avps
-                           :where (key-qualifier-for-instance
-                                   obj :database database)
-                           :database database))
+          (let ((where (key-qualifier-for-instance
+                        obj :database database)))
+            (unless where
+              (error "update-record-from-slots: Could not generate a where clause for this object: ~a" obj))
+            (update-records (sql-expression :table vct)
+                            :av-pairs avps
+                            :where where
+                            :database database)))
           ((and avps (not (view-database obj)))
            (insert-records :into (sql-expression :table vct)
                            :av-pairs avps
                                            :result-types nil
                                            :database database)))))
     (when res
+      (setf (slot-value instance 'view-database) database)
       (get-slot-values-from-view instance (mapcar #'car sels) (car res)))))
 
 (defmethod update-slot-from-record ((instance standard-db-object)
     (format nil "~F" val)))
 
 (defmethod read-sql-value (val type database db-type)
-  (declare (ignore type database db-type))
-  (read-from-string val))
+  (declare (ignore database db-type))
+  (cond
+    ((null type) val) ;;we have no desired type, just give the value
+    ((typep val type) val) ;;check that it hasn't already been converted.
+    ((typep val 'string) (read-from-string val)) ;;maybe read will just take care of it?
+    (T (error "Unable to read-sql-value ~a as type ~a" val type))))
 
 (defmethod read-sql-value (val (type (eql 'string)) database db-type)
   (declare (ignore database db-type))
@@ -866,21 +874,23 @@ maximum of MAX-LEN instances updated in each query."
           (car objects)
         objects))))
 
+(defmethod select-table-sql-expr ((table T))
+  "Turns an object representing a table into the :from part of the sql expression that will be executed "
+  (sql-expression :table (view-table table)))
+
 (defun find-all (view-classes
                  &rest args
                  &key all set-operation distinct from where group-by having
                       order-by offset limit refresh flatp result-types
                       inner-join on
                       (database *default-database*)
-                      instances)
+                      instances parameters)
   "Called by SELECT to generate object query results when the
   View Classes VIEW-CLASSES are passed as arguments to SELECT."
   (declare (ignore all set-operation group-by having offset limit inner-join on))
   (flet ((ref-equal (ref1 ref2)
            (string= (sql-output ref1 database)
                     (sql-output ref2 database)))
-         (table-sql-expr (table)
-           (sql-expression :table (view-table table)))
          (tables-equal (table-a table-b)
            (when (and table-a table-b)
              (string= (string (slot-value table-a 'name))
@@ -906,15 +916,15 @@ maximum of MAX-LEN instances updated in each query."
            (fullsels (apply #'append (mapcar #'append sels immediate-join-sels)))
            (sel-tables (collect-table-refs where))
            (tables (remove-if #'null
-                              (remove-duplicates
-                               (append (mapcar #'table-sql-expr sclasses)
-                                       (mapcan #'(lambda (jc-list)
-                                                   (mapcar
-                                                    #'(lambda (jc) (when jc (table-sql-expr jc)))
-                                                    jc-list))
-                                               immediate-join-classes)
-                                       sel-tables)
-                               :test #'tables-equal)))
+                             (remove-duplicates
+                              (append (mapcar #'select-table-sql-expr sclasses)
+                                      (mapcan #'(lambda (jc-list)
+                                                  (mapcar
+                                                   #'(lambda (jc) (when jc (select-table-sql-expr jc)))
+                                                   jc-list))
+                                              immediate-join-classes)
+                                      sel-tables)
+                              :test #'tables-equal)))
            (order-by-slots (mapcar #'(lambda (ob) (if (atom ob) ob (car ob)))
                                    (listify order-by)))
            (join-where nil))
@@ -1102,6 +1112,7 @@ as elements of a list."
                       results))))))))
          (t
           (let* ((expr (apply #'make-query select-all-args))
+                (parameters (second (member :parameters select-all-args)))
                  (specified-types
                   (mapcar #'(lambda (attrib)
                               (if (typep attrib 'sql-ident-attribute)
@@ -1111,12 +1122,17 @@ as elements of a list."
                                         t))
                                   t))
                           (slot-value expr 'selections))))
+           
             (destructuring-bind (&key (flatp nil)
-                                      (result-types :auto)
-                                      (field-names t)
-                                      (database *default-database*)
-                                      &allow-other-keys)
+                                (result-types :auto)
+                                (field-names t)
+                                (database *default-database*)
+                                &allow-other-keys)
                 qualifier-args
+             (when parameters
+               (setf expr (make-instance 'command-object
+                                         :expression (sql-output expr database)
+                                         :parameters parameters)))
               (query expr :flatp flatp
                      :result-types
                      ;; specifying a type for an attribute overrides result-types