added lisp-on-lines.lisp
[clinton/lisp-on-lines.git] / src / meta-model.lisp
index 8705a68..09f7f6f 100644 (file)
@@ -22,6 +22,9 @@
 (defmethod meta-model.metadata ((self (eql nil)))
   nil)
 
+(defmethod meta-model.metadata ((self symbol))
+  (meta-model.metadata (make-instance self)))
+
 (defun gen-supers (supers)
   (let (subclassp)
     (dolist (x supers)
@@ -55,6 +58,9 @@
   (class-name (class-of (meta-model.instance model))))
 
 (defmethod view-class-metadata ((model t))
+  "
+This is what meta-model.metadata used to be called,
+most of the below functions expect this method to exist"
   (meta-model.metadata model))
 
 (defun list-item-helper (type view &key (ret-fun #'car))
              (list-item-helper
               type view
               :ret-fun #'rfun)))
-    (append (lister :key) (lister :base))))
+    (append (lister :key) (lister :base) (lister nil))))
                         
 (defmethod slot-type ((view t) slot)
   "returns the CLSQL type of SLOT"
                 :test-key :target-slot
                 :return-full t)))
 
-(defmethod explode-foreign-key ((model clsql:standard-db-object) slot)
+(defmethod explode-foreign-key ((model clsql:standard-db-object) slot &key (createp t))
   "returns the clsql view-class joined on SLOT"
   (dolist (s (list-join-attributes model))
     (when (equal (getf (cdr s) :home-key) slot)
-      (let ((val (slot-value model (car s))))
-      (return-from explode-foreign-key 
-       (values (if val val (make-instance (getf (cdr s) :join-class))) 
-               (getf (cdr s) :foreign-key)))))))
+      (let* ((fkey (getf (cdr s) :foreign-key))
+            (new (sync-instance (make-instance (getf (cdr s) :join-class))))
+            (val (or (slot-value model (car s))
+                     (progn
+                       (when createp
+                         (setf
+                          (slot-value model slot)
+                          (slot-value new fkey))
+                         (sync-instance model)
+                         (slot-value model (car s)))))))
+           
+       (return-from explode-foreign-key 
+         (values val fkey))))))
 
 (defun find-join-helper (foreign-key)
   (lambda (class slot) 
 (defgeneric select-instances (instance &rest args)
   (:documentation "Select instances in backend dependent way"))
 
+(defgeneric prepare-slot-name-for-select (instance slot-name)
+  (:method (i s) s))
+
 (defmacro def-compare-expr (instance-type name expr &key value-format)
   `(defmethod ,name ((instance ,instance-type) slot-name value)
      (declare (ignore instance))
-     (,expr slot-name ,(typecase value-format
+     (,expr (prepare-slot-name-for-select instance slot-name) ,(typecase value-format
                                  (null 'value)
                                  (string `(format nil ,value-format value))
                                  (t `(,value-format value))))))
 
+
 (defmacro def-logical-expr (instance-type name expr)
   `(defmethod ,name ((instance ,instance-type) &rest args)
      (declare (ignore instance))