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
50 :accessor view-class-qualifier
53 (:documentation
"Metaclass for all CLSQL View Classes."))
55 ;;; Lispworks 4.2 and before requires special processing of extra slot and class options
57 (defvar +extra-slot-options
+ '(:column
:db-kind
:db-type
:db-reader
:void-value
:db-constraints
59 (defvar +extra-class-options
+ '(:base-table
))
62 (dolist (slot-option +extra-slot-options
+)
63 (eval `(process-slot-option standard-db-class
,slot-option
)))
66 (dolist (class-option +extra-class-options
+)
67 (eval `(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)
75 (intern (sql-escape arg
)))
76 ((typep arg
'sql-ident
)
77 (if (symbolp (slot-value arg
'name
))
78 (intern (sql-escape (slot-value arg
'name
)))
79 (sql-escape (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 (defun set-view-table-slot (class base-table
)
105 (setf (view-table class
)
106 (table-name-from-arg (or (and base-table
107 (if (listp base-table
)
110 (class-name class
)))))
112 (defmethod initialize-instance :around
((class standard-db-class
)
114 &key direct-superclasses base-table
117 (let ((root-class (find-class 'standard-db-object nil
))
118 (vmc 'standard-db-class
))
119 (setf (view-class-qualifier class
)
122 (if (some #'(lambda (super) (typep super vmc
))
125 (apply #'call-next-method
127 :direct-superclasses
(append (list root-class
)
129 (remove-keyword-arg all-keys
:direct-superclasses
)))
131 (set-view-table-slot class base-table
)
132 (register-metaclass class
(nth (1+ (position :direct-slots all-keys
))
135 (defmethod reinitialize-instance :around
((class standard-db-class
)
138 direct-superclasses qualifier
140 (let ((root-class (find-class 'standard-db-object nil
))
141 (vmc 'standard-db-class
))
142 (set-view-table-slot class base-table
)
143 (setf (view-class-qualifier class
)
145 (if (and root-class
(not (equal class root-class
)))
146 (if (some #'(lambda (super) (typep super vmc
))
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-definition-type slot
)))
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
))
194 (setf (key-slots class
) (remove-if-not (lambda (slot)
195 (eql (slot-value slot
'db-kind
)
197 (ordered-class-slots class
)))))
200 (defmethod finalize-inheritance :after
((class standard-db-class
))
201 (setf (key-slots class
) (remove-if-not (lambda (slot)
202 (eql (slot-value slot
'db-kind
)
204 (ordered-class-slots class
))))
206 ;; return the deepest view-class ancestor for a given view class
208 (defun base-db-class (classname)
209 (let* ((class (find-class classname
))
210 (db-class (find-class 'standard-db-object
)))
212 (let ((cds (class-direct-superclasses class
)))
214 (error "not a db class"))
215 ((member db-class cds
)
216 (return (class-name class
))))
217 (setq class
(car cds
))))))
219 (defun db-ancestors (classname)
220 (let ((class (find-class classname
))
221 (db-class (find-class 'standard-db-object
)))
222 (labels ((ancestors (class)
223 (let ((scs (class-direct-superclasses class
)))
224 (if (member db-class scs
)
226 (append (list class
) (mapcar #'ancestors scs
))))))
229 (defclass view-class-slot-definition-mixin
()
231 :accessor view-class-slot-column
234 "The name of the SQL column this slot is stored in. Defaults to
237 :accessor view-class-slot-db-kind
240 ;; openmcl 0.14.2 stores the value as list in the DSD
241 ;; :type (or list keyword)
242 #-openmcl
:type
#-openmcl keyword
244 "The kind of DB mapping which is performed for this slot. :base
245 indicates the slot maps to an ordinary column of the DB view. :key
246 indicates that this slot corresponds to part of the unique keys for
247 this view, :join indicates ... and :virtual indicates that this slot
248 is an ordinary CLOS slot. Defaults to :base.")
250 :accessor view-class-slot-db-reader
254 "If a string, then when reading values from the DB, the string
255 will be used for a format string, with the only value being the value
256 from the database. The resulting string will be used as the slot
257 value. If a function then it will take one argument, the value from
258 the database, and return the value that should be put into the slot.")
260 :accessor view-class-slot-db-writer
264 "If a string, then when reading values from the slot for the DB,
265 the string will be used for a format string, with the only value being
266 the value of the slot. The resulting string will be used as the
267 column value in the DB. If a function then it will take one argument,
268 the value of the slot, and return the value that should be put into
271 :accessor view-class-slot-db-type
275 "A string which will be used as the type specifier for this slots
276 column definition in the database.")
278 :accessor view-class-slot-db-constraints
279 :initarg
:db-constraints
282 "A keyword symbol representing a single SQL column constraint or list of such symbols.")
284 :accessor view-class-slot-void-value
288 "Value to store if the SQL value is NULL. Default is NIL.")
290 :accessor view-class-slot-db-info
292 :documentation
"Description of the join.")
294 :accessor specified-type
295 :initarg specified-type
297 :documentation
"Internal slot storing the :type specified by user.")))
299 (defparameter *db-info-lambda-list
*
305 (retrieval :immmediate
)
308 (defun parse-db-info (db-info-list)
310 (&key join-class home-key key-join foreign-key
(delete-rule nil
)
311 (target-slot nil
) (retrieval :deferred
) (set t
))
313 (let ((ih (make-hash-table :size
6)))
315 (setf (gethash :join-class ih
) join-class
)
316 (error "Must specify :join-class in :db-info"))
318 (setf (gethash :home-key ih
) home-key
)
319 (error "Must specify :home-key in :db-info"))
321 (setf (gethash :delete-rule ih
) delete-rule
))
323 (setf (gethash :foreign-key ih
) foreign-key
)
324 (error "Must specify :foreign-key in :db-info"))
326 (setf (gethash :key-join ih
) t
))
328 (setf (gethash :target-slot ih
) target-slot
))
330 (setf (gethash :set ih
) set
))
333 (setf (gethash :retrieval ih
) retrieval
)
334 (if (eql retrieval
:immediate
)
335 (setf (gethash :set ih
) nil
))))
338 (defclass view-class-direct-slot-definition
(view-class-slot-definition-mixin
339 standard-direct-slot-definition
)
342 (defclass view-class-effective-slot-definition
(view-class-slot-definition-mixin
343 standard-effective-slot-definition
)
346 (defmethod direct-slot-definition-class ((class standard-db-class
)
347 #+kmr-normal-dsdc
&rest
349 (declare (ignore initargs
))
350 (find-class 'view-class-direct-slot-definition
))
352 (defmethod effective-slot-definition-class ((class standard-db-class
)
353 #+kmr-normal-esdc
&rest
355 (declare (ignore initargs
))
356 (find-class 'view-class-effective-slot-definition
))
359 (when (not (symbol-function 'compute-class-precedence-list
))
361 (defun compute-class-precedence-list (class)
362 (class-precedence-list class
))))
364 #-mop-slot-order-reversed
365 (defmethod compute-slots ((class standard-db-class
))
366 "Need to sort order of class slots so they are the same across
368 (let ((slots (call-next-method))
371 (dolist (c (compute-class-precedence-list class
))
372 (dolist (s (class-direct-slots c
))
373 (let ((name (slot-definition-name s
)))
374 (unless (find name desired-sequence
)
375 (push name desired-sequence
)))))
376 (dolist (desired desired-sequence
)
377 (let ((slot (find desired slots
:key
#'slot-definition-name
)))
379 (push slot output-slots
)))
382 (defun compute-lisp-type-from-specified-type (specified-type db-constraints
)
383 "Computes the Lisp type for a user-specified type."
386 ((consp specified-type
)
387 (let* ((first (first specified-type
))
388 (name (etypecase first
389 (symbol (symbol-name first
))
392 ((or (string-equal name
"string")
393 (string-equal name
"varchar")
394 (string-equal name
"char"))
398 ((eq (ensure-keyword specified-type
) :bigint
)
400 ((eq (ensure-keyword specified-type
) :char
)
402 ((eq (ensure-keyword specified-type
) :varchar
)
406 (if (and type
(not (member :not-null
(listify db-constraints
))))
410 ;; Compute the slot definition for slots in a view-class. Figures out
411 ;; what kind of database value (if any) is stored there, generates and
412 ;; verifies the column name.
414 (declaim (inline delistify
))
415 (defun delistify (list)
416 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
421 (declaim (inline delistify-dsd
))
422 (defun delistify-dsd (list)
423 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
424 (if (and (listp list
) (null (cdr list
)))
428 (defmethod initialize-instance :around
429 ((obj view-class-direct-slot-definition
)
430 &rest initargs
&key db-constraints db-kind type
&allow-other-keys
)
431 (when (and (not db-kind
) (member :primary-key
(listify db-constraints
)))
432 (warn "Slot ~S constrained to be :primary-key, but not marked as :db-kind :key"
433 (slot-definition-name obj
)))
434 (apply #'call-next-method obj
436 :type
(compute-lisp-type-from-specified-type
440 (defmethod compute-effective-slot-definition ((class standard-db-class
)
441 #+kmr-normal-cesd slot-name
443 #+kmr-normal-cesd
(declare (ignore slot-name
))
445 ;; KMR: store the user-specified type and then compute
446 ;; real Lisp type and store it
447 (let ((dsd (car direct-slots
)))
448 (let ((esd (call-next-method)))
450 (view-class-slot-definition-mixin
451 ;; Use the specified :column argument if it is supplied, otherwise
452 ;; the column slot is filled in with the slot-name, but transformed
453 ;; to be sql safe, - to _ and such.
454 (setf (slot-value esd
'column
)
455 (column-name-from-arg
456 (if (slot-boundp dsd
'column
)
457 (delistify-dsd (view-class-slot-column dsd
))
458 (column-name-from-arg
459 (sql-escape (slot-definition-name dsd
))))))
461 (setf (slot-value esd
'db-type
)
462 (when (slot-boundp dsd
'db-type
)
464 (view-class-slot-db-type dsd
))))
466 (setf (slot-value esd
'void-value
)
468 (view-class-slot-void-value dsd
)))
470 ;; :db-kind slot value defaults to :base (store slot value in
473 (setf (slot-value esd
'db-kind
)
474 (if (slot-boundp dsd
'db-kind
)
475 (delistify-dsd (view-class-slot-db-kind dsd
))
478 (setf (slot-value esd
'db-reader
)
479 (when (slot-boundp dsd
'db-reader
)
480 (delistify-dsd (view-class-slot-db-reader dsd
))))
481 (setf (slot-value esd
'db-writer
)
482 (when (slot-boundp dsd
'db-writer
)
483 (delistify-dsd (view-class-slot-db-writer dsd
))))
484 (setf (slot-value esd
'db-constraints
)
485 (when (slot-boundp dsd
'db-constraints
)
486 (delistify-dsd (view-class-slot-db-constraints dsd
))))
488 ;; I wonder if this slot option and the previous could be merged,
489 ;; so that :base and :key remain keyword options, but :db-kind
490 ;; :join becomes :db-kind (:join <db info .... >)?
492 (setf (slot-value esd
'db-info
)
493 (when (slot-boundp dsd
'db-info
)
494 (let ((dsd-info (view-class-slot-db-info dsd
)))
498 ((and (listp dsd-info
) (> (length dsd-info
) 1)
499 (atom (car dsd-info
)))
500 (parse-db-info dsd-info
))
501 ((and (listp dsd-info
) (= 1 (length dsd-info
))
502 (listp (car dsd-info
)))
503 (parse-db-info (car dsd-info
)))))))
505 (setf (specified-type esd
)
506 (delistify-dsd (specified-type dsd
)))
511 (unless (typep esd
'view-class-effective-slot-definition
)
512 (warn "Non view-class-direct-slot object with non-view-class-effective-slot-definition in compute-effective-slot-definition")
514 (let ((type-predicate #+openmcl
(slot-value esd
'ccl
::type-predicate
)))
515 #-openmcl
(declare (ignore type-predicate
))
516 #-
(or clisp sbcl
) (change-class esd
'view-class-effective-slot-definition
518 #+allegro
(slot-definition-name dsd
))
519 #+openmcl
(setf (slot-value esd
'ccl
::type-predicate
)
522 (setf (slot-value esd
'column
)
523 (column-name-from-arg
524 (sql-escape (slot-definition-name dsd
))))
526 (setf (slot-value esd
'db-info
) nil
)
527 (setf (slot-value esd
'db-kind
) :virtual
)
528 (setf (specified-type esd
) (slot-definition-type dsd
)))
532 (defun slotdefs-for-slots-with-class (slots class
)
535 (let ((c (slotdef-for-slot-with-class s class
)))
536 (if c
(setf result
(cons c result
)))))
539 (defun slotdef-for-slot-with-class (slot class
)
540 (find-if #'(lambda (d) (eql slot
(slot-definition-name d
)))
541 (class-slots class
)))
544 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
546 (setq cl
:*features
* (delete :kmr-normal-cesd cl
:*features
*))
548 (setq cl
:*features
* (delete :kmr-normal-dsdc cl
:*features
*))
550 (setq cl
:*features
* (delete :kmr-normal-esdc cl
:*features
*))