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 "Metaclass for all CLSQL View Classes."))
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 (eval `(process-slot-option standard-db-class ,slot-option)))
68 (dolist (class-option +extra-class-options+)
69 (eval `(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))
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 'standard-db-class))
111 (setf (view-class-qualifier class)
114 (if (some #'(lambda (super) (typep super vmc))
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 '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 (some #'(lambda (super) (typep super vmc))
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-definition-type slot)))
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))
196 (setf (key-slots class) (remove-if-not (lambda (slot)
197 (eql (slot-value slot 'db-kind)
199 (ordered-class-slots class)))))
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
242 ;; openmcl 0.14.2 stores the value as list in the DSD
243 ;; :type (or list keyword)
244 #-openmcl :type #-openmcl keyword
246 "The kind of DB mapping which is performed for this slot. :base
247 indicates the slot maps to an ordinary column of the DB view. :key
248 indicates that this slot corresponds to part of the unique keys for
249 this view, :join indicates ... and :virtual indicates that this slot
250 is an ordinary CLOS slot. Defaults to :base.")
252 :accessor view-class-slot-db-reader
256 "If a string, then when reading values from the DB, the string
257 will be used for a format string, with the only value being the value
258 from the database. The resulting string will be used as the slot
259 value. If a function then it will take one argument, the value from
260 the database, and return the value that should be put into the slot.")
262 :accessor view-class-slot-db-writer
266 "If a string, then when reading values from the slot for the DB,
267 the string will be used for a format string, with the only value being
268 the value of the slot. The resulting string will be used as the
269 column value in the DB. If a function then it will take one argument,
270 the value of the slot, and return the value that should be put into
273 :accessor view-class-slot-db-type
277 "A string which will be used as the type specifier for this slots
278 column definition in the database.")
280 :accessor view-class-slot-db-constraints
281 :initarg :db-constraints
284 "A keyword symbol representing a single SQL column constraint or list of such symbols.")
286 :accessor view-class-slot-void-value
290 "Value to store if the SQL value is NULL. Default is NIL.")
292 :accessor view-class-slot-db-info
294 :documentation "Description of the join.")
296 :accessor specified-type
298 :documentation "KMR: Internal slot storing the :type specified by user.")))
300 (defparameter *db-info-lambda-list*
306 (retrieval :immmediate)
309 (defun parse-db-info (db-info-list)
311 (&key join-class home-key key-join foreign-key (delete-rule nil)
312 (target-slot nil) (retrieval :deferred) (set t))
314 (let ((ih (make-hash-table :size 6)))
316 (setf (gethash :join-class ih) join-class)
317 (error "Must specify :join-class in :db-info"))
319 (setf (gethash :home-key ih) home-key)
320 (error "Must specify :home-key in :db-info"))
322 (setf (gethash :delete-rule ih) delete-rule))
324 (setf (gethash :foreign-key ih) foreign-key)
325 (error "Must specify :foreign-key in :db-info"))
327 (setf (gethash :key-join ih) t))
329 (setf (gethash :target-slot ih) target-slot))
331 (setf (gethash :set ih) set))
334 (setf (gethash :retrieval ih) retrieval)
335 (if (eql retrieval :immediate)
336 (setf (gethash :set ih) nil))))
339 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
340 standard-direct-slot-definition)
343 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
344 standard-effective-slot-definition)
347 (defmethod direct-slot-definition-class ((class standard-db-class)
348 #+kmr-normal-dsdc &rest
350 (declare (ignore initargs))
351 (find-class 'view-class-direct-slot-definition))
353 (defmethod effective-slot-definition-class ((class standard-db-class)
354 #+kmr-normal-esdc &rest
356 (declare (ignore initargs))
357 (find-class 'view-class-effective-slot-definition))
360 (when (not (symbol-function 'compute-class-precedence-list))
362 (defun compute-class-precedence-list (class)
363 (class-precedence-list class))))
365 #-mop-slot-order-reversed
366 (defmethod compute-slots ((class standard-db-class))
367 "Need to sort order of class slots so they are the same across
369 (let ((slots (call-next-method))
372 (dolist (c (compute-class-precedence-list class))
373 (dolist (s (class-direct-slots c))
374 (let ((name (slot-definition-name s)))
375 (unless (find name desired-sequence)
376 (push name desired-sequence)))))
377 (dolist (desired desired-sequence)
378 (let ((slot (find desired slots :key #'slot-definition-name)))
380 (push slot output-slots)))
383 (defun compute-lisp-type-from-slot-specification (slotd specified-type)
384 "Computes the Lisp type for a user-specified type. Needed for OpenMCL
385 which does type checking before storing a value in a slot."
386 ;; This function is called after the base compute-effective-slots is called.
387 ;; OpenMCL sets the type-predicate based on the initial value of the slots type.
388 ;; so we have to override the type-predicates here
391 ((consp specified-type)
393 ((and (symbolp (car specified-type))
394 (string-equal (symbol-name (car specified-type)) "string"))
396 ((and (symbolp (car specified-type))
397 (string-equal (symbol-name (car specified-type)) "varchar"))
399 ((and (symbolp (car specified-type))
400 (string-equal (symbol-name (car specified-type)) "char"))
404 ((eq (ensure-keyword specified-type) :bigint)
406 ((eq (ensure-keyword specified-type) :char)
408 ((eq (ensure-keyword specified-type) :varchar)
412 (constraints (slot-value slotd 'db-constraints)))
413 (if (and type (not (member :not-null (listify constraints))))
417 ;; Compute the slot definition for slots in a view-class. Figures out
418 ;; what kind of database value (if any) is stored there, generates and
419 ;; verifies the column name.
421 (declaim (inline delistify))
422 (defun delistify (list)
423 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
428 (declaim (inline delistify-dsd))
429 (defun delistify-dsd (list)
430 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
431 (if (and (listp list) (null (cdr list)))
435 (defvar *impl-type-attrib-name* #-clisp 'type #+clisp 'clos::$type)
437 (defmethod compute-effective-slot-definition ((class standard-db-class)
438 #+kmr-normal-cesd slot-name
440 #+kmr-normal-cesd (declare (ignore slot-name))
442 ;; KMR: store the user-specified type and then compute
443 ;; real Lisp type and store it
444 (let ((dsd (car direct-slots)))
445 (when (and (typep dsd 'view-class-slot-definition-mixin)
446 (null (specified-type dsd)))
447 (setf (specified-type dsd)
448 (slot-definition-type dsd))
449 (setf #-(or clisp sbcl) (slot-value dsd 'type)
450 #+(or clisp sbcl) (slot-definition-type dsd)
451 (compute-lisp-type-from-slot-specification
452 dsd (slot-definition-type dsd))))
454 (let ((esd (call-next-method)))
456 (view-class-slot-definition-mixin
457 ;; Use the specified :column argument if it is supplied, otherwise
458 ;; the column slot is filled in with the slot-name, but transformed
459 ;; to be sql safe, - to _ and such.
460 (setf (slot-value esd 'column)
461 (column-name-from-arg
462 (if (slot-boundp dsd 'column)
463 (delistify-dsd (view-class-slot-column dsd))
464 (column-name-from-arg
465 (sql-escape (slot-definition-name dsd))))))
467 (setf (slot-value esd 'db-type)
468 (when (slot-boundp dsd 'db-type)
470 (view-class-slot-db-type dsd))))
472 (setf (slot-value esd 'void-value)
474 (view-class-slot-void-value dsd)))
476 ;; :db-kind slot value defaults to :base (store slot value in
479 (setf (slot-value esd 'db-kind)
480 (if (slot-boundp dsd 'db-kind)
481 (delistify-dsd (view-class-slot-db-kind dsd))
484 (setf (slot-value esd 'db-reader)
485 (when (slot-boundp dsd 'db-reader)
486 (delistify-dsd (view-class-slot-db-reader dsd))))
487 (setf (slot-value esd 'db-writer)
488 (when (slot-boundp dsd 'db-writer)
489 (delistify-dsd (view-class-slot-db-writer dsd))))
490 (setf (slot-value esd 'db-constraints)
491 (when (slot-boundp dsd 'db-constraints)
492 (delistify-dsd (view-class-slot-db-constraints dsd))))
494 ;; I wonder if this slot option and the previous could be merged,
495 ;; so that :base and :key remain keyword options, but :db-kind
496 ;; :join becomes :db-kind (:join <db info .... >)?
498 (setf (slot-value esd 'db-info)
499 (when (slot-boundp dsd 'db-info)
500 (let ((dsd-info (view-class-slot-db-info dsd)))
504 ((and (listp dsd-info) (> (length dsd-info) 1)
505 (atom (car dsd-info)))
506 (parse-db-info dsd-info))
507 ((and (listp dsd-info) (= 1 (length dsd-info))
508 (listp (car dsd-info)))
509 (parse-db-info (car dsd-info)))))))
511 (setf (specified-type esd)
512 (delistify-dsd (specified-type dsd)))
517 (let ((type-predicate #+openmcl (slot-value esd 'ccl::type-predicate)))
518 #-openmcl (declare (ignore type-predicate))
519 #-(or clisp sbcl) (change-class esd 'view-class-effective-slot-definition
521 #+allegro (slot-definition-name dsd))
522 #+openmcl (setf (slot-value esd 'ccl::type-predicate)
525 (setf (slot-value esd 'column)
526 (column-name-from-arg
527 (sql-escape (slot-definition-name dsd))))
529 (setf (slot-value esd 'db-info) nil)
530 (setf (slot-value esd 'db-kind) :virtual)
531 (setf (specified-type esd) (slot-definition-type dsd)))
535 (defun slotdefs-for-slots-with-class (slots class)
538 (let ((c (slotdef-for-slot-with-class s class)))
539 (if c (setf result (cons c result)))))
542 (defun slotdef-for-slot-with-class (slot class)
543 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
544 (class-slots class)))
547 (eval-when (:compile-toplevel :load-toplevel :execute)
549 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
551 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
553 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))