docstring for view-class-metadata
[clinton/lisp-on-lines.git] / src / meta-model.lisp
CommitLineData
579597e3 1(in-package :meta-model)
2
3(defvar *meta-models* nil)
4
5(defun list-meta-models ()
6 *meta-models*)
7
8(defclass meta-model-class ()
9 ((metadata
10 :accessor meta-model.metadata
11 :initarg :metadata
12 :initform nil)
13 (instance
14 :accessor meta-model.instance
15 :initarg :instance
16 :initform nil)
17 (base-type
18 :accessor meta-model.base-type
9d6c69fb
DC
19 :initarg :base-type
20 :initform :clsql)))
579597e3 21
22(defmethod meta-model.metadata ((self (eql nil)))
23 nil)
24
25(defun gen-supers (supers)
26 (let (subclassp)
27 (dolist (x supers)
28 (when (member x (list-meta-models))
29 (setf subclassp t)))
30 (if subclassp
31 supers
32 (cons 'meta-model-class supers))))
33
34(defmethod %def-meta-model ((base-type t) name supers slots &rest options)
1da9fd46
DC
35 `(eval-when (:compile-toplevel :load-toplevel :execute)
36 (defclass ,name ,(gen-supers supers)
37 ()
38 (:default-initargs :metadata ',slots :base-type ,base-type))))
579597e3 39
40
41(defmacro def-meta-model (name supers slots &rest options)
1da9fd46 42 `(eval-when (:compile-toplevel :load-toplevel :execute)
579597e3 43 (when (not (member (quote ,name) *meta-models*))
44 (setf *meta-models* (cons (quote ,name) *meta-models*)))
45
46 (let ((class ,(%def-meta-model (cadr (or (assoc :base-type options) '(t t))) name supers slots options)))
47 class)))
48
9d6c69fb 49(defgeneric def-base-type-class-expander (base-type model name args))
579597e3 50
9d6c69fb
DC
51(defmethod def-base-class-expander ((model t) name args)
52 (def-base-type-class-expander (meta-model.base-type model) model name args))
579597e3 53
579597e3 54(defmethod base-class-name ((model t))
55 (class-name (class-of (meta-model.instance model))))
579597e3 56
57(defmethod view-class-metadata ((model t))
835ac88d
DC
58 "
59This is what meta-model.metadata used to be called,
60most of the below functions expect this method to exist"
579597e3 61 (meta-model.metadata model))
62
63(defun list-item-helper (type view &key (ret-fun #'car))
64 "A helper function for the LIST-* methods"
65 (remove nil
66 (mapcar #'(lambda (slot)
67 (let ((ret-val (funcall ret-fun slot))
68 (kind (getf (cdr slot) :db-kind)))
69 (when (eql kind type)
70 ret-val )))
71 (view-class-metadata view))))
72
73(defmethod list-join-attributes ((view t))
74 "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"
75 (remove nil (mapcar #'(lambda (def)(cons (car def) (getf (cdr def) :db-info ))) (view-class-metadata view))))
76
77(defun list-relations-helper (view predicate-method &key (test-key :home-key) (return-key :join-class) (return-full nil))
78 "A helper function for the listing join, relations and the like"
79 (remove nil (mapcar #'(lambda (x)
80 (when (funcall predicate-method view (getf (cdr x) test-key ))
81 (if return-full
82 x
83 (getf (cdr x) return-key ))))
84 (list-join-attributes view))))
85
86(defmethod list-slots ((view t))
87 "list the non-joined slots of VIEW-CLASS"
88 (remove-if #'(lambda (x) (find x (list-joins view)))
89 (append (list-item-helper :key view)
90 (list-item-helper nil view)
91 (list-item-helper :base view))))
92
93(defmethod list-slot-types ((view t))
94 "Returns an alist of (slot-name slot-type) where SLOT-TYPE is the CLSQL type"
95 (labels ((rfun (slot)
96 (cons (car slot)
97 (list (getf (cdr slot):type))))
98 (lister (type)
99 (list-item-helper
100 type view
101 :ret-fun #'rfun)))
102 (append (lister :key) (lister :base))))
103
104(defmethod slot-type ((view t) slot)
105 "returns the CLSQL type of SLOT"
106 (second (assoc slot (list-slot-types view))))
107
108(defmethod list-joins ((view t))
109 "lists slots that represent joins"
110 (list-item-helper :join view))
111
112(defmethod list-keys ((view t))
113 "lists slots marked as :key"
114 (list-item-helper :key view))
115
116(defmethod primary-key-p ((view t) slot)
117 "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"
118 (find slot (list-keys view)))
119
120(defmethod list-foreign-keys ((view t))
121 "returns a list of FOREIGN-KEYS"
122 (flet ((my-primary-key-p (slot)
123 (primary-key-p view slot)))
124 (remove nil (remove-if #'my-primary-key-p
125 (mapcar #'(lambda (def)
126 (getf (cdr def) :home-key))
127 (list-join-attributes view))))))
128
129(defmethod foreign-key-p ((view t) slot)
130 "returns SLOT if it's a foreign key, nil otherwise"
131 (find slot (list-foreign-keys view)))
132
133
134
135(defmethod list-has-a ((view t))
136 "returns a list of view-classes that are in a 1:1 relationship with VIEW"
137 (list-relations-helper view #'foreign-key-p))
138
139(defmethod list-has-many ((view t))
140 "returns a list of view-classes that are in a 1:Many relationship with VIEW"
141 (mapcar #'car
142 (remove-if #'(lambda (x) (getf (cdr x) :target-slot))
143 (list-relations-helper
144 view
145 #'primary-key-p :return-full t))))
146
147(defmethod list-many-to-many ((view t))
148 "returns a list of view-classes that are in a Many:Many relationship with VIEW"
149 (mapcar #'car (list-relations-helper
150 view
151 #'(lambda (c a)
152 (declare (ignore c))a)
153 :test-key :target-slot
154 :return-full t)))
155
156(defmethod explode-foreign-key ((model clsql:standard-db-object) slot)
157 "returns the clsql view-class joined on SLOT"
158 (dolist (s (list-join-attributes model))
159 (when (equal (getf (cdr s) :home-key) slot)
4eabb213 160 (let ((val (slot-value model (car s))))
161 (return-from explode-foreign-key
233380f7 162 (values (if val val (make-instance (getf (cdr s) :join-class)))
163 (getf (cdr s) :foreign-key)))))))
579597e3 164
165(defun find-join-helper (foreign-key)
166 (lambda (class slot)
167 (declare (ignore class))
168 (when (equal slot foreign-key) t)))
169
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) )))
173
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)))
177
a6644385 178(defmethod explode-has-many ((view t) join-slot)
179 "returns the class of the join as the primary value, the second and third value is the home key and the foreign key"
180 (let ((att (assoc join-slot (list-join-attributes view))))
181 (values (getf (cdr att) :join-class)
182 (getf (cdr att) :home-key)
183 (getf (cdr att) :foreign-key))))
184
9d6c69fb
DC
185(defgeneric expr-= (instance slot-name value)
186 (:documentation "Create search expression for appropriate backend."))
a6644385 187
9d6c69fb
DC
188(defgeneric expr-> (instance slot-name value)
189 (:documentation "Create search expression for appropriate backend."))
579597e3 190
9d6c69fb
DC
191(defgeneric expr-< (instance slot-name value)
192 (:documentation "Create search expression for appropriate backend."))
579597e3 193
9d6c69fb
DC
194(defgeneric expr-ends-with (instance slot-name value)
195 (:documentation "Create search expression for appropriate backend."))
196
197(defgeneric expr-starts-with (instance slot-name value)
198 (:documentation "Create search expression for appropriate backend."))
199
200(defgeneric expr-contains (instance slot-name value)
201 (:documentation "Create search expression for appropriate backend."))
202
203(defgeneric expr-and (instance &rest args)
204 (:documentation "Create search expression for appropriate backend."))
205
206(defgeneric expr-or (instance &rest args)
207 (:documentation "Create search expression for appropriate backend."))
208
209(defgeneric expr-not (instance &rest args)
210 (:documentation "Create search expression for appropriate backend."))
211
212(defgeneric select-instances (instance &rest args)
213 (:documentation "Select instances in backend dependent way"))
214
215(defmacro def-compare-expr (instance-type name expr &key value-format)
216 `(defmethod ,name ((instance ,instance-type) slot-name value)
217 (declare (ignore instance))
218 (,expr slot-name ,(typecase value-format
219 (null 'value)
220 (string `(format nil ,value-format value))
221 (t `(,value-format value))))))
222
223(defmacro def-logical-expr (instance-type name expr)
224 `(defmethod ,name ((instance ,instance-type) &rest args)
225 (declare (ignore instance))
226 (apply ,expr args)))