eval-when :wave-dead-chicken. this is part of the EVAL mess that i need to clean up.
[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))
58 (meta-model.metadata model))
59
60(defun list-item-helper (type view &key (ret-fun #'car))
61 "A helper function for the LIST-* methods"
62 (remove nil
63 (mapcar #'(lambda (slot)
64 (let ((ret-val (funcall ret-fun slot))
65 (kind (getf (cdr slot) :db-kind)))
66 (when (eql kind type)
67 ret-val )))
68 (view-class-metadata view))))
69
70(defmethod list-join-attributes ((view t))
71 "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"
72 (remove nil (mapcar #'(lambda (def)(cons (car def) (getf (cdr def) :db-info ))) (view-class-metadata view))))
73
74(defun list-relations-helper (view predicate-method &key (test-key :home-key) (return-key :join-class) (return-full nil))
75 "A helper function for the listing join, relations and the like"
76 (remove nil (mapcar #'(lambda (x)
77 (when (funcall predicate-method view (getf (cdr x) test-key ))
78 (if return-full
79 x
80 (getf (cdr x) return-key ))))
81 (list-join-attributes view))))
82
83(defmethod list-slots ((view t))
84 "list the non-joined slots of VIEW-CLASS"
85 (remove-if #'(lambda (x) (find x (list-joins view)))
86 (append (list-item-helper :key view)
87 (list-item-helper nil view)
88 (list-item-helper :base view))))
89
90(defmethod list-slot-types ((view t))
91 "Returns an alist of (slot-name slot-type) where SLOT-TYPE is the CLSQL type"
92 (labels ((rfun (slot)
93 (cons (car slot)
94 (list (getf (cdr slot):type))))
95 (lister (type)
96 (list-item-helper
97 type view
98 :ret-fun #'rfun)))
99 (append (lister :key) (lister :base))))
100
101(defmethod slot-type ((view t) slot)
102 "returns the CLSQL type of SLOT"
103 (second (assoc slot (list-slot-types view))))
104
105(defmethod list-joins ((view t))
106 "lists slots that represent joins"
107 (list-item-helper :join view))
108
109(defmethod list-keys ((view t))
110 "lists slots marked as :key"
111 (list-item-helper :key view))
112
113(defmethod primary-key-p ((view t) slot)
114 "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"
115 (find slot (list-keys view)))
116
117(defmethod list-foreign-keys ((view t))
118 "returns a list of FOREIGN-KEYS"
119 (flet ((my-primary-key-p (slot)
120 (primary-key-p view slot)))
121 (remove nil (remove-if #'my-primary-key-p
122 (mapcar #'(lambda (def)
123 (getf (cdr def) :home-key))
124 (list-join-attributes view))))))
125
126(defmethod foreign-key-p ((view t) slot)
127 "returns SLOT if it's a foreign key, nil otherwise"
128 (find slot (list-foreign-keys view)))
129
130
131
132(defmethod list-has-a ((view t))
133 "returns a list of view-classes that are in a 1:1 relationship with VIEW"
134 (list-relations-helper view #'foreign-key-p))
135
136(defmethod list-has-many ((view t))
137 "returns a list of view-classes that are in a 1:Many relationship with VIEW"
138 (mapcar #'car
139 (remove-if #'(lambda (x) (getf (cdr x) :target-slot))
140 (list-relations-helper
141 view
142 #'primary-key-p :return-full t))))
143
144(defmethod list-many-to-many ((view t))
145 "returns a list of view-classes that are in a Many:Many relationship with VIEW"
146 (mapcar #'car (list-relations-helper
147 view
148 #'(lambda (c a)
149 (declare (ignore c))a)
150 :test-key :target-slot
151 :return-full t)))
152
153(defmethod explode-foreign-key ((model clsql:standard-db-object) slot)
154 "returns the clsql view-class joined on SLOT"
155 (dolist (s (list-join-attributes model))
156 (when (equal (getf (cdr s) :home-key) slot)
4eabb213 157 (let ((val (slot-value model (car s))))
158 (return-from explode-foreign-key
233380f7 159 (values (if val val (make-instance (getf (cdr s) :join-class)))
160 (getf (cdr s) :foreign-key)))))))
579597e3 161
162(defun find-join-helper (foreign-key)
163 (lambda (class slot)
164 (declare (ignore class))
165 (when (equal slot foreign-key) t)))
166
167(defmethod find-join-class ((view t) foreign-key)
168 "Returns the VIEW-CLASS that is joined to VIEW via FOREGN-KEY"
169 (car (list-relations-helper view (find-join-helper foreign-key) )))
170
171(defmethod find-join-key ((view t) foreign-key)
172 "returns the SLOT in the foreign VIEW-CLASS that joins with FOREIGN-KEY"
173 (car (list-relations-helper view (find-join-helper foreign-key) :return-key :foreign-key)))
174
a6644385 175(defmethod explode-has-many ((view t) join-slot)
176 "returns the class of the join as the primary value, the second and third value is the home key and the foreign key"
177 (let ((att (assoc join-slot (list-join-attributes view))))
178 (values (getf (cdr att) :join-class)
179 (getf (cdr att) :home-key)
180 (getf (cdr att) :foreign-key))))
181
9d6c69fb
DC
182(defgeneric expr-= (instance slot-name value)
183 (:documentation "Create search expression for appropriate backend."))
a6644385 184
9d6c69fb
DC
185(defgeneric expr-> (instance slot-name value)
186 (:documentation "Create search expression for appropriate backend."))
579597e3 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-ends-with (instance slot-name value)
192 (:documentation "Create search expression for appropriate backend."))
193
194(defgeneric expr-starts-with (instance slot-name value)
195 (:documentation "Create search expression for appropriate backend."))
196
197(defgeneric expr-contains (instance slot-name value)
198 (:documentation "Create search expression for appropriate backend."))
199
200(defgeneric expr-and (instance &rest args)
201 (:documentation "Create search expression for appropriate backend."))
202
203(defgeneric expr-or (instance &rest args)
204 (:documentation "Create search expression for appropriate backend."))
205
206(defgeneric expr-not (instance &rest args)
207 (:documentation "Create search expression for appropriate backend."))
208
209(defgeneric select-instances (instance &rest args)
210 (:documentation "Select instances in backend dependent way"))
211
212(defmacro def-compare-expr (instance-type name expr &key value-format)
213 `(defmethod ,name ((instance ,instance-type) slot-name value)
214 (declare (ignore instance))
215 (,expr slot-name ,(typecase value-format
216 (null 'value)
217 (string `(format nil ,value-format value))
218 (t `(,value-format value))))))
219
220(defmacro def-logical-expr (instance-type name expr)
221 `(defmethod ,name ((instance ,instance-type) &rest args)
222 (declare (ignore instance))
223 (apply ,expr args)))