1 (in-package :meta-model
)
3 (defvar *meta-models
* nil
)
5 (defun list-meta-models ()
8 (defclass meta-model-class
()
10 :accessor meta-model.metadata
14 :accessor meta-model.instance
18 :accessor meta-model.base-type
21 (defmethod meta-model.metadata
((self (eql nil
)))
24 (defun gen-supers (supers)
27 (when (member x
(list-meta-models))
31 (cons 'meta-model-class supers
))))
33 (defmethod %def-meta-model
((base-type t
) name supers slots
&rest options
)
34 `(defclass ,name
,(gen-supers supers
)
36 (:default-initargs
:metadata
',slots
)))
39 (defmacro def-meta-model
(name supers slots
&rest options
)
41 (when (not (member (quote ,name
) *meta-models
*))
42 (setf *meta-models
* (cons (quote ,name
) *meta-models
*)))
44 (let ((class ,(%def-meta-model
(cadr (or (assoc :base-type options
) '(t t
))) name supers slots options
)))
47 (defgeneric def-base-class-expander
(model base-type name args
))
50 (defmacro def-base-class
(name (model) &rest args
)
51 (let ((i (make-instance model
)))
53 ,(def-base-class-expander i
:clsql name args
)
54 (defmethod meta-model.metadata
((m ,name
))
55 ',(meta-model.metadata i
)))))
58 (defmethod base-class-name ((model t
))
59 (class-name (class-of (meta-model.instance model
))))
63 (defmethod view-class-metadata ((model t
))
64 (meta-model.metadata model
))
66 (defun list-item-helper (type view
&key
(ret-fun #'car
))
67 "A helper function for the LIST-* methods"
69 (mapcar #'(lambda (slot)
70 (let ((ret-val (funcall ret-fun slot
))
71 (kind (getf (cdr slot
) :db-kind
)))
74 (view-class-metadata view
))))
76 (defmethod list-join-attributes ((view t
))
77 "Returns all slots as an alist of (SLOT-NAME JOIN-ATTRIBUTES) where J-A is the contents of the :JOIN-CLASS portion of a slot definition"
78 (remove nil
(mapcar #'(lambda (def)(cons (car def
) (getf (cdr def
) :db-info
))) (view-class-metadata view
))))
80 (defun list-relations-helper (view predicate-method
&key
(test-key :home-key
) (return-key :join-class
) (return-full nil
))
81 "A helper function for the listing join, relations and the like"
82 (remove nil
(mapcar #'(lambda (x)
83 (when (funcall predicate-method view
(getf (cdr x
) test-key
))
86 (getf (cdr x
) return-key
))))
87 (list-join-attributes view
))))
89 (defmethod list-slots ((view t
))
90 "list the non-joined slots of VIEW-CLASS"
91 (remove-if #'(lambda (x) (find x
(list-joins view
)))
92 (append (list-item-helper :key view
)
93 (list-item-helper nil view
)
94 (list-item-helper :base view
))))
96 (defmethod list-slot-types ((view t
))
97 "Returns an alist of (slot-name slot-type) where SLOT-TYPE is the CLSQL type"
100 (list (getf (cdr slot
):type
))))
105 (append (lister :key
) (lister :base
))))
107 (defmethod slot-type ((view t
) slot
)
108 "returns the CLSQL type of SLOT"
109 (second (assoc slot
(list-slot-types view
))))
111 (defmethod list-joins ((view t
))
112 "lists slots that represent joins"
113 (list-item-helper :join view
))
115 (defmethod list-keys ((view t
))
116 "lists slots marked as :key"
117 (list-item-helper :key view
))
119 (defmethod primary-key-p ((view t
) slot
)
120 "returns slot if it is primary key (NOTE: Currently this returns T if the slot appears in LIST_KEYS and does not take into account the :primary-key option. b0rked, to be sure"
121 (find slot
(list-keys view
)))
123 (defmethod list-foreign-keys ((view t
))
124 "returns a list of FOREIGN-KEYS"
125 (flet ((my-primary-key-p (slot)
126 (primary-key-p view slot
)))
127 (remove nil
(remove-if #'my-primary-key-p
128 (mapcar #'(lambda (def)
129 (getf (cdr def
) :home-key
))
130 (list-join-attributes view
))))))
132 (defmethod foreign-key-p ((view t
) slot
)
133 "returns SLOT if it's a foreign key, nil otherwise"
134 (find slot
(list-foreign-keys view
)))
138 (defmethod list-has-a ((view t
))
139 "returns a list of view-classes that are in a 1:1 relationship with VIEW"
140 (list-relations-helper view
#'foreign-key-p
))
142 (defmethod list-has-many ((view t
))
143 "returns a list of view-classes that are in a 1:Many relationship with VIEW"
145 (remove-if #'(lambda (x) (getf (cdr x
) :target-slot
))
146 (list-relations-helper
148 #'primary-key-p
:return-full t
))))
150 (defmethod list-many-to-many ((view t
))
151 "returns a list of view-classes that are in a Many:Many relationship with VIEW"
152 (mapcar #'car
(list-relations-helper
155 (declare (ignore c
))a
)
156 :test-key
:target-slot
159 (defmethod explode-foreign-key ((model clsql
:standard-db-object
) slot
)
160 "returns the clsql view-class joined on SLOT"
161 (dolist (s (list-join-attributes model
))
162 (when (equal (getf (cdr s
) :home-key
) slot
)
163 (return-from explode-foreign-key
(slot-value model
(car s
))))))
165 (defun find-join-helper (foreign-key)
167 (declare (ignore class
))
168 (when (equal slot foreign-key
) t
)))
170 (defmethod find-join-class ((view t
) foreign-key
)
171 "Returns the VIEW-CLASS that is joined to VIEW via FOREGN-KEY"
172 (car (list-relations-helper view
(find-join-helper foreign-key
) )))
174 (defmethod find-join-key ((view t
) foreign-key
)
175 "returns the SLOT in the foreign VIEW-CLASS that joins with FOREIGN-KEY"
176 (car (list-relations-helper view
(find-join-helper foreign-key
) :return-key
:foreign-key
)))