1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; CLSQL metaclass for standard-db-objects created in the OODDL.
8 ;;;; This file is part of CLSQL.
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (when (>= (length (generic-function-lambda-list
19 (ensure-generic-function
20 'compute-effective-slot-definition)))
22 (pushnew :kmr-normal-cesd cl:*features*))
24 (when (>= (length (generic-function-lambda-list
25 (ensure-generic-function
26 'direct-slot-definition-class)))
28 (pushnew :kmr-normal-dsdc cl:*features*))
30 (when (>= (length (generic-function-lambda-list
31 (ensure-generic-function
32 'effective-slot-definition-class)))
34 (pushnew :kmr-normal-esdc cl:*features*)))
37 ;; ------------------------------------------------------------
38 ;; metaclass: view-class
40 (defclass standard-db-class (standard-class)
45 :accessor object-definition
52 :accessor view-class-qualifier
55 (:documentation "VIEW-CLASS metaclass."))
57 ;;; Lispworks 4.2 and before requires special processing of extra slot and class options
59 (defvar +extra-slot-options+ '(:column :db-kind :db-type :db-reader :void-value :db-constraints
61 (defvar +extra-class-options+ '(:base-table))
64 (dolist (slot-option +extra-slot-options+)
65 (process-slot-option standard-db-class slot-option))
68 (dolist (class-option +extra-class-options+)
69 (process-class-option standard-db-class class-option))
71 (defmethod validate-superclass ((class standard-db-class)
72 (superclass standard-class))
75 (defun table-name-from-arg (arg)
78 ((typep arg 'sql-ident)
79 (slot-value arg 'name))
81 (intern (symbol-name-default-case arg)))))
83 (defun column-name-from-arg (arg)
86 ((typep arg 'sql-ident)
87 (slot-value arg 'name))
89 (intern (symbol-name-default-case arg)))))
92 (defun remove-keyword-arg (arglist akey)
93 (let ((mylist arglist)
95 (labels ((pop-arg (alist)
96 (let ((arg (pop alist))
98 (unless (equal arg akey)
99 (setf newlist (append (list arg val) newlist)))
100 (when alist (pop-arg alist)))))
104 (defmethod initialize-instance :around ((class standard-db-class)
106 &key direct-superclasses base-table
109 (let ((root-class (find-class 'standard-db-object nil))
110 (vmc (find-class 'standard-db-class)))
111 (setf (view-class-qualifier class)
114 (if (member-if #'(lambda (super)
115 (eq (class-of super) vmc)) direct-superclasses)
117 (apply #'call-next-method
119 :direct-superclasses (append (list root-class)
121 (remove-keyword-arg all-keys :direct-superclasses)))
123 (setf (view-table class)
124 (table-name-from-arg (sql-escape (or (and base-table
125 (if (listp base-table)
128 (class-name class)))))
129 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
132 (defmethod reinitialize-instance :around ((class standard-db-class)
135 direct-superclasses qualifier
137 (let ((root-class (find-class 'standard-db-object nil))
138 (vmc (find-class 'standard-db-class)))
139 (setf (view-table class)
140 (table-name-from-arg (sql-escape (or (and base-table
141 (if (listp base-table)
144 (class-name class)))))
145 (setf (view-class-qualifier class)
147 (if (and root-class (not (equal class root-class)))
148 (if (member-if #'(lambda (super)
149 (eq (class-of super) vmc)) direct-superclasses)
151 (apply #'call-next-method
153 :direct-superclasses (append (list root-class)
155 (remove-keyword-arg all-keys :direct-superclasses)))
157 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
161 (defun get-keywords (keys list)
162 (flet ((extract (key)
163 (let ((pos (position key list)))
165 (nth (1+ pos) list)))))
166 (mapcar #'extract keys)))
168 (defun describe-db-layout (class)
169 (flet ((not-db-col (col)
170 (not (member (nth 2 col) '(nil :base :key))))
172 (let ((type (slot-value slot 'type)))
175 (list (slot-value slot 'name)
177 (slot-value slot 'db-kind)
178 (and (slot-boundp slot 'column)
179 (slot-value slot 'column))))))
180 (let ((all-slots (mapcar #'frob-slot (ordered-class-slots class))))
181 (setq all-slots (remove-if #'not-db-col all-slots))
182 (setq all-slots (stable-sort all-slots #'string< :key #'car))
183 ;;(mapcar #'dink-type all-slots)
186 (defun register-metaclass (class slots)
187 (labels ((not-db-col (col)
188 (not (member (nth 2 col) '(nil :base :key))))
190 (get-keywords '(:name :type :db-kind :column) slot)))
191 (let ((all-slots (mapcar #'frob-slot slots)))
192 (setq all-slots (remove-if #'not-db-col all-slots))
193 (setq all-slots (stable-sort all-slots #'string< :key #'car))
194 (setf (object-definition class) all-slots))
195 #-(or allegro openmcl)
196 (setf (key-slots class) (remove-if-not (lambda (slot)
197 (eql (slot-value slot 'db-kind)
199 (ordered-class-slots class)))))
201 #+(or allegro openmcl)
202 (defmethod finalize-inheritance :after ((class standard-db-class))
203 (setf (key-slots class) (remove-if-not (lambda (slot)
204 (eql (slot-value slot 'db-kind)
206 (ordered-class-slots class))))
208 ;; return the deepest view-class ancestor for a given view class
210 (defun base-db-class (classname)
211 (let* ((class (find-class classname))
212 (db-class (find-class 'standard-db-object)))
214 (let ((cds (class-direct-superclasses class)))
216 (error "not a db class"))
217 ((member db-class cds)
218 (return (class-name class))))
219 (setq class (car cds))))))
221 (defun db-ancestors (classname)
222 (let ((class (find-class classname))
223 (db-class (find-class 'standard-db-object)))
224 (labels ((ancestors (class)
225 (let ((scs (class-direct-superclasses class)))
226 (if (member db-class scs)
228 (append (list class) (mapcar #'ancestors scs))))))
231 (defclass view-class-slot-definition-mixin ()
233 :accessor view-class-slot-column
236 "The name of the SQL column this slot is stored in. Defaults to
239 :accessor view-class-slot-db-kind
244 "The kind of DB mapping which is performed for this slot. :base
245 indicates the slot maps to an ordinary column of the DB view. :key
246 indicates that this slot corresponds to part of the unique keys for
247 this view, :join indicates ... and :virtual indicates that this slot
248 is an ordinary CLOS slot. Defaults to :base.")
250 :accessor view-class-slot-db-reader
254 "If a string, then when reading values from the DB, the string
255 will be used for a format string, with the only value being the value
256 from the database. The resulting string will be used as the slot
257 value. If a function then it will take one argument, the value from
258 the database, and return the value that should be put into the slot.")
260 :accessor view-class-slot-db-writer
264 "If a string, then when reading values from the slot for the DB,
265 the string will be used for a format string, with the only value being
266 the value of the slot. The resulting string will be used as the
267 column value in the DB. If a function then it will take one argument,
268 the value of the slot, and return the value that should be put into
271 :accessor view-class-slot-db-type
275 "A string which will be used as the type specifier for this slots
276 column definition in the database.")
278 :accessor view-class-slot-db-constraints
279 :initarg :db-constraints
282 "A single constraint or list of constraints for this column")
284 :accessor view-class-slot-void-value
288 "Value to store is the SQL value is NULL. Default is NIL.")
290 :accessor view-class-slot-db-info
292 :documentation "Description of the join.")
294 :accessor specified-type
296 :documentation "KMR: Internal slot storing the :type specified by user.")))
298 (defparameter *db-info-lambda-list*
304 (retrieval :immmediate)
307 (defun parse-db-info (db-info-list)
309 (&key join-class home-key key-join foreign-key (delete-rule nil)
310 (target-slot nil) (retrieval :deferred) (set nil))
312 (let ((ih (make-hash-table :size 6)))
314 (setf (gethash :join-class ih) join-class)
315 (error "Must specify :join-class in :db-info"))
317 (setf (gethash :home-key ih) home-key)
318 (error "Must specify :home-key in :db-info"))
320 (setf (gethash :delete-rule ih) delete-rule))
322 (setf (gethash :foreign-key ih) foreign-key)
323 (error "Must specify :foreign-key in :db-info"))
325 (setf (gethash :key-join ih) t))
327 (setf (gethash :target-slot ih) target-slot))
329 (setf (gethash :set ih) set))
332 (setf (gethash :retrieval ih) retrieval)
333 (if (eql retrieval :immediate)
334 (setf (gethash :set ih) nil))))
337 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
338 standard-direct-slot-definition)
341 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
342 standard-effective-slot-definition)
345 (defmethod direct-slot-definition-class ((class standard-db-class)
346 #+kmr-normal-dsdc &rest
348 (declare (ignore initargs))
349 (find-class 'view-class-direct-slot-definition))
351 (defmethod effective-slot-definition-class ((class standard-db-class)
352 #+kmr-normal-esdc &rest
354 (declare (ignore initargs))
355 (find-class 'view-class-effective-slot-definition))
358 (defun compute-class-precedence-list (class)
359 ;; safe to call this in openmcl
360 (class-precedence-list class))
363 (defmethod compute-slots ((class standard-db-class))
364 "Need to sort order of class slots so they are the same across
366 (let ((slots (call-next-method))
369 (dolist (c (compute-class-precedence-list class))
370 (dolist (s (class-direct-slots c))
371 (let ((name (slot-definition-name s)))
372 (unless (find name desired-sequence)
373 (push name desired-sequence)))))
374 (dolist (desired desired-sequence)
375 (let ((slot (find desired slots :key #'slot-definition-name)))
377 (push slot output-slots)))
380 (defun compute-lisp-type-from-slot-specification (slotd specified-type)
381 "Computes the Lisp type for a user-specified type. Needed for OpenMCL
382 which does type checking before storing a value in a slot."
383 (declare (ignore slotd))
384 ;; This function is called after the base compute-effective-slots is called.
385 ;; OpenMCL sets the type-predicate based on the initial value of the slots type.
386 ;; so we have to override the type-predicates here
388 ((consp specified-type)
390 ((and (symbolp (car specified-type))
391 (string-equal (symbol-name (car specified-type)) "string"))
395 ((eq (ensure-keyword specified-type) :bigint)
398 ((null specified-type)
403 ;; Compute the slot definition for slots in a view-class. Figures out
404 ;; what kind of database value (if any) is stored there, generates and
405 ;; verifies the column name.
407 (defmethod compute-effective-slot-definition ((class standard-db-class)
408 #+kmr-normal-cesd slot-name
410 #+kmr-normal-cesd (declare (ignore slot-name))
412 ;; KMR: store the user-specified type and then compute
413 ;; real Lisp type and store it
414 (let ((dsd (car direct-slots)))
415 (when (and (typep dsd 'view-class-slot-definition-mixin)
416 (null (specified-type dsd)))
417 (setf (specified-type dsd)
418 (slot-definition-type dsd))
419 (setf (slot-value dsd 'type)
420 (compute-lisp-type-from-slot-specification
421 dsd (slot-definition-type dsd))))
423 (let ((esd (call-next-method)))
425 (view-class-slot-definition-mixin
426 ;; Use the specified :column argument if it is supplied, otherwise
427 ;; the column slot is filled in with the slot-name, but transformed
428 ;; to be sql safe, - to _ and such.
429 (setf (slot-value esd 'column)
430 (column-name-from-arg
431 (if (slot-boundp dsd 'column)
432 (view-class-slot-column dsd)
433 (column-name-from-arg
434 (sql-escape (slot-definition-name dsd))))))
436 (setf (slot-value esd 'db-type)
437 (when (slot-boundp dsd 'db-type)
438 (view-class-slot-db-type dsd)))
440 (setf (slot-value esd 'void-value)
441 (view-class-slot-void-value dsd))
443 ;; :db-kind slot value defaults to :base (store slot value in
446 (setf (slot-value esd 'db-kind)
447 (if (slot-boundp dsd 'db-kind)
448 (view-class-slot-db-kind dsd)
451 (setf (slot-value esd 'db-writer)
452 (when (slot-boundp dsd 'db-writer)
453 (view-class-slot-db-writer dsd)))
454 (setf (slot-value esd 'db-constraints)
455 (when (slot-boundp dsd 'db-constraints)
456 (view-class-slot-db-constraints dsd)))
458 ;; I wonder if this slot option and the previous could be merged,
459 ;; so that :base and :key remain keyword options, but :db-kind
460 ;; :join becomes :db-kind (:join <db info .... >)?
462 (setf (slot-value esd 'db-info)
463 (when (slot-boundp dsd 'db-info)
464 (if (listp (view-class-slot-db-info dsd))
465 (parse-db-info (view-class-slot-db-info dsd))
466 (view-class-slot-db-info dsd))))
468 (setf (specified-type esd) (specified-type dsd))
473 (change-class esd 'view-class-effective-slot-definition
475 #+allegro (slot-definition-name dsd))
477 (setf (slot-value esd 'column)
478 (column-name-from-arg
479 (sql-escape (slot-definition-name dsd))))
481 (setf (slot-value esd 'db-info) nil)
482 (setf (slot-value esd 'db-kind)
486 (defun slotdefs-for-slots-with-class (slots class)
489 (let ((c (slotdef-for-slot-with-class s class)))
490 (if c (setf result (cons c result)))))
493 (defun slotdef-for-slot-with-class (slot class)
494 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
495 (class-slots class)))
498 (eval-when (:compile-toplevel :load-toplevel :execute)
500 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
502 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
504 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))