X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Foodml.lisp;h=9aff2c8dab99a36b930d09c103340b1125961abe;hb=814ef0649edf23f0136d5cad2d7738ae72e79871;hp=b5a5907176bb44646287d2eef2bb2663e64f64b3;hpb=968b580ab64c6ac20a5ddbd5a5b8eeec092d2e1f;p=clsql.git diff --git a/sql/oodml.lisp b/sql/oodml.lisp index b5a5907..9aff2c8 100644 --- a/sql/oodml.lisp +++ b/sql/oodml.lisp @@ -95,9 +95,9 @@ (cond ((and value (null slot-reader)) (setf (slot-value instance slot-name) (read-sql-value value (delistify slot-type) - (view-database instance) + (choose-database-for-instance instance) (database-underlying-type - (view-database instance))))) + (choose-database-for-instance instance))))) ((null value) (update-slot-with-null instance slot-name slotdef)) ((typep slot-reader 'string) @@ -158,7 +158,7 @@ (mapc #'update-slot slotdeflist values) obj)) -(defmethod choose-database-for-instance ((obj standard-db-object) database) +(defmethod choose-database-for-instance ((obj standard-db-object) &optional database) "Determine which database connection to use for a standard-db-object. Errs if none is available." (or (find-if #'(lambda (db) @@ -189,11 +189,14 @@ (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 @@ -235,6 +238,7 @@ :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) @@ -469,8 +473,12 @@ (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)) @@ -625,10 +633,10 @@ :table jc-view-table)) :where jq :result-types :auto - :database (view-database object)))) + :database (choose-database-for-instance object)))) (mapcar #'(lambda (i) (let* ((instance (car i)) - (jcc (make-instance jc :view-database (view-database instance)))) + (jcc (make-instance jc :view-database (choose-database-for-instance object)))) (setf (slot-value jcc (gethash :foreign-key dbi)) key) (setf (slot-value jcc (gethash :home-key tdbi)) @@ -639,8 +647,8 @@ ;; just fill in minimal slots (mapcar #'(lambda (k) - (let ((instance (make-instance tsc :view-database (view-database object))) - (jcc (make-instance jc :view-database (view-database object))) + (let ((instance (make-instance tsc :view-database (choose-database-for-instance object))) + (jcc (make-instance jc :view-database (choose-database-for-instance object))) (fk (car k))) (setf (slot-value instance (gethash :home-key tdbi)) fk) (setf (slot-value jcc (gethash :foreign-key dbi)) @@ -651,7 +659,7 @@ (select (sql-expression :attribute (gethash :foreign-key tdbi) :table jc-view-table) :from (sql-expression :table jc-view-table) :where jq - :database (view-database object)))))))) + :database (choose-database-for-instance object)))))))) ;;; Remote Joins @@ -749,7 +757,7 @@ maximum of MAX-LEN instances updated in each query." (let ((jq (join-qualifier class object slot-def))) (when jq (select jc :where jq :flatp t :result-types nil - :database (view-database object)))))) + :database (choose-database-for-instance object)))))) (defun fault-join-slot (class object slot-def) (let* ((dbi (view-class-slot-db-info slot-def)) @@ -866,6 +874,10 @@ 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 @@ -879,8 +891,6 @@ maximum of MAX-LEN instances updated in each query." (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))