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 ;;;; *************************************************************************
15 (in-package #:clsql-sys)
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))
63 (dolist (slot-option +extra-slot-options+)
64 (process-slot-option standard-db-class slot-option))
66 (dolist (class-option +extra-class-options+)
67 (process-class-option standard-db-class class-option))
69 (defmethod validate-superclass ((class standard-db-class)
70 (superclass standard-class))
73 (defun table-name-from-arg (arg)
76 ((typep arg 'sql-ident)
77 (slot-value arg 'name))
79 (intern (symbol-name-default-case arg)))))
81 (defun column-name-from-arg (arg)
84 ((typep arg 'sql-ident)
85 (slot-value arg 'name))
87 (intern (symbol-name-default-case arg)))))
90 (defun remove-keyword-arg (arglist akey)
91 (let ((mylist arglist)
93 (labels ((pop-arg (alist)
94 (let ((arg (pop alist))
96 (unless (equal arg akey)
97 (setf newlist (append (list arg val) newlist)))
98 (when alist (pop-arg alist)))))
102 (defmethod initialize-instance :around ((class standard-db-class)
104 &key direct-superclasses base-table
107 (let ((root-class (find-class 'standard-db-object nil))
108 (vmc (find-class 'standard-db-class)))
109 (setf (view-class-qualifier class)
112 (if (member-if #'(lambda (super)
113 (eq (class-of super) vmc)) direct-superclasses)
115 (apply #'call-next-method
117 :direct-superclasses (append (list root-class)
119 (remove-keyword-arg all-keys :direct-superclasses)))
121 (setf (view-table class)
122 (table-name-from-arg (sql-escape (or (and base-table
123 (if (listp base-table)
126 (class-name class)))))
127 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
130 (defmethod reinitialize-instance :around ((class standard-db-class)
133 direct-superclasses qualifier
135 (let ((root-class (find-class 'standard-db-object nil))
136 (vmc (find-class 'standard-db-class)))
137 (setf (view-table class)
138 (table-name-from-arg (sql-escape (or (and base-table
139 (if (listp base-table)
142 (class-name class)))))
143 (setf (view-class-qualifier class)
145 (if (and root-class (not (equal class root-class)))
146 (if (member-if #'(lambda (super)
147 (eq (class-of super) vmc)) direct-superclasses)
149 (apply #'call-next-method
151 :direct-superclasses (append (list root-class)
153 (remove-keyword-arg all-keys :direct-superclasses)))
155 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
159 (defun get-keywords (keys list)
160 (flet ((extract (key)
161 (let ((pos (position key list)))
163 (nth (1+ pos) list)))))
164 (mapcar #'extract keys)))
166 (defun describe-db-layout (class)
167 (flet ((not-db-col (col)
168 (not (member (nth 2 col) '(nil :base :key))))
170 (let ((type (slot-value slot 'type)))
173 (list (slot-value slot 'name)
175 (slot-value slot 'db-kind)
176 (and (slot-boundp slot 'column)
177 (slot-value slot 'column))))))
178 (let ((all-slots (mapcar #'frob-slot (ordered-class-slots class))))
179 (setq all-slots (remove-if #'not-db-col all-slots))
180 (setq all-slots (stable-sort all-slots #'string< :key #'car))
181 ;;(mapcar #'dink-type all-slots)
184 (defun register-metaclass (class slots)
185 (labels ((not-db-col (col)
186 (not (member (nth 2 col) '(nil :base :key))))
188 (get-keywords '(:name :type :db-kind :column) slot)))
189 (let ((all-slots (mapcar #'frob-slot slots)))
190 (setq all-slots (remove-if #'not-db-col all-slots))
191 (setq all-slots (stable-sort all-slots #'string< :key #'car))
192 (setf (object-definition class) all-slots))
193 #-(or allegro openmcl)
194 (setf (key-slots class) (remove-if-not (lambda (slot)
195 (eql (slot-value slot 'db-kind)
197 (ordered-class-slots class)))))
199 #+(or allegro openmcl)
200 (defmethod finalize-inheritance :after ((class standard-db-class))
201 ;; KMRL for slots without a type set, openmcl sets type-predicate to ccl:false
202 ;; for standard-db-class
206 (if (eq 'ccl:false (slot-value s 'ccl::type-predicate))
207 (setf (slot-value s 'ccl::type-predicate) 'ccl:true)))
210 (setf (key-slots class) (remove-if-not (lambda (slot)
211 (eql (slot-value slot 'db-kind)
213 (ordered-class-slots class))))
215 ;; return the deepest view-class ancestor for a given view class
217 (defun base-db-class (classname)
218 (let* ((class (find-class classname))
219 (db-class (find-class 'standard-db-object)))
221 (let ((cds (class-direct-superclasses class)))
223 (error "not a db class"))
224 ((member db-class cds)
225 (return (class-name class))))
226 (setq class (car cds))))))
228 (defun db-ancestors (classname)
229 (let ((class (find-class classname))
230 (db-class (find-class 'standard-db-object)))
231 (labels ((ancestors (class)
232 (let ((scs (class-direct-superclasses class)))
233 (if (member db-class scs)
235 (append (list class) (mapcar #'ancestors scs))))))
238 (defclass view-class-slot-definition-mixin ()
240 :accessor view-class-slot-column
243 "The name of the SQL column this slot is stored in. Defaults to
246 :accessor view-class-slot-db-kind
251 "The kind of DB mapping which is performed for this slot. :base
252 indicates the slot maps to an ordinary column of the DB view. :key
253 indicates that this slot corresponds to part of the unique keys for
254 this view, :join indicates ... and :virtual indicates that this slot
255 is an ordinary CLOS slot. Defaults to :base.")
257 :accessor view-class-slot-db-reader
261 "If a string, then when reading values from the DB, the string
262 will be used for a format string, with the only value being the value
263 from the database. The resulting string will be used as the slot
264 value. If a function then it will take one argument, the value from
265 the database, and return the value that should be put into the slot.")
267 :accessor view-class-slot-db-writer
271 "If a string, then when reading values from the slot for the DB,
272 the string will be used for a format string, with the only value being
273 the value of the slot. The resulting string will be used as the
274 column value in the DB. If a function then it will take one argument,
275 the value of the slot, and return the value that should be put into
278 :accessor view-class-slot-db-type
282 "A string which will be used as the type specifier for this slots
283 column definition in the database.")
285 :accessor view-class-slot-db-constraints
286 :initarg :db-constraints
289 "A single constraint or list of constraints for this column")
291 :accessor view-class-slot-void-value
295 "Value to store is the SQL value is NULL. Default is NIL.")
297 :accessor view-class-slot-db-info
299 :documentation "Description of the join.")
301 :accessor specified-type
303 :documentation "KMR: Internal slot storing the :type specified by user.")))
305 (defparameter *db-info-lambda-list*
311 (retrieval :immmediate)
314 (defun parse-db-info (db-info-list)
316 (&key join-class home-key key-join foreign-key (delete-rule nil)
317 (target-slot nil) (retrieval :deferred) (set nil))
319 (let ((ih (make-hash-table :size 6)))
321 (setf (gethash :join-class ih) join-class)
322 (error "Must specify :join-class in :db-info"))
324 (setf (gethash :home-key ih) home-key)
325 (error "Must specify :home-key in :db-info"))
327 (setf (gethash :delete-rule ih) delete-rule))
329 (setf (gethash :foreign-key ih) foreign-key)
330 (error "Must specify :foreign-key in :db-info"))
332 (setf (gethash :key-join ih) t))
334 (setf (gethash :target-slot ih) target-slot))
336 (setf (gethash :set ih) set))
339 (setf (gethash :retrieval ih) retrieval)
340 (if (eql retrieval :immediate)
341 (setf (gethash :set ih) nil))))
344 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
345 standard-direct-slot-definition)
348 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
349 standard-effective-slot-definition)
352 (defmethod direct-slot-definition-class ((class standard-db-class)
353 #+kmr-normal-dsdc &rest
355 (declare (ignore initargs))
356 (find-class 'view-class-direct-slot-definition))
358 (defmethod effective-slot-definition-class ((class standard-db-class)
359 #+kmr-normal-esdc &rest
361 (declare (ignore initargs))
362 (find-class 'view-class-effective-slot-definition))
365 (defun compute-class-precedence-list (class)
366 ;; safe to call this in openmcl
367 (class-precedence-list class))
370 (defmethod compute-slots ((class standard-db-class))
371 "Need to sort order of class slots so they are the same across
373 (let ((slots (call-next-method))
376 (dolist (c (compute-class-precedence-list class))
377 (dolist (s (class-direct-slots c))
378 (let ((name (slot-definition-name s)))
379 (unless (find name desired-sequence)
380 (push name desired-sequence)))))
381 (dolist (desired desired-sequence)
382 (let ((slot (find desired slots :key #'slot-definition-name)))
384 (push slot output-slots)))
387 (defun compute-lisp-type-from-slot-specification (slotd specified-type)
388 "Computes the Lisp type for a user-specified type. Needed for OpenMCL
389 which does type checking before storing a value in a slot."
390 #-openmcl (declare (ignore slotd))
391 ;; This function is called after the base compute-effective-slots is called.
392 ;; OpenMCL sets the type-predicate based on the initial value of the slots type.
393 ;; so we have to override the type-predicates here
395 ((consp specified-type)
397 ((and (symbolp (car specified-type))
398 (string-equal (symbol-name (car specified-type)) "string"))
399 #+openmcl (setf (slot-value slotd 'ccl::type-predicate) 'stringp)
402 #+openmcl (setf (slot-value slotd 'ccl::type-predicate) 'ccl:true)
404 ((eq (ensure-keyword specified-type) :bigint)
407 ((null specified-type)
408 ;; setting this here is not enough since openmcl later sets the
409 ;; type-predicate to ccl:false. So, have to check slots again
410 ;; in finalize-inheritance
411 #+openmcl (setf (slot-value slotd 'ccl::type-predicate) 'ccl:true)
414 ;; This can be improved for OpenMCL to set a more specific type
415 ;; predicate based on the value specified-type
416 #+openmcl (setf (slot-value slotd 'ccl::type-predicate) 'ccl:true)
419 ;; Compute the slot definition for slots in a view-class. Figures out
420 ;; what kind of database value (if any) is stored there, generates and
421 ;; verifies the column name.
423 (defmethod compute-effective-slot-definition ((class standard-db-class)
424 #+kmr-normal-cesd slot-name
426 #+kmr-normal-cesd (declare (ignore slot-name))
428 ;; KMR: store the user-specified type and then compute
429 ;; real Lisp type and store it
430 (let ((dsd (car direct-slots)))
431 (when (and (typep dsd 'view-class-slot-definition-mixin)
432 (null (specified-type dsd)))
433 (setf (specified-type dsd)
434 (slot-definition-type dsd))
435 (setf (slot-value dsd 'type)
436 (compute-lisp-type-from-slot-specification
437 dsd (slot-definition-type dsd))))
439 (let ((esd (call-next-method)))
441 (view-class-slot-definition-mixin
442 ;; Use the specified :column argument if it is supplied, otherwise
443 ;; the column slot is filled in with the slot-name, but transformed
444 ;; to be sql safe, - to _ and such.
445 (setf (slot-value esd 'column)
446 (column-name-from-arg
447 (if (slot-boundp dsd 'column)
448 (view-class-slot-column dsd)
449 (column-name-from-arg
450 (sql-escape (slot-definition-name dsd))))))
452 (setf (slot-value esd 'db-type)
453 (when (slot-boundp dsd 'db-type)
454 (view-class-slot-db-type dsd)))
456 (setf (slot-value esd 'void-value)
457 (view-class-slot-void-value dsd))
459 ;; :db-kind slot value defaults to :base (store slot value in
462 (setf (slot-value esd 'db-kind)
463 (if (slot-boundp dsd 'db-kind)
464 (view-class-slot-db-kind dsd)
467 (setf (slot-value esd 'db-writer)
468 (when (slot-boundp dsd 'db-writer)
469 (view-class-slot-db-writer dsd)))
470 (setf (slot-value esd 'db-constraints)
471 (when (slot-boundp dsd 'db-constraints)
472 (view-class-slot-db-constraints dsd)))
474 ;; I wonder if this slot option and the previous could be merged,
475 ;; so that :base and :key remain keyword options, but :db-kind
476 ;; :join becomes :db-kind (:join <db info .... >)?
478 (setf (slot-value esd 'db-info)
479 (when (slot-boundp dsd 'db-info)
480 (if (listp (view-class-slot-db-info dsd))
481 (parse-db-info (view-class-slot-db-info dsd))
482 (view-class-slot-db-info dsd))))
484 (setf (specified-type esd) (specified-type dsd))
489 (change-class esd 'view-class-effective-slot-definition
491 #+allegro (slot-definition-name dsd))
493 (setf (slot-value esd 'column)
494 (column-name-from-arg
495 (sql-escape (slot-definition-name dsd))))
497 (setf (slot-value esd 'db-info) nil)
498 (setf (slot-value esd 'db-kind)
502 (defun slotdefs-for-slots-with-class (slots class)
505 (let ((c (slotdef-for-slot-with-class s class)))
506 (if c (setf result (cons c result)))))
509 (defun slotdef-for-slot-with-class (slot class)
510 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
511 (class-slots class)))
514 (eval-when (:compile-toplevel :load-toplevel :execute)
516 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
518 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
520 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))