1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
4 ;;;; CLSQL metaclass for standard-db-objects created in the OODDL.
6 ;;;; This file is part of CLSQL.
8 ;;;; CLSQL users are granted the rights to distribute and use this software
9 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
10 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
11 ;;;; *************************************************************************
13 (in-package #:clsql-sys)
15 (eval-when (:compile-toplevel :load-toplevel :execute)
16 (when (>= (length (generic-function-lambda-list
17 (ensure-generic-function
18 'compute-effective-slot-definition)))
20 (pushnew :kmr-normal-cesd cl:*features*))
22 (when (>= (length (generic-function-lambda-list
23 (ensure-generic-function
24 'direct-slot-definition-class)))
26 (pushnew :kmr-normal-dsdc cl:*features*))
28 (when (>= (length (generic-function-lambda-list
29 (ensure-generic-function
30 'effective-slot-definition-class)))
32 (pushnew :kmr-normal-esdc cl:*features*)))
35 ;; ------------------------------------------------------------
36 ;; metaclass: view-class
38 (defclass standard-db-class (standard-class)
43 :accessor object-definition
53 :accessor view-class-qualifier
56 (:documentation "Metaclass for all CLSQL View Classes."))
58 ;;; Lispworks 4.2 and before requires special processing of extra slot and class options
60 (defvar +extra-slot-options+ '(:column :db-kind :db-type :db-reader :void-value :db-constraints
62 (defvar +extra-class-options+ '(:base-table))
65 (dolist (slot-option +extra-slot-options+)
66 (eval `(process-slot-option standard-db-class ,slot-option)))
69 (dolist (class-option +extra-class-options+)
70 (eval `(process-class-option standard-db-class ,class-option)))
72 (defmethod validate-superclass ((class standard-db-class)
73 (superclass standard-class))
76 (defun table-name-from-arg (arg)
78 (intern (sql-escape arg)))
79 ((typep arg 'sql-ident)
80 (if (symbolp (slot-value arg 'name))
81 (intern (sql-escape (slot-value arg 'name)))
82 (sql-escape (slot-value arg 'name))))
86 (defun remove-keyword-arg (arglist akey)
87 (let ((mylist arglist)
89 (labels ((pop-arg (alist)
90 (let ((arg (pop alist))
92 (unless (equal arg akey)
93 (setf newlist (append (list arg val) newlist)))
94 (when alist (pop-arg alist)))))
98 (defun set-view-table-slot (class base-table)
99 (setf (view-table class)
100 (table-name-from-arg (or (and base-table
101 (if (listp base-table)
104 (class-name class)))))
106 (defgeneric ordered-class-direct-slots (class))
107 (defmethod ordered-class-direct-slots ((self standard-db-class))
108 (let ((direct-slot-names
109 (mapcar #'slot-definition-name (class-direct-slots self)))
110 (ordered-direct-class-slots '()))
111 (dolist (slot (ordered-class-slots self))
112 (let ((slot-name (slot-definition-name slot)))
113 (when (find slot-name direct-slot-names)
114 (push slot ordered-direct-class-slots))))
115 (nreverse ordered-direct-class-slots)))
117 (defmethod initialize-instance :around ((class standard-db-class)
119 &key direct-superclasses base-table
120 qualifier normalizedp
122 (let ((root-class (find-class 'standard-db-object nil))
123 (vmc 'standard-db-class))
124 (setf (view-class-qualifier class)
127 (if (some #'(lambda (super) (typep super vmc))
130 (apply #'call-next-method
132 :direct-superclasses (append (list root-class)
134 (remove-keyword-arg all-keys :direct-superclasses)))
136 (set-view-table-slot class base-table)
137 (setf (normalizedp class) (car normalizedp))
138 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
141 (defmethod reinitialize-instance :around ((class standard-db-class)
143 &key base-table normalizedp
144 direct-superclasses qualifier
146 (let ((root-class (find-class 'standard-db-object nil))
147 (vmc 'standard-db-class))
148 (set-view-table-slot class base-table)
149 (setf (normalizedp class) (car normalizedp))
150 (setf (view-class-qualifier class)
152 (if (and root-class (not (equal class root-class)))
153 (if (some #'(lambda (super) (typep super vmc))
156 (apply #'call-next-method
158 :direct-superclasses (append (list root-class)
160 (remove-keyword-arg all-keys :direct-superclasses)))
162 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
166 (defun get-keywords (keys list)
167 (flet ((extract (key)
168 (let ((pos (position key list)))
170 (nth (1+ pos) list)))))
171 (mapcar #'extract keys)))
173 (defun describe-db-layout (class)
174 (flet ((not-db-col (col)
175 (not (member (nth 2 col) '(nil :base :key))))
177 (let ((type (slot-definition-type slot)))
180 (list (slot-value slot 'name)
182 (slot-value slot 'db-kind)
183 (and (slot-boundp slot 'column)
184 (slot-value slot 'column))))))
185 (let ((all-slots (mapcar #'frob-slot (ordered-class-slots class))))
186 (setq all-slots (remove-if #'not-db-col all-slots))
187 (setq all-slots (stable-sort all-slots #'string< :key #'car))
188 ;;(mapcar #'dink-type all-slots)
191 (defun register-metaclass (class slots)
192 (labels ((not-db-col (col)
193 (not (member (nth 2 col) '(nil :base :key))))
195 (get-keywords '(:name :type :db-kind :column) slot)))
196 (let ((all-slots (mapcar #'frob-slot slots)))
197 (setq all-slots (remove-if #'not-db-col all-slots))
198 (setq all-slots (stable-sort all-slots #'string< :key #'car))
199 (setf (object-definition class) all-slots))
201 (setf (key-slots class) (remove-if-not (lambda (slot)
202 (eql (slot-value slot 'db-kind)
204 (if (normalizedp class)
205 (ordered-class-direct-slots class)
206 (ordered-class-slots class))))))
209 (defmethod finalize-inheritance :after ((class standard-db-class))
210 (setf (key-slots class) (remove-if-not (lambda (slot)
211 (eql (slot-value slot 'db-kind)
213 (if (normalizedp class)
214 (ordered-class-direct-slots class)
215 (ordered-class-slots class)))))
217 ;; return the deepest view-class ancestor for a given view class
219 (defun base-db-class (classname)
220 (let* ((class (find-class classname))
221 (db-class (find-class 'standard-db-object)))
223 (let ((cds (class-direct-superclasses class)))
225 (error "not a db class"))
226 ((member db-class cds)
227 (return (class-name class))))
228 (setq class (car cds))))))
230 (defun db-ancestors (classname)
231 (let ((class (find-class classname))
232 (db-class (find-class 'standard-db-object)))
233 (labels ((ancestors (class)
234 (let ((scs (class-direct-superclasses class)))
235 (if (member db-class scs)
237 (append (list class) (mapcar #'ancestors scs))))))
240 (defclass view-class-slot-definition-mixin ()
242 :accessor view-class-slot-column
245 "The name of the SQL column this slot is stored in. Defaults to
248 :accessor view-class-slot-db-kind
251 ;; openmcl 0.14.2 stores the value as list in the DSD
252 ;; :type (or list keyword)
253 #-openmcl :type #-openmcl keyword
255 "The kind of DB mapping which is performed for this slot. :base
256 indicates the slot maps to an ordinary column of the DB view. :key
257 indicates that this slot corresponds to part of the unique keys for
258 this view, :join indicates ... and :virtual indicates that this slot
259 is an ordinary CLOS slot. Defaults to :base.")
261 :accessor view-class-slot-db-reader
265 "If a string, then when reading values from the DB, the string
266 will be used for a format string, with the only value being the value
267 from the database. The resulting string will be used as the slot
268 value. If a function then it will take one argument, the value from
269 the database, and return the value that should be put into the slot.")
271 :accessor view-class-slot-db-writer
275 "If a string, then when reading values from the slot for the DB,
276 the string will be used for a format string, with the only value being
277 the value of the slot. The resulting string will be used as the
278 column value in the DB. If a function then it will take one argument,
279 the value of the slot, and return the value that should be put into
282 :accessor view-class-slot-db-type
286 "A string which will be used as the type specifier for this slots
287 column definition in the database.")
289 :accessor view-class-slot-db-constraints
290 :initarg :db-constraints
293 "A keyword symbol representing a single SQL column constraint or list of such symbols.")
295 :accessor view-class-slot-void-value
299 "Value to store if the SQL value is NULL. Default is NIL.")
301 :accessor view-class-slot-db-info
303 :documentation "Description of the join.")
305 :accessor specified-type
306 :initarg specified-type
308 :documentation "Internal slot storing the :type specified by user.")
309 (autoincrement-sequence
310 :accessor view-class-slot-autoincrement-sequence
311 :initarg :autoincrement-sequence
313 :documentation "A string naming the (possibly automatically generated) sequence
314 for a slot with an :auto-increment constraint.")))
316 (defparameter *db-info-lambda-list*
322 (retrieval :immmediate)
325 (defun parse-db-info (db-info-list)
327 (&key join-class home-key key-join foreign-key (delete-rule nil)
328 (target-slot nil) (retrieval :deferred) (set t))
330 (let ((ih (make-hash-table :size 6)))
332 (setf (gethash :join-class ih) join-class)
333 (error "Must specify :join-class in :db-info"))
335 (setf (gethash :home-key ih) home-key)
336 (error "Must specify :home-key in :db-info"))
338 (setf (gethash :delete-rule ih) delete-rule))
340 (setf (gethash :foreign-key ih) foreign-key)
341 (error "Must specify :foreign-key in :db-info"))
343 (setf (gethash :key-join ih) t))
345 (setf (gethash :target-slot ih) target-slot))
347 (setf (gethash :set ih) set))
350 (setf (gethash :retrieval ih) retrieval)
351 (if (eql retrieval :immediate)
352 (setf (gethash :set ih) nil))))
355 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
356 standard-direct-slot-definition)
359 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
360 standard-effective-slot-definition)
363 (defmethod direct-slot-definition-class ((class standard-db-class)
364 #+kmr-normal-dsdc &rest
366 (declare (ignore initargs))
367 (find-class 'view-class-direct-slot-definition))
369 (defmethod effective-slot-definition-class ((class standard-db-class)
370 #+kmr-normal-esdc &rest
372 (declare (ignore initargs))
373 (find-class 'view-class-effective-slot-definition))
376 (when (not (symbol-function 'compute-class-precedence-list))
378 (defun compute-class-precedence-list (class)
379 (class-precedence-list class))))
381 #-mop-slot-order-reversed
382 (defmethod compute-slots ((class standard-db-class))
383 "Need to sort order of class slots so they are the same across
385 (let ((slots (call-next-method))
388 (dolist (c (compute-class-precedence-list class))
389 (dolist (s (class-direct-slots c))
390 (let ((name (slot-definition-name s)))
391 (unless (find name desired-sequence)
392 (push name desired-sequence)))))
393 (dolist (desired desired-sequence)
394 (let ((slot (find desired slots :key #'slot-definition-name)))
396 (push slot output-slots)))
399 (defun compute-lisp-type-from-specified-type (specified-type db-constraints)
400 "Computes the Lisp type for a user-specified type."
403 ((consp specified-type)
404 (let* ((first (first specified-type))
405 (name (etypecase first
406 (symbol (symbol-name first))
409 ((or (string-equal name "string")
410 (string-equal name "varchar")
411 (string-equal name "char"))
415 ((eq (ensure-keyword specified-type) :bigint)
417 ((eq (ensure-keyword specified-type) :char)
419 ((eq (ensure-keyword specified-type) :varchar)
423 (if (and type (not (member :not-null (listify db-constraints))))
427 ;; Compute the slot definition for slots in a view-class. Figures out
428 ;; what kind of database value (if any) is stored there, generates and
429 ;; verifies the column name.
431 (declaim (inline delistify))
432 (defun delistify (list)
433 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
438 (declaim (inline delistify-dsd))
439 ;; there is an :after method below too
440 (defmethod initialize-instance :around
441 ((obj view-class-direct-slot-definition)
442 &rest initargs &key db-constraints db-kind type &allow-other-keys)
443 (when (and (not db-kind) (member :primary-key (listify db-constraints)))
444 (warn "Slot ~S constrained to be :primary-key, but not marked as :db-kind :key"
445 (slot-definition-name obj)))
446 (apply #'call-next-method obj
448 :type (if (and (eql db-kind :virtual) (null type))
450 (compute-lisp-type-from-specified-type
451 type db-constraints))
454 (defun compute-column-name (arg)
455 (database-identifier arg nil))
457 (defmethod initialize-instance :after
458 ((obj view-class-direct-slot-definition)
459 &key &allow-other-keys)
460 (setf (view-class-slot-column obj) (compute-column-name obj)))
462 (defmethod compute-effective-slot-definition ((class standard-db-class)
463 #+kmr-normal-cesd slot-name
465 #+kmr-normal-cesd (declare (ignore slot-name))
467 ;; KMR: store the user-specified type and then compute
468 ;; real Lisp type and store it
469 (let ((dsd (car direct-slots)))
470 (let ((esd (call-next-method)))
472 (view-class-slot-definition-mixin
473 (setf (slot-value esd 'column) (compute-column-name dsd))
475 (setf (slot-value esd 'db-type)
476 (when (slot-boundp dsd 'db-type)
478 (view-class-slot-db-type dsd))))
480 (setf (slot-value esd 'void-value)
482 (view-class-slot-void-value dsd)))
484 ;; :db-kind slot value defaults to :base (store slot value in
487 (setf (slot-value esd 'db-kind)
488 (if (slot-boundp dsd 'db-kind)
489 (delistify-dsd (view-class-slot-db-kind dsd))
492 (setf (slot-value esd 'db-reader)
493 (when (slot-boundp dsd 'db-reader)
494 (delistify-dsd (view-class-slot-db-reader dsd))))
495 (setf (slot-value esd 'db-writer)
496 (when (slot-boundp dsd 'db-writer)
497 (delistify-dsd (view-class-slot-db-writer dsd))))
498 (setf (slot-value esd 'db-constraints)
499 (when (slot-boundp dsd 'db-constraints)
500 (delistify-dsd (view-class-slot-db-constraints dsd))))
502 ;; I wonder if this slot option and the previous could be merged,
503 ;; so that :base and :key remain keyword options, but :db-kind
504 ;; :join becomes :db-kind (:join <db info .... >)?
506 (setf (slot-value esd 'db-info)
507 (when (slot-boundp dsd 'db-info)
508 (let ((dsd-info (view-class-slot-db-info dsd)))
512 ((and (listp dsd-info) (> (length dsd-info) 1)
513 (atom (car dsd-info)))
514 (parse-db-info dsd-info))
515 ((and (listp dsd-info) (= 1 (length dsd-info))
516 (listp (car dsd-info)))
517 (parse-db-info (car dsd-info)))))))
519 (setf (specified-type esd)
520 (delistify-dsd (specified-type dsd)))
521 ;; In older SBCL's the type-check-function is computed at
522 ;; defclass expansion, which is too early for the CLSQL type
523 ;; conversion to take place. This gets rid of it. It's ugly
524 ;; but it's better than nothing -wcp10/4/10.
525 #+(and sbcl #.(cl:if (cl:and (cl:find-package :sb-pcl)
526 (cl:find-symbol "%TYPE-CHECK-FUNCTION" :sb-pcl))
528 (setf (slot-value esd 'sb-pcl::%type-check-function) nil)
533 (unless (typep esd 'view-class-effective-slot-definition)
534 (warn "Non view-class-direct-slot object with non-view-class-effective-slot-definition in compute-effective-slot-definition")
536 (let ((type-predicate #+openmcl (slot-value esd 'ccl::type-predicate)))
537 #-openmcl (declare (ignore type-predicate))
538 #-(or clisp sbcl) (change-class esd 'view-class-effective-slot-definition
540 #+allegro (slot-definition-name dsd))
541 #+openmcl (setf (slot-value esd 'ccl::type-predicate)
544 ;; has no column name if it is not a database column
545 (setf (slot-value esd 'column) nil)
546 (setf (slot-value esd 'db-info) nil)
547 (setf (slot-value esd 'db-kind) :virtual)
548 (setf (specified-type esd) (slot-definition-type dsd)))
552 (defun slotdefs-for-slots-with-class (slots class)
555 (let ((c (slotdef-for-slot-with-class s class)))
556 (if c (setf result (cons c result)))))
559 (defun slotdef-for-slot-with-class (slot class)
561 (standard-slot-definition slot)
563 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
564 (class-slots class)))))
567 (eval-when (:compile-toplevel :load-toplevel :execute)
569 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
571 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
573 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))
576 (defmethod database-identifier ( (name standard-db-class)
577 &optional database find-class-p)
578 "the majority of this function is in expressions.lisp
579 this is here to make loading be less painful (try-recompiles) in SBCL"
580 (database-identifier (view-table name) database))
582 (defmethod database-identifier ((name view-class-slot-definition-mixin)
583 &optional database find-class-p)
585 (if (slot-boundp name 'column)
586 (delistify-dsd (view-class-slot-column name))
587 (slot-definition-name name))
590 (defun find-standard-db-class (name &aux cls)
591 (and (setf cls (ignore-errors (find-class name)))
592 (typep cls 'standard-db-class)