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 (defmethod initialize-instance :around ((class standard-db-class)
108 &key direct-superclasses base-table
109 qualifier normalizedp
111 (let ((root-class (find-class 'standard-db-object nil))
112 (vmc 'standard-db-class))
113 (setf (view-class-qualifier class)
116 (if (some #'(lambda (super) (typep super vmc))
119 (apply #'call-next-method
121 :direct-superclasses (append (list root-class)
123 (remove-keyword-arg all-keys :direct-superclasses)))
125 (set-view-table-slot class base-table)
126 (setf (normalizedp class) (car normalizedp))
127 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
130 (defmethod reinitialize-instance :around ((class standard-db-class)
132 &key base-table normalizedp
133 direct-superclasses qualifier
135 (let ((root-class (find-class 'standard-db-object nil))
136 (vmc 'standard-db-class))
137 (set-view-table-slot class base-table)
138 (setf (normalizedp class) (car normalizedp))
139 (setf (view-class-qualifier class)
141 (if (and root-class (not (equal class root-class)))
142 (if (some #'(lambda (super) (typep super vmc))
145 (apply #'call-next-method
147 :direct-superclasses (append (list root-class)
149 (remove-keyword-arg all-keys :direct-superclasses)))
151 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
156 (defun get-keywords (keys list)
157 (flet ((extract (key)
158 (let ((pos (position key list)))
160 (nth (1+ pos) list)))))
161 (mapcar #'extract keys)))
163 (defun describe-db-layout (class)
164 (flet ((not-db-col (col)
165 (not (member (nth 2 col) '(nil :base :key))))
167 (let ((type (slot-definition-type slot)))
170 (list (slot-value slot 'name)
172 (slot-value slot 'db-kind)
173 (and (slot-boundp slot 'column)
174 (slot-value slot 'column))))))
175 (let ((all-slots (mapcar #'frob-slot (ordered-class-slots class))))
176 (setq all-slots (remove-if #'not-db-col all-slots))
177 (setq all-slots (stable-sort all-slots #'string< :key #'car))
178 ;;(mapcar #'dink-type all-slots)
181 (defun register-metaclass (class slots)
182 (labels ((not-db-col (col)
183 (not (member (nth 2 col) '(nil :base :key))))
185 (get-keywords '(:name :type :db-kind :column) slot)))
186 (let ((all-slots (mapcar #'frob-slot slots)))
187 (setq all-slots (remove-if #'not-db-col all-slots))
188 (setq all-slots (stable-sort all-slots #'string< :key #'car))
189 (setf (object-definition class) all-slots))
191 (setf (key-slots class) (remove-if-not (lambda (slot)
192 (eql (slot-value slot 'db-kind)
194 (slots-for-possibly-normalized-class class)))))
197 (defmethod finalize-inheritance :after ((class standard-db-class))
198 (setf (key-slots class) (remove-if-not (lambda (slot)
199 (eql (slot-value slot 'db-kind)
201 (slots-for-possibly-normalized-class class))))
203 ;; return the deepest view-class ancestor for a given view class
205 (defun base-db-class (classname)
206 (let* ((class (find-class classname))
207 (db-class (find-class 'standard-db-object)))
209 (let ((cds (class-direct-superclasses class)))
211 (error "not a db class"))
212 ((member db-class cds)
213 (return (class-name class))))
214 (setq class (car cds))))))
216 (defun db-ancestors (classname)
217 (let ((class (find-class classname))
218 (db-class (find-class 'standard-db-object)))
219 (labels ((ancestors (class)
220 (let ((scs (class-direct-superclasses class)))
221 (if (member db-class scs)
223 (append (list class) (mapcar #'ancestors scs))))))
226 (defclass view-class-slot-definition-mixin ()
228 :accessor view-class-slot-column
231 "The name of the SQL column this slot is stored in. Defaults to
234 :accessor view-class-slot-db-kind
237 ;; openmcl 0.14.2 stores the value as list in the DSD
238 ;; :type (or list keyword)
239 #-openmcl :type #-openmcl keyword
241 "The kind of DB mapping which is performed for this slot. :base
242 indicates the slot maps to an ordinary column of the DB view. :key
243 indicates that this slot corresponds to part of the unique keys for
244 this view, :join indicates ... and :virtual indicates that this slot
245 is an ordinary CLOS slot. Defaults to :base.")
247 :accessor view-class-slot-db-reader
251 "If a string, then when reading values from the DB, the string
252 will be used for a format string, with the only value being the value
253 from the database. The resulting string will be used as the slot
254 value. If a function then it will take one argument, the value from
255 the database, and return the value that should be put into the slot.")
257 :accessor view-class-slot-db-writer
261 "If a string, then when reading values from the slot for the DB,
262 the string will be used for a format string, with the only value being
263 the value of the slot. The resulting string will be used as the
264 column value in the DB. If a function then it will take one argument,
265 the value of the slot, and return the value that should be put into
268 :accessor view-class-slot-db-type
272 "A string which will be used as the type specifier for this slots
273 column definition in the database.")
275 :accessor view-class-slot-db-constraints
276 :initarg :db-constraints
279 "A keyword symbol representing a single SQL column constraint or list of such symbols.")
281 :accessor view-class-slot-void-value
285 "Value to store if the SQL value is NULL. Default is NIL.")
287 :accessor view-class-slot-db-info
289 :documentation "Description of the join.")
291 :accessor specified-type
292 :initarg specified-type
294 :documentation "Internal slot storing the :type specified by user.")
295 (autoincrement-sequence
296 :accessor view-class-slot-autoincrement-sequence
297 :initarg :autoincrement-sequence
299 :documentation "A string naming the (possibly automatically generated) sequence
300 for a slot with an :auto-increment constraint.")))
302 (defparameter *db-info-lambda-list*
308 (retrieval :immmediate)
311 (defun parse-db-info (db-info-list)
313 (&key join-class home-key key-join foreign-key (delete-rule nil)
314 (target-slot nil) (retrieval :deferred) (set t))
316 (let ((ih (make-hash-table :size 6)))
318 (setf (gethash :join-class ih) join-class)
319 (error "Must specify :join-class in :db-info"))
321 (setf (gethash :home-key ih) home-key)
322 (error "Must specify :home-key in :db-info"))
324 (setf (gethash :delete-rule ih) delete-rule))
326 (setf (gethash :foreign-key ih) foreign-key)
327 (error "Must specify :foreign-key in :db-info"))
329 (setf (gethash :key-join ih) t))
331 (setf (gethash :target-slot ih) target-slot))
333 (setf (gethash :set ih) set))
336 (setf (gethash :retrieval ih) retrieval)
337 (if (eql retrieval :immediate)
338 (setf (gethash :set ih) nil))))
341 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
342 standard-direct-slot-definition)
345 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
346 standard-effective-slot-definition)
349 (defmethod direct-slot-definition-class ((class standard-db-class)
350 #+kmr-normal-dsdc &rest
352 (declare (ignore initargs))
353 (find-class 'view-class-direct-slot-definition))
355 (defmethod effective-slot-definition-class ((class standard-db-class)
356 #+kmr-normal-esdc &rest
358 (declare (ignore initargs))
359 (find-class 'view-class-effective-slot-definition))
362 (when (not (symbol-function 'compute-class-precedence-list))
364 (defun compute-class-precedence-list (class)
365 (class-precedence-list class))))
367 #-mop-slot-order-reversed
368 (defmethod compute-slots ((class standard-db-class))
369 "Need to sort order of class slots so they are the same across
371 (let ((slots (call-next-method))
374 (dolist (c (compute-class-precedence-list class))
375 (dolist (s (class-direct-slots c))
376 (let ((name (slot-definition-name s)))
377 (unless (find name desired-sequence)
378 (push name desired-sequence)))))
379 (dolist (desired desired-sequence)
380 (let ((slot (find desired slots :key #'slot-definition-name)))
382 (push slot output-slots)))
385 (defun compute-lisp-type-from-specified-type (specified-type db-constraints)
386 "Computes the Lisp type for a user-specified type."
389 ((consp specified-type)
390 (let* ((first (first specified-type))
391 (name (etypecase first
392 (symbol (symbol-name first))
395 ((or (string-equal name "string")
396 (string-equal name "varchar")
397 (string-equal name "char"))
401 ((eq (ensure-keyword specified-type) :bigint)
403 ((eq (ensure-keyword specified-type) :char)
405 ((eq (ensure-keyword specified-type) :varchar)
409 (if (and type (not (member :not-null (listify db-constraints))))
413 ;; Compute the slot definition for slots in a view-class. Figures out
414 ;; what kind of database value (if any) is stored there, generates and
415 ;; verifies the column name.
417 (declaim (inline delistify))
418 (defun delistify (list)
419 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
424 (declaim (inline delistify-dsd))
425 ;; there is an :after method below too
426 (defmethod initialize-instance :around
427 ((obj view-class-direct-slot-definition)
428 &rest initargs &key db-constraints db-kind type &allow-other-keys)
429 (when (and (not db-kind) (member :primary-key (listify db-constraints)))
430 (warn "Slot ~S constrained to be :primary-key, but not marked as :db-kind :key"
431 (slot-definition-name obj)))
432 (apply #'call-next-method obj
434 :type (if (and (eql db-kind :virtual) (null type))
436 (compute-lisp-type-from-specified-type
437 type db-constraints))
440 (defun compute-column-name (arg)
441 (database-identifier arg nil))
443 (defun %convert-db-info-to-hash (slot-def)
444 ;; I wonder if this slot option and the previous could be merged,
445 ;; so that :base and :key remain keyword options, but :db-kind
446 ;; :join becomes :db-kind (:join <db info .... >)?
447 (setf (slot-value slot-def 'db-info)
448 (when (slot-boundp slot-def 'db-info)
449 (let ((info (view-class-slot-db-info slot-def)))
454 (cond ((and (> (length info) 1)
456 (parse-db-info info))
457 ((and (= 1 (length info))
459 (parse-db-info (car info)))
462 (defmethod initialize-instance :after
463 ((obj view-class-direct-slot-definition)
464 &key &allow-other-keys)
465 (setf (view-class-slot-column obj) (compute-column-name obj)
466 (view-class-slot-autoincrement-sequence obj)
468 (view-class-slot-autoincrement-sequence obj)))
469 (%convert-db-info-to-hash obj))
471 (defmethod compute-effective-slot-definition ((class standard-db-class)
472 #+kmr-normal-cesd slot-name
474 #+kmr-normal-cesd (declare (ignore slot-name))
476 ;; KMR: store the user-specified type and then compute
477 ;; real Lisp type and store it
478 (let ((dsd (car direct-slots)))
479 (let ((esd (call-next-method)))
481 (view-class-slot-definition-mixin
482 (setf (slot-value esd 'column) (compute-column-name dsd))
485 ((safe-copy-value (name &optional default)
486 (let ((fn (intern (format nil "~A~A" 'view-class-slot- name ))))
487 `(setf (slot-value esd ',name)
488 (or (when (slot-boundp dsd ',name)
489 (delistify-dsd (,fn dsd)))
491 (safe-copy-value autoincrement-sequence)
492 (safe-copy-value db-type)
493 (safe-copy-value void-value)
494 (safe-copy-value db-reader)
495 (safe-copy-value db-writer)
496 ;; :db-kind slot value defaults to :base (store slot value in
498 (safe-copy-value db-kind :base)
499 (safe-copy-value db-constraints)
500 (safe-copy-value db-info)
501 (%convert-db-info-to-hash esd))
503 (setf (specified-type esd)
504 (delistify-dsd (specified-type dsd)))
505 ;; In older SBCL's the type-check-function is computed at
506 ;; defclass expansion, which is too early for the CLSQL type
507 ;; conversion to take place. This gets rid of it. It's ugly
508 ;; but it's better than nothing -wcp10/4/10.
509 #+(and sbcl #.(cl:if (cl:and (cl:find-package :sb-pcl)
510 (cl:find-symbol "%TYPE-CHECK-FUNCTION" :sb-pcl))
512 (setf (slot-value esd 'sb-pcl::%type-check-function) nil)
517 (unless (typep esd 'view-class-effective-slot-definition)
518 (warn "Non view-class-direct-slot object with non-view-class-effective-slot-definition in compute-effective-slot-definition")
520 (let ((type-predicate #+openmcl (slot-value esd 'ccl::type-predicate)))
521 #-openmcl (declare (ignore type-predicate))
522 #-(or clisp sbcl) (change-class esd 'view-class-effective-slot-definition
524 #+allegro (slot-definition-name dsd))
525 #+openmcl (setf (slot-value esd 'ccl::type-predicate)
528 ;; has no column name if it is not a database column
529 (setf (slot-value esd 'column) nil)
530 (setf (slot-value esd 'db-info) nil)
531 (setf (slot-value esd 'db-kind) :virtual)
532 (setf (specified-type esd) (slot-definition-type dsd)))
536 (defun slotdefs-for-slots-with-class (slots class)
539 (let ((c (slotdef-for-slot-with-class s class)))
540 (if c (setf result (cons c result)))))
543 (defun slotdef-for-slot-with-class (slot class)
545 (standard-slot-definition slot)
546 (symbol (find-slot-by-name class slot))))
549 (eval-when (:compile-toplevel :load-toplevel :execute)
551 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
553 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
555 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))
558 (defmethod database-identifier ( (name standard-db-class)
559 &optional database find-class-p)
560 "the majority of this function is in expressions.lisp
561 this is here to make loading be less painful (try-recompiles) in SBCL"
562 (declare (ignore find-class-p))
563 (database-identifier (view-table name) database))
565 (defmethod database-identifier ((name view-class-slot-definition-mixin)
566 &optional database find-class-p)
567 (declare (ignore find-class-p))
569 (if (slot-boundp name 'column)
570 (delistify-dsd (view-class-slot-column name))
571 (slot-definition-name name))
574 (defun find-standard-db-class (name &aux cls)
575 (and (setf cls (ignore-errors (find-class name)))
576 (typep cls 'standard-db-class)
579 (defun slots-for-possibly-normalized-class (class)
580 "Get the slots for this class, if normalized this is only the direct slots
581 otherwiese its all the slots"
582 (if (normalizedp class)
583 (ordered-class-direct-slots class)
584 (ordered-class-slots class)))
587 (defun key-slot-p (slot-def)
588 "takes a slot def and returns whether or not it is a key"
589 (eql :key (view-class-slot-db-kind slot-def)))
591 (defun join-slot-p (slot-def)
592 "takes a slot def and returns whether or not it is a join slot"
593 (eql :join (view-class-slot-db-kind slot-def)))
595 (defun join-slot-info-value (slot-def key)
596 "Get the join-slot db-info value associated with a key"
597 (when (join-slot-p slot-def)
598 (let ((dbi (view-class-slot-db-info slot-def)))
599 (when dbi (gethash key dbi)))))
601 (defun join-slot-retrieval-method (slot-def)
602 "if this is a join slot return the retrieval param in the db-info"
603 (join-slot-info-value slot-def :retrieval))
605 (defun join-slot-class-name (slot-def)
606 "get the join class name for a given join slot"
607 (join-slot-info-value slot-def :join-class))
609 (defun join-slot-class (slot-def)
610 "Get the join class for a given join slot"
611 (let ((c (join-slot-class-name slot-def)))
612 (when c (find-class c))))
614 (defun key-or-base-slot-p (slot-def)
615 "takes a slot def and returns whether or not it is a key"
616 (member (view-class-slot-db-kind slot-def) '(:key :base)))
618 (defun direct-normalized-slot-p (class slot-name)
619 "Is this a normalized class and if so is the slot one of our direct slots?"
620 (setf slot-name (to-slot-name slot-name))
621 (and (typep class 'standard-db-class)
623 (member slot-name (ordered-class-direct-slots class)
624 :key #'slot-definition-name)))
626 (defun not-direct-normalized-slot-p (class slot-name)
627 "Is this a normalized class and if so is the slot not one of our direct slots?"
628 (setf slot-name (to-slot-name slot-name))
629 (and (typep class 'standard-db-class)
631 (not (member slot-name (ordered-class-direct-slots class)
632 :key #'slot-definition-name))))
634 (defun slot-has-default-p (slot)
635 "returns nil if the slot does not have a default constraint"
637 (when (typep slot '(or view-class-direct-slot-definition
638 view-class-effective-slot-definition))
639 (listify (view-class-slot-db-constraints slot)))))
640 (member :default constraints)))