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)
77 (intern (sql-escape arg)))
78 ((typep arg 'sql-ident)
79 (if (symbolp (slot-value arg 'name))
80 (intern (sql-escape (slot-value arg 'name)))
81 (sql-escape (slot-value arg 'name))))
85 (defun column-name-from-arg (arg)
88 ((typep arg 'sql-ident)
89 (slot-value arg 'name))
91 (intern (symbol-name-default-case arg)))))
94 (defun remove-keyword-arg (arglist akey)
95 (let ((mylist arglist)
97 (labels ((pop-arg (alist)
98 (let ((arg (pop alist))
100 (unless (equal arg akey)
101 (setf newlist (append (list arg val) newlist)))
102 (when alist (pop-arg alist)))))
106 (defun set-view-table-slot (class base-table)
107 (setf (view-table class)
108 (table-name-from-arg (or (and base-table
109 (if (listp base-table)
112 (class-name class)))))
114 (defmethod initialize-instance :around ((class standard-db-class)
116 &key direct-superclasses base-table
119 (let ((root-class (find-class 'standard-db-object nil))
120 (vmc 'standard-db-class))
121 (setf (view-class-qualifier class)
124 (if (some #'(lambda (super) (typep super vmc))
127 (apply #'call-next-method
129 :direct-superclasses (append (list root-class)
131 (remove-keyword-arg all-keys :direct-superclasses)))
133 (set-view-table-slot class base-table)
134 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
137 (defmethod reinitialize-instance :around ((class standard-db-class)
140 direct-superclasses qualifier
142 (let ((root-class (find-class 'standard-db-object nil))
143 (vmc 'standard-db-class))
144 (set-view-table-slot class base-table)
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
297 :initarg specified-type
299 :documentation "Internal slot storing the :type specified by user.")))
301 (defparameter *db-info-lambda-list*
307 (retrieval :immmediate)
310 (defun parse-db-info (db-info-list)
312 (&key join-class home-key key-join foreign-key (delete-rule nil)
313 (target-slot nil) (retrieval :deferred) (set t))
315 (let ((ih (make-hash-table :size 6)))
317 (setf (gethash :join-class ih) join-class)
318 (error "Must specify :join-class in :db-info"))
320 (setf (gethash :home-key ih) home-key)
321 (error "Must specify :home-key in :db-info"))
323 (setf (gethash :delete-rule ih) delete-rule))
325 (setf (gethash :foreign-key ih) foreign-key)
326 (error "Must specify :foreign-key in :db-info"))
328 (setf (gethash :key-join ih) t))
330 (setf (gethash :target-slot ih) target-slot))
332 (setf (gethash :set ih) set))
335 (setf (gethash :retrieval ih) retrieval)
336 (if (eql retrieval :immediate)
337 (setf (gethash :set ih) nil))))
340 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
341 standard-direct-slot-definition)
344 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
345 standard-effective-slot-definition)
348 (defmethod direct-slot-definition-class ((class standard-db-class)
349 #+kmr-normal-dsdc &rest
351 (declare (ignore initargs))
352 (find-class 'view-class-direct-slot-definition))
354 (defmethod effective-slot-definition-class ((class standard-db-class)
355 #+kmr-normal-esdc &rest
357 (declare (ignore initargs))
358 (find-class 'view-class-effective-slot-definition))
361 (when (not (symbol-function 'compute-class-precedence-list))
363 (defun compute-class-precedence-list (class)
364 (class-precedence-list class))))
366 #-mop-slot-order-reversed
367 (defmethod compute-slots ((class standard-db-class))
368 "Need to sort order of class slots so they are the same across
370 (let ((slots (call-next-method))
373 (dolist (c (compute-class-precedence-list class))
374 (dolist (s (class-direct-slots c))
375 (let ((name (slot-definition-name s)))
376 (unless (find name desired-sequence)
377 (push name desired-sequence)))))
378 (dolist (desired desired-sequence)
379 (let ((slot (find desired slots :key #'slot-definition-name)))
381 (push slot output-slots)))
384 (defun compute-lisp-type-from-specified-type (specified-type db-constraints)
385 "Computes the Lisp type for a user-specified type."
388 ((consp specified-type)
389 (let* ((first (first specified-type))
390 (name (etypecase first
391 (symbol (symbol-name first))
394 ((or (string-equal name "string")
395 (string-equal name "varchar")
396 (string-equal name "char"))
400 ((eq (ensure-keyword specified-type) :bigint)
402 ((eq (ensure-keyword specified-type) :char)
404 ((eq (ensure-keyword specified-type) :varchar)
408 (if (and type (not (member :not-null (listify db-constraints))))
412 ;; Compute the slot definition for slots in a view-class. Figures out
413 ;; what kind of database value (if any) is stored there, generates and
414 ;; verifies the column name.
416 (declaim (inline delistify))
417 (defun delistify (list)
418 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
423 (declaim (inline delistify-dsd))
424 (defun delistify-dsd (list)
425 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
426 (if (and (listp list) (null (cdr list)))
430 (defmethod initialize-instance :around ((obj view-class-direct-slot-definition)
432 (do* ((parsed (list obj))
433 (name (first initargs) (first initargs))
434 (val (second initargs) (second initargs))
436 (db-constraints nil))
440 (list 'specified-type type
441 :type (compute-lisp-type-from-specified-type
442 type db-constraints))))
443 (apply #'call-next-method parsed))
446 (setq db-constraints val)
447 (setq parsed (append parsed (list name val))))
451 (setq parsed (append parsed (list name val)))))
452 (setq initargs (cddr initargs))))
454 (defmethod compute-effective-slot-definition ((class standard-db-class)
455 #+kmr-normal-cesd slot-name
457 #+kmr-normal-cesd (declare (ignore slot-name))
459 ;; KMR: store the user-specified type and then compute
460 ;; real Lisp type and store it
461 (let ((dsd (car direct-slots)))
462 (let ((esd (call-next-method)))
464 (view-class-slot-definition-mixin
465 ;; Use the specified :column argument if it is supplied, otherwise
466 ;; the column slot is filled in with the slot-name, but transformed
467 ;; to be sql safe, - to _ and such.
468 (setf (slot-value esd 'column)
469 (column-name-from-arg
470 (if (slot-boundp dsd 'column)
471 (delistify-dsd (view-class-slot-column dsd))
472 (column-name-from-arg
473 (sql-escape (slot-definition-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)))
525 (unless (typep esd 'view-class-effective-slot-definition)
526 (warn "Non view-class-direct-slot object with non-view-class-effective-slot-definition in compute-effective-slot-definition")
528 (let ((type-predicate #+openmcl (slot-value esd 'ccl::type-predicate)))
529 #-openmcl (declare (ignore type-predicate))
530 #-(or clisp sbcl) (change-class esd 'view-class-effective-slot-definition
532 #+allegro (slot-definition-name dsd))
533 #+openmcl (setf (slot-value esd 'ccl::type-predicate)
536 (setf (slot-value esd 'column)
537 (column-name-from-arg
538 (sql-escape (slot-definition-name dsd))))
540 (setf (slot-value esd 'db-info) nil)
541 (setf (slot-value esd 'db-kind) :virtual)
542 (setf (specified-type esd) (slot-definition-type dsd)))
546 (defun slotdefs-for-slots-with-class (slots class)
549 (let ((c (slotdef-for-slot-with-class s class)))
550 (if c (setf result (cons c result)))))
553 (defun slotdef-for-slot-with-class (slot class)
554 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
555 (class-slots class)))
558 (eval-when (:compile-toplevel :load-toplevel :execute)
560 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
562 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
564 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))