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
49 :accessor object-version
56 :accessor view-class-qualifier
59 (:documentation "VIEW-CLASS metaclass."))
62 (defmacro push-on-end (value location)
63 `(setf ,location (nconc ,location (list ,value))))
65 ;; As Heiko Kirscke (author of PLOB!) would say: !@##^@%! Lispworks!
67 (defconstant +extra-slot-options+ '(:column :db-kind :db-reader :nulls-ok
68 :db-writer :db-type :db-info))
71 (define-setf-expander assoc (key alist &environment env)
72 (multiple-value-bind (temps vals stores store-form access-form)
73 (get-setf-expansion alist env)
74 (let ((new-value (gensym "NEW-VALUE-"))
75 (keyed (gensym "KEYED-"))
76 (accessed (gensym "ACCESSED-"))
77 (store-new-value (car stores)))
78 (values (cons keyed temps)
81 `(let* ((,accessed ,access-form)
82 (,store-new-value (assoc ,keyed ,accessed)))
84 (rplacd ,store-new-value ,new-value)
86 (setq ,store-new-value
87 (acons ,keyed ,new-value ,accessed))
90 `(assoc ,new-value ,access-form)))))
93 (defmethod clos::canonicalize-defclass-slot :around
94 ((prototype standard-db-class) slot)
95 "\\lw\\ signals an error on unknown slot options; so this method
96 removes any extra allowed options before calling the default method
97 and returns the canonicalized extra options concatenated to the result
98 of the default method. The extra allowed options are the value of the
99 \\fcite{+extra-slot-options+}."
100 (let ((extra-slot-options ())
103 (do ((olist (cdr slot) (cddr olist)))
105 (let ((option (car olist)))
107 ((find option +extra-slot-options+)
108 ;;(push (cons option (cadr olist)) extra-slot-options))
109 (setf (assoc option extra-slot-options) (cadr olist)))
111 (push (cadr olist) rest-options)
112 (push (car olist) rest-options)))))
113 (setf result (call-next-method prototype (cons (car slot) rest-options)))
114 (dolist (option extra-slot-options)
115 (push-on-end (car option) result)
116 (push-on-end `(quote ,(cdr option)) result))
120 (defconstant +extra-class-options+ '(:base-table :version :schemas))
123 (defmethod clos::canonicalize-class-options :around
124 ((prototype standard-db-class) class-options)
125 "\\lw\\ signals an error on unknown class options; so this method
126 removes any extra allowed options before calling the default method
127 and returns the canonicalized extra options concatenated to the result
128 of the default method. The extra allowed options are the value of the
129 \\fcite{+extra-class-options+}."
130 (let ((extra-class-options nil)
133 (dolist (o class-options)
134 (let ((option (car o)))
136 ((find option +extra-class-options+)
137 ;;(push (cons option (cadr o)) extra-class-options))
138 (setf (assoc option extra-class-options) (cadr o)))
140 (push o rest-options)))))
141 (setf result (call-next-method prototype rest-options))
142 (dolist (option extra-class-options)
143 (push-on-end (car option) result)
144 (push-on-end `(quote ,(cdr option)) result))
148 (defmethod validate-superclass ((class standard-db-class)
149 (superclass standard-class))
152 (defun table-name-from-arg (arg)
155 ((typep arg 'sql-ident)
156 (slot-value arg 'name))
158 (intern (string-upcase arg)))))
160 (defun column-name-from-arg (arg)
163 ((typep arg 'sql-ident)
164 (slot-value arg 'name))
166 (intern (string-upcase arg)))))
169 (defun remove-keyword-arg (arglist akey)
170 (let ((mylist arglist)
172 (labels ((pop-arg (alist)
173 (let ((arg (pop alist))
175 (unless (equal arg akey)
176 (setf newlist (append (list arg val) newlist)))
177 (when alist (pop-arg alist)))))
181 (defmethod initialize-instance :around ((class standard-db-class)
183 &key direct-superclasses base-table
184 schemas version qualifier
186 (let ((root-class (find-class 'standard-db-object nil))
187 (vmc (find-class 'standard-db-class)))
188 (setf (view-class-qualifier class)
191 (if (member-if #'(lambda (super)
192 (eq (class-of super) vmc)) direct-superclasses)
194 (apply #'call-next-method
196 :direct-superclasses (append (list root-class)
198 (remove-keyword-arg all-keys :direct-superclasses)))
200 (setf (view-table class)
201 (table-name-from-arg (sql-escape (or (and base-table
202 (if (listp base-table)
205 (class-name class)))))
206 (setf (object-version class) version)
207 (mapc (lambda (schema)
208 (pushnew (class-name class) (gethash schema *object-schemas*)))
209 (if (listp schemas) schemas (list schemas)))
210 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
213 (defmethod reinitialize-instance :around ((class standard-db-class)
215 &key base-table schemas version
216 direct-superclasses qualifier
218 (let ((root-class (find-class 'standard-db-object nil))
219 (vmc (find-class 'standard-db-class)))
220 (setf (view-table class)
221 (table-name-from-arg (sql-escape (or (and base-table
222 (if (listp base-table)
225 (class-name class)))))
226 (setf (view-class-qualifier class)
228 (if (and root-class (not (equal class root-class)))
229 (if (member-if #'(lambda (super)
230 (eq (class-of super) vmc)) direct-superclasses)
232 (apply #'call-next-method
234 :direct-superclasses (append (list root-class)
236 (remove-keyword-arg all-keys :direct-superclasses)))
238 (setf (object-version class) version)
239 (mapc (lambda (schema)
240 (pushnew (class-name class) (gethash schema *object-schemas*)))
241 (if (listp schemas) schemas (list schemas)))
242 (register-metaclass class (nth (1+ (position :direct-slots all-keys))
246 (defun get-keywords (keys list)
247 (flet ((extract (key)
248 (let ((pos (position key list)))
250 (nth (1+ pos) list)))))
251 (mapcar #'extract keys)))
253 (defun describe-db-layout (class)
254 (flet ((not-db-col (col)
255 (not (member (nth 2 col) '(nil :base :key))))
257 (let ((type (slot-value slot 'type)))
260 (list (slot-value slot 'name)
262 (slot-value slot 'db-kind)
263 (and (slot-boundp slot 'column)
264 (slot-value slot 'column))))))
265 (let ((all-slots (mapcar #'frob-slot (class-slots class))))
266 (setq all-slots (remove-if #'not-db-col all-slots))
267 (setq all-slots (stable-sort all-slots #'string< :key #'car))
268 ;;(mapcar #'dink-type all-slots)
271 (defun register-metaclass (class slots)
272 (labels ((not-db-col (col)
273 (not (member (nth 2 col) '(nil :base :key))))
275 (get-keywords '(:name :type :db-kind :column) slot)))
276 (let ((all-slots (mapcar #'frob-slot slots)))
277 (setq all-slots (remove-if #'not-db-col all-slots))
278 (setq all-slots (stable-sort all-slots #'string< :key #'car))
279 (setf (object-definition class) all-slots))
280 #-(or allegro openmcl)
281 (setf (key-slots class) (remove-if-not (lambda (slot)
282 (eql (slot-value slot 'db-kind)
284 (class-slots class)))))
286 #+(or allegro openmcl)
287 (defmethod finalize-inheritance :after ((class standard-db-class))
288 (setf (key-slots class) (remove-if-not (lambda (slot)
289 (eql (slot-value slot 'db-kind)
291 (class-slots class))))
293 ;; return the deepest view-class ancestor for a given view class
295 (defun base-db-class (classname)
296 (let* ((class (find-class classname))
297 (db-class (find-class 'standard-db-object)))
299 (let ((cds (class-direct-superclasses class)))
301 (error "not a db class"))
302 ((member db-class cds)
303 (return (class-name class))))
304 (setq class (car cds))))))
306 (defun db-ancestors (classname)
307 (let ((class (find-class classname))
308 (db-class (find-class 'standard-db-object)))
309 (labels ((ancestors (class)
310 (let ((scs (class-direct-superclasses class)))
311 (if (member db-class scs)
313 (append (list class) (mapcar #'ancestors scs))))))
316 (defclass view-class-slot-definition-mixin ()
318 :accessor view-class-slot-column
321 "The name of the SQL column this slot is stored in. Defaults to
324 :accessor view-class-slot-db-kind
329 "The kind of DB mapping which is performed for this slot. :base
330 indicates the slot maps to an ordinary column of the DB view. :key
331 indicates that this slot corresponds to part of the unique keys for
332 this view, :join indicates ... and :virtual indicates that this slot
333 is an ordinary CLOS slot. Defaults to :base.")
335 :accessor view-class-slot-db-reader
339 "If a string, then when reading values from the DB, the string
340 will be used for a format string, with the only value being the value
341 from the database. The resulting string will be used as the slot
342 value. If a function then it will take one argument, the value from
343 the database, and return the value that should be put into the slot.")
345 :accessor view-class-slot-db-writer
349 "If a string, then when reading values from the slot for the DB,
350 the string will be used for a format string, with the only value being
351 the value of the slot. The resulting string will be used as the
352 column value in the DB. If a function then it will take one argument,
353 the value of the slot, and return the value that should be put into
356 :accessor view-class-slot-db-type
360 "A string which will be used as the type specifier for this slots
361 column definition in the database.")
363 :accessor view-class-slot-db-constraints
364 :initarg :db-constraints
367 "A single constraint or list of constraints for this column")
369 :accessor view-class-slot-nulls-ok
373 "If t, all sql NULL values retrieved from the database become nil; if nil,
374 all NULL values retrieved are converted by DATABASE-NULL-VALUE")
376 :accessor view-class-slot-db-info
378 :documentation "Description of the join.")))
380 (defparameter *db-info-lambda-list*
386 (retrieval :immmediate)
389 (defun parse-db-info (db-info-list)
391 (&key join-class home-key key-join foreign-key (delete-rule nil)
392 (target-slot nil) (retrieval :deferred) (set nil))
394 (let ((ih (make-hash-table :size 6)))
396 (setf (gethash :join-class ih) join-class)
397 (error "Must specify :join-class in :db-info"))
399 (setf (gethash :home-key ih) home-key)
400 (error "Must specify :home-key in :db-info"))
402 (setf (gethash :delete-rule ih) delete-rule))
404 (setf (gethash :foreign-key ih) foreign-key)
405 (error "Must specify :foreign-key in :db-info"))
407 (setf (gethash :key-join ih) t))
409 (setf (gethash :target-slot ih) target-slot))
411 (setf (gethash :set ih) set))
414 (setf (gethash :retrieval ih) retrieval)
415 (if (eql retrieval :immediate)
416 (setf (gethash :set ih) nil))))
419 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
420 standard-direct-slot-definition)
423 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
424 standard-effective-slot-definition)
427 (defmethod direct-slot-definition-class ((class standard-db-class)
428 #+kmr-normal-dsdc &rest
430 (declare (ignore initargs))
431 (find-class 'view-class-direct-slot-definition))
433 (defmethod effective-slot-definition-class ((class standard-db-class)
434 #+kmr-normal-esdc &rest
436 (declare (ignore initargs))
437 (find-class 'view-class-effective-slot-definition))
439 ;; Compute the slot definition for slots in a view-class. Figures out
440 ;; what kind of database value (if any) is stored there, generates and
441 ;; verifies the column name.
443 (defmethod compute-effective-slot-definition ((class standard-db-class)
444 #+kmr-normal-cesd slot-name
446 #+kmr-normal-cesd (declare (ignore slot-name))
447 (let ((slotd (call-next-method))
448 (sd (car direct-slots)))
451 (view-class-slot-definition-mixin
452 ;; Use the specified :column argument if it is supplied, otherwise
453 ;; the column slot is filled in with the slot-name, but transformed
454 ;; to be sql safe, - to _ and such.
455 (setf (slot-value slotd 'column)
456 (column-name-from-arg
457 (if (slot-boundp sd 'column)
458 (view-class-slot-column sd)
459 (column-name-from-arg
460 (sql-escape (slot-definition-name sd))))))
462 (setf (slot-value slotd 'db-type)
463 (when (slot-boundp sd 'db-type)
464 (view-class-slot-db-type sd)))
467 (setf (slot-value slotd 'nulls-ok)
468 (view-class-slot-nulls-ok sd))
470 ;; :db-kind slot value defaults to :base (store slot value in
473 (setf (slot-value slotd 'db-kind)
474 (if (slot-boundp sd 'db-kind)
475 (view-class-slot-db-kind sd)
478 (setf (slot-value slotd 'db-writer)
479 (when (slot-boundp sd 'db-writer)
480 (view-class-slot-db-writer sd)))
481 (setf (slot-value slotd 'db-constraints)
482 (when (slot-boundp sd 'db-constraints)
483 (view-class-slot-db-constraints sd)))
486 ;; I wonder if this slot option and the previous could be merged,
487 ;; so that :base and :key remain keyword options, but :db-kind
488 ;; :join becomes :db-kind (:join <db info .... >)?
490 (setf (slot-value slotd 'db-info)
491 (when (slot-boundp sd 'db-info)
492 (if (listp (view-class-slot-db-info sd))
493 (parse-db-info (view-class-slot-db-info sd))
494 (view-class-slot-db-info sd)))))
497 (change-class slotd 'view-class-effective-slot-definition
499 #+allegro (slot-definition-name sd))
500 (setf (slot-value slotd 'column)
501 (column-name-from-arg
502 (sql-escape (slot-definition-name sd))))
504 (setf (slot-value slotd 'db-info) nil)
505 (setf (slot-value slotd 'db-kind)
509 (defun slotdefs-for-slots-with-class (slots class)
512 (let ((c (slotdef-for-slot-with-class s class)))
513 (if c (setf result (cons c result)))))
516 (defun slotdef-for-slot-with-class (slot class)
517 (find-if #'(lambda (d) (eql slot (slot-definition-name d)))
518 (class-slots class)))
521 (eval-when (:compile-toplevel :load-toplevel :execute)
523 (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
525 (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
527 (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))