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
22 (defmethod meta-model.metadata
((self (eql nil
)))
25 (defun gen-supers (supers)
28 (when (member x
(list-meta-models))
32 (cons 'meta-model-class supers
))))
34 (defmethod %def-meta-model
((base-type t
) name supers slots
&rest options
)
35 `(defclass ,name
,(gen-supers supers
)
37 (:default-initargs
:metadata
',slots
:base-type
,base-type
)))
40 (defmacro def-meta-model
(name supers slots
&rest options
)
42 (when (not (member (quote ,name
) *meta-models
*))
43 (setf *meta-models
* (cons (quote ,name
) *meta-models
*)))
45 (let ((class ,(%def-meta-model
(cadr (or (assoc :base-type options
) '(t t
))) name supers slots options
)))
48 (defgeneric def-base-type-class-expander
(base-type model name args
))
50 (defmethod def-base-class-expander ((model t
) name args
)
51 (def-base-type-class-expander (meta-model.base-type model
) model name args
))
53 (defmacro def-base-class
(name (model) &rest args
)
54 (let ((i (make-instance model
)))
56 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
57 ,(def-base-class-expander i
:clsql name args
))
58 (defmethod meta-model.metadata
((m ,name
))
59 ',(meta-model.metadata i
)))))
62 (defmethod base-class-name ((model t
))
63 (class-name (class-of (meta-model.instance model
))))
67 (defmethod view-class-metadata ((model t
))
68 (meta-model.metadata model
))
70 (defun list-item-helper (type view
&key
(ret-fun #'car
))
71 "A helper function for the LIST-* methods"
73 (mapcar #'(lambda (slot)
74 (let ((ret-val (funcall ret-fun slot
))
75 (kind (getf (cdr slot
) :db-kind
)))
78 (view-class-metadata view
))))
80 (defmethod list-join-attributes ((view t
))
81 "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"
82 (remove nil
(mapcar #'(lambda (def)(cons (car def
) (getf (cdr def
) :db-info
))) (view-class-metadata view
))))
84 (defun list-relations-helper (view predicate-method
&key
(test-key :home-key
) (return-key :join-class
) (return-full nil
))
85 "A helper function for the listing join, relations and the like"
86 (remove nil
(mapcar #'(lambda (x)
87 (when (funcall predicate-method view
(getf (cdr x
) test-key
))
90 (getf (cdr x
) return-key
))))
91 (list-join-attributes view
))))
93 (defmethod list-slots ((view t
))
94 "list the non-joined slots of VIEW-CLASS"
95 (remove-if #'(lambda (x) (find x
(list-joins view
)))
96 (append (list-item-helper :key view
)
97 (list-item-helper nil view
)
98 (list-item-helper :base view
))))
100 (defmethod list-slot-types ((view t
))
101 "Returns an alist of (slot-name slot-type) where SLOT-TYPE is the CLSQL type"
102 (labels ((rfun (slot)
104 (list (getf (cdr slot
):type
))))
109 (append (lister :key
) (lister :base
))))
111 (defmethod slot-type ((view t
) slot
)
112 "returns the CLSQL type of SLOT"
113 (second (assoc slot
(list-slot-types view
))))
115 (defmethod list-joins ((view t
))
116 "lists slots that represent joins"
117 (list-item-helper :join view
))
119 (defmethod list-keys ((view t
))
120 "lists slots marked as :key"
121 (list-item-helper :key view
))
123 (defmethod primary-key-p ((view t
) slot
)
124 "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"
125 (find slot
(list-keys view
)))
127 (defmethod list-foreign-keys ((view t
))
128 "returns a list of FOREIGN-KEYS"
129 (flet ((my-primary-key-p (slot)
130 (primary-key-p view slot
)))
131 (remove nil
(remove-if #'my-primary-key-p
132 (mapcar #'(lambda (def)
133 (getf (cdr def
) :home-key
))
134 (list-join-attributes view
))))))
136 (defmethod foreign-key-p ((view t
) slot
)
137 "returns SLOT if it's a foreign key, nil otherwise"
138 (find slot
(list-foreign-keys view
)))
142 (defmethod list-has-a ((view t
))
143 "returns a list of view-classes that are in a 1:1 relationship with VIEW"
144 (list-relations-helper view
#'foreign-key-p
))
146 (defmethod list-has-many ((view t
))
147 "returns a list of view-classes that are in a 1:Many relationship with VIEW"
149 (remove-if #'(lambda (x) (getf (cdr x
) :target-slot
))
150 (list-relations-helper
152 #'primary-key-p
:return-full t
))))
154 (defmethod list-many-to-many ((view t
))
155 "returns a list of view-classes that are in a Many:Many relationship with VIEW"
156 (mapcar #'car
(list-relations-helper
159 (declare (ignore c
))a
)
160 :test-key
:target-slot
163 (defmethod explode-foreign-key ((model clsql
:standard-db-object
) slot
)
164 "returns the clsql view-class joined on SLOT"
165 (dolist (s (list-join-attributes model
))
166 (when (equal (getf (cdr s
) :home-key
) slot
)
167 (let ((val (slot-value model
(car s
))))
168 (return-from explode-foreign-key
169 (values (if val val
(make-instance (getf (cdr s
) :join-class
)))
170 (getf (cdr s
) :foreign-key
)))))))
172 (defun find-join-helper (foreign-key)
174 (declare (ignore class
))
175 (when (equal slot foreign-key
) t
)))
177 (defmethod find-join-class ((view t
) foreign-key
)
178 "Returns the VIEW-CLASS that is joined to VIEW via FOREGN-KEY"
179 (car (list-relations-helper view
(find-join-helper foreign-key
) )))
181 (defmethod find-join-key ((view t
) foreign-key
)
182 "returns the SLOT in the foreign VIEW-CLASS that joins with FOREIGN-KEY"
183 (car (list-relations-helper view
(find-join-helper foreign-key
) :return-key
:foreign-key
)))
185 (defmethod explode-has-many ((view t
) join-slot
)
186 "returns the class of the join as the primary value, the second and third value is the home key and the foreign key"
187 (let ((att (assoc join-slot
(list-join-attributes view
))))
188 (values (getf (cdr att
) :join-class
)
189 (getf (cdr att
) :home-key
)
190 (getf (cdr att
) :foreign-key
))))
192 (defgeneric expr-
= (instance slot-name value
)
193 (:documentation
"Create search expression for appropriate backend."))
195 (defgeneric expr-
> (instance slot-name value
)
196 (:documentation
"Create search expression for appropriate backend."))
198 (defgeneric expr-
< (instance slot-name value
)
199 (:documentation
"Create search expression for appropriate backend."))
201 (defgeneric expr-ends-with
(instance slot-name value
)
202 (:documentation
"Create search expression for appropriate backend."))
204 (defgeneric expr-starts-with
(instance slot-name value
)
205 (:documentation
"Create search expression for appropriate backend."))
207 (defgeneric expr-contains
(instance slot-name value
)
208 (:documentation
"Create search expression for appropriate backend."))
210 (defgeneric expr-and
(instance &rest args
)
211 (:documentation
"Create search expression for appropriate backend."))
213 (defgeneric expr-or
(instance &rest args
)
214 (:documentation
"Create search expression for appropriate backend."))
216 (defgeneric expr-not
(instance &rest args
)
217 (:documentation
"Create search expression for appropriate backend."))
219 (defgeneric select-instances
(instance &rest args
)
220 (:documentation
"Select instances in backend dependent way"))
222 (defmacro def-compare-expr
(instance-type name expr
&key value-format
)
223 `(defmethod ,name
((instance ,instance-type
) slot-name value
)
224 (declare (ignore instance
))
225 (,expr slot-name
,(typecase value-format
227 (string `(format nil
,value-format value
))
228 (t `(,value-format value
))))))
230 (defmacro def-logical-expr
(instance-type name expr
)
231 `(defmethod ,name
((instance ,instance-type
) &rest args
)
232 (declare (ignore instance
))