added perform-set-attribute and a few other fixes to this builds again.
[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)
35 `(defclass ,name ,(gen-supers supers)
36 ()
9d6c69fb 37 (:default-initargs :metadata ',slots :base-type ,base-type)))
579597e3 38
39
40(defmacro def-meta-model (name supers slots &rest options)
41 `(progn
42 (when (not (member (quote ,name) *meta-models*))
43 (setf *meta-models* (cons (quote ,name) *meta-models*)))
44
45 (let ((class ,(%def-meta-model (cadr (or (assoc :base-type options) '(t t))) name supers slots options)))
46 class)))
47
9d6c69fb 48(defgeneric def-base-type-class-expander (base-type model name args))
579597e3 49
9d6c69fb
DC
50(defmethod def-base-class-expander ((model t) name args)
51 (def-base-type-class-expander (meta-model.base-type model) model name args))
579597e3 52
53(defmacro def-base-class (name (model) &rest args)
54 (let ((i (make-instance model)))
e8e743d7 55 `(progn
56 (eval-when (:compile-toplevel :load-toplevel :execute)
57 ,(def-base-class-expander i :clsql name args))
579597e3 58 (defmethod meta-model.metadata ((m ,name))
59 ',(meta-model.metadata i)))))
60
61
62(defmethod base-class-name ((model t))
63 (class-name (class-of (meta-model.instance model))))
64
65
66
67(defmethod view-class-metadata ((model t))
68 (meta-model.metadata model))
69
70(defun list-item-helper (type view &key (ret-fun #'car))
71 "A helper function for the LIST-* methods"
72 (remove nil
73 (mapcar #'(lambda (slot)
74 (let ((ret-val (funcall ret-fun slot))
75 (kind (getf (cdr slot) :db-kind)))
76 (when (eql kind type)
77 ret-val )))
78 (view-class-metadata view))))
79
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))))
83
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 ))
88 (if return-full
89 x
90 (getf (cdr x) return-key ))))
91 (list-join-attributes view))))
92
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))))
99
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)
103 (cons (car slot)
104 (list (getf (cdr slot):type))))
105 (lister (type)
106 (list-item-helper
107 type view
108 :ret-fun #'rfun)))
109 (append (lister :key) (lister :base))))
110
111(defmethod slot-type ((view t) slot)
112 "returns the CLSQL type of SLOT"
113 (second (assoc slot (list-slot-types view))))
114
115(defmethod list-joins ((view t))
116 "lists slots that represent joins"
117 (list-item-helper :join view))
118
119(defmethod list-keys ((view t))
120 "lists slots marked as :key"
121 (list-item-helper :key view))
122
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)))
126
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))))))
135
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)))
139
140
141
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))
145
146(defmethod list-has-many ((view t))
147 "returns a list of view-classes that are in a 1:Many relationship with VIEW"
148 (mapcar #'car
149 (remove-if #'(lambda (x) (getf (cdr x) :target-slot))
150 (list-relations-helper
151 view
152 #'primary-key-p :return-full t))))
153
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
157 view
158 #'(lambda (c a)
159 (declare (ignore c))a)
160 :test-key :target-slot
161 :return-full t)))
162
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)
4eabb213 167 (let ((val (slot-value model (car s))))
168 (return-from explode-foreign-key
233380f7 169 (values (if val val (make-instance (getf (cdr s) :join-class)))
170 (getf (cdr s) :foreign-key)))))))
579597e3 171
172(defun find-join-helper (foreign-key)
173 (lambda (class slot)
174 (declare (ignore class))
175 (when (equal slot foreign-key) t)))
176
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) )))
180
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)))
184
a6644385 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))))
191
9d6c69fb
DC
192(defgeneric expr-= (instance slot-name value)
193 (:documentation "Create search expression for appropriate backend."))
a6644385 194
9d6c69fb
DC
195(defgeneric expr-> (instance slot-name value)
196 (:documentation "Create search expression for appropriate backend."))
579597e3 197
9d6c69fb
DC
198(defgeneric expr-< (instance slot-name value)
199 (:documentation "Create search expression for appropriate backend."))
579597e3 200
9d6c69fb
DC
201(defgeneric expr-ends-with (instance slot-name value)
202 (:documentation "Create search expression for appropriate backend."))
203
204(defgeneric expr-starts-with (instance slot-name value)
205 (:documentation "Create search expression for appropriate backend."))
206
207(defgeneric expr-contains (instance slot-name value)
208 (:documentation "Create search expression for appropriate backend."))
209
210(defgeneric expr-and (instance &rest args)
211 (:documentation "Create search expression for appropriate backend."))
212
213(defgeneric expr-or (instance &rest args)
214 (:documentation "Create search expression for appropriate backend."))
215
216(defgeneric expr-not (instance &rest args)
217 (:documentation "Create search expression for appropriate backend."))
218
219(defgeneric select-instances (instance &rest args)
220 (:documentation "Select instances in backend dependent way"))
221
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
226 (null 'value)
227 (string `(format nil ,value-format value))
228 (t `(,value-format value))))))
229
230(defmacro def-logical-expr (instance-type name expr)
231 `(defmethod ,name ((instance ,instance-type) &rest args)
232 (declare (ignore instance))
233 (apply ,expr args)))