Did some work on searching, changed object presentation to specialise on each row...
[clinton/lisp-on-lines.git] / src / meta-model.lisp
index b0d1ff5..4416423 100644 (file)
     :initform nil)
    (base-type
     :accessor meta-model.base-type
-    :initform 'clsql)))
+    :initarg :base-type
+    :initform :clsql)))
 
 (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)
        (cons 'meta-model-class supers))))
 
 (defmethod %def-meta-model ((base-type t) name supers slots &rest options)
-  `(defclass ,name ,(gen-supers supers)
-     ()
-     (:default-initargs :metadata ',slots)))
+  `(eval-when (:compile-toplevel :load-toplevel :execute)
+    (defclass ,name ,(gen-supers supers)
+      ()
+      (:default-initargs :metadata ',slots :base-type ,base-type))))
   
   
 (defmacro def-meta-model (name supers slots &rest options)
-  `(progn
+  `(eval-when (:compile-toplevel :load-toplevel :execute)
      (when (not (member (quote ,name) *meta-models*))
        (setf *meta-models* (cons (quote ,name) *meta-models*)))
 
      (let ((class ,(%def-meta-model (cadr (or (assoc :base-type options) '(t t))) name supers slots options)))
        class)))
 
-(defgeneric def-base-class-expander (model base-type name args))
-
+(defgeneric def-base-type-class-expander (base-type model name args))
 
-(defmacro def-base-class (name (model) &rest args)
-  (let ((i (make-instance model)))
-    `(progn 
-       ,(def-base-class-expander i :clsql name args)
-       (defmethod meta-model.metadata ((m ,name))
-        ',(meta-model.metadata i)))))
-  
+(defmethod def-base-class-expander ((model t) name args)
+  (def-base-type-class-expander (meta-model.base-type model) model name args))
 
 (defmethod base-class-name ((model t))
   (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"
   "returns the clsql view-class joined on SLOT"
   (dolist (s (list-join-attributes model))
     (when (equal (getf (cdr s) :home-key) slot)
-      (return-from explode-foreign-key (slot-value model (car s))))))
+      (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)))))))
 
 (defun find-join-helper (foreign-key)
   (lambda (class slot) 
   "returns the SLOT in the foreign VIEW-CLASS that joins with FOREIGN-KEY"
   (car (list-relations-helper view (find-join-helper foreign-key) :return-key :foreign-key)))
 
+(defmethod explode-has-many ((view t) join-slot)
+  "returns the class of the join as the primary value, the second and third value is the home key and the foreign key"
+  (let ((att (assoc join-slot (list-join-attributes view))))
+    (values (getf (cdr att) :join-class) 
+           (getf (cdr att) :home-key) 
+           (getf (cdr att) :foreign-key))))
+  
+(defgeneric expr-= (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-> (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-< (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-ends-with (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-starts-with (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-contains (instance slot-name value)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-and (instance &rest args)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-or (instance &rest args)
+  (:documentation "Create search expression for appropriate backend."))
+
+(defgeneric expr-not (instance &rest args)
+  (:documentation "Create search expression for appropriate backend."))
+
+(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 (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))
+     (apply ,expr args)))
\ No newline at end of file