return class from reinitialize-instance as SBCL expects (reported in SBCL-devel by...
[clsql.git] / sql / metaclasses.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; CLSQL metaclass for standard-db-objects created in the OODDL.
5 ;;;;
6 ;;;; This file is part of CLSQL.
7 ;;;;
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 ;;;; *************************************************************************
12
13 (in-package #:clsql-sys)
14
15 (eval-when (:compile-toplevel :load-toplevel :execute)
16   (when (>= (length (generic-function-lambda-list
17                      (ensure-generic-function
18                       'compute-effective-slot-definition)))
19             3)
20     (pushnew :kmr-normal-cesd cl:*features*))
21
22   (when (>= (length (generic-function-lambda-list
23                      (ensure-generic-function
24                       'direct-slot-definition-class)))
25             3)
26     (pushnew :kmr-normal-dsdc cl:*features*))
27
28   (when (>= (length (generic-function-lambda-list
29                      (ensure-generic-function
30                       'effective-slot-definition-class)))
31             3)
32     (pushnew :kmr-normal-esdc cl:*features*)))
33
34
35 ;; ------------------------------------------------------------
36 ;; metaclass: view-class
37
38 (defclass standard-db-class (standard-class)
39   ((view-table
40     :accessor view-table
41     :initarg :view-table)
42    (definition
43     :accessor object-definition
44     :initarg :definition
45     :initform nil)
46    (key-slots
47     :accessor key-slots
48     :initform nil)
49    (normalizedp
50     :accessor normalizedp
51     :initform nil)
52    (class-qualifier
53     :accessor view-class-qualifier
54     :initarg :qualifier
55     :initform nil))
56   (:documentation "Metaclass for all CLSQL View Classes."))
57
58 ;;; Lispworks 4.2 and before requires special processing of extra slot and class options
59
60 (defvar +extra-slot-options+ '(:column :db-kind :db-type :db-reader :void-value :db-constraints
61                                :db-writer :db-info))
62 (defvar +extra-class-options+ '(:base-table))
63
64 #+lispworks
65 (dolist (slot-option +extra-slot-options+)
66   (eval `(process-slot-option standard-db-class ,slot-option)))
67
68 #+lispworks
69 (dolist (class-option +extra-class-options+)
70   (eval `(process-class-option standard-db-class ,class-option)))
71
72 (defmethod validate-superclass ((class standard-db-class)
73                                 (superclass standard-class))
74   t)
75
76 (defun table-name-from-arg (arg)
77   (cond ((symbolp 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))))
83         ((stringp arg)
84          (sql-escape arg))))
85
86 (defun remove-keyword-arg (arglist akey)
87   (let ((mylist arglist)
88         (newlist ()))
89     (labels ((pop-arg (alist)
90              (let ((arg (pop alist))
91                    (val (pop alist)))
92                (unless (equal arg akey)
93                  (setf newlist (append (list arg val) newlist)))
94                (when alist (pop-arg alist)))))
95       (pop-arg mylist))
96     newlist))
97
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)
102                                           (car base-table)
103                                           base-table))
104                                  (class-name class)))))
105
106 (defmethod initialize-instance :around ((class standard-db-class)
107                                         &rest all-keys
108                                         &key direct-superclasses base-table
109                                         qualifier normalizedp
110                                         &allow-other-keys)
111   (let ((root-class (find-class 'standard-db-object nil))
112         (vmc 'standard-db-class))
113     (setf (view-class-qualifier class)
114           (car qualifier))
115     (if root-class
116         (if (some #'(lambda (super) (typep super vmc))
117                   direct-superclasses)
118             (call-next-method)
119             (apply #'call-next-method
120                    class
121                    :direct-superclasses (append (list root-class)
122                                                 direct-superclasses)
123                    (remove-keyword-arg all-keys :direct-superclasses)))
124         (call-next-method))
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))
128                                    all-keys))))
129
130 (defmethod reinitialize-instance :around ((class standard-db-class)
131                                           &rest all-keys
132                                           &key base-table normalizedp
133                                           direct-superclasses qualifier
134                                           &allow-other-keys)
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)
140           (car qualifier))
141     (if (and root-class (not (equal class root-class)))
142         (if (some #'(lambda (super) (typep super vmc))
143                   direct-superclasses)
144             (call-next-method)
145             (apply #'call-next-method
146                    class
147                    :direct-superclasses (append (list root-class)
148                                                 direct-superclasses)
149                    (remove-keyword-arg all-keys :direct-superclasses)))
150         (call-next-method)))
151   (register-metaclass class (nth (1+ (position :direct-slots all-keys))
152                                  all-keys))
153   class)
154
155
156 (defun get-keywords (keys list)
157   (flet ((extract (key)
158            (let ((pos (position key list)))
159              (when pos
160                (nth (1+ pos) list)))))
161     (mapcar #'extract keys)))
162
163 (defun describe-db-layout (class)
164   (flet ((not-db-col (col)
165            (not (member (nth 2 col) '(nil :base :key))))
166          (frob-slot (slot)
167            (let ((type (slot-definition-type slot)))
168              (if (eq type t)
169                  (setq type nil))
170              (list (slot-value slot 'name)
171                    type
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)
179       all-slots)))
180
181 (defun register-metaclass (class slots)
182   (labels ((not-db-col (col)
183              (not (member (nth 2 col)  '(nil :base :key))))
184            (frob-slot (slot)
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))
190     #-(or sbcl allegro)
191     (setf (key-slots class) (remove-if-not (lambda (slot)
192                                              (eql (slot-value slot 'db-kind)
193                                                   :key))
194                                            (slots-for-possibly-normalized-class class)))))
195
196 #+(or sbcl allegro)
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)
200                                                 :key))
201                                          (slots-for-possibly-normalized-class class))))
202
203 ;; return the deepest view-class ancestor for a given view class
204
205 (defun base-db-class (classname)
206   (let* ((class (find-class classname))
207          (db-class (find-class 'standard-db-object)))
208     (loop
209      (let ((cds (class-direct-superclasses class)))
210        (cond ((null cds)
211               (error "not a db class"))
212              ((member db-class cds)
213               (return (class-name class))))
214        (setq class (car cds))))))
215
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)
222                    (list class)
223                    (append (list class) (mapcar #'ancestors scs))))))
224       (ancestors class))))
225
226 (defclass view-class-slot-definition-mixin ()
227   ((column
228     :accessor view-class-slot-column
229     :initarg :column
230     :documentation
231     "The name of the SQL column this slot is stored in.  Defaults to
232 the slot name.")
233    (db-kind
234     :accessor view-class-slot-db-kind
235     :initarg :db-kind
236     :initform :base
237     ;; openmcl 0.14.2 stores the value as list in the DSD
238     ;; :type (or list keyword)
239     #-openmcl :type #-openmcl keyword
240     :documentation
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.")
246    (db-reader
247     :accessor view-class-slot-db-reader
248     :initarg :db-reader
249     :initform nil
250     :documentation
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.")
256    (db-writer
257     :accessor view-class-slot-db-writer
258     :initarg :db-writer
259     :initform nil
260     :documentation
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
266 the database.")
267    (db-type
268     :accessor view-class-slot-db-type
269     :initarg :db-type
270     :initform nil
271     :documentation
272     "A string which will be used as the type specifier for this slots
273 column definition in the database.")
274    (db-constraints
275     :accessor view-class-slot-db-constraints
276     :initarg :db-constraints
277     :initform nil
278     :documentation
279     "A keyword symbol representing a single SQL column constraint or list of such symbols.")
280    (void-value
281     :accessor view-class-slot-void-value
282     :initarg :void-value
283     :initform nil
284     :documentation
285     "Value to store if the SQL value is NULL. Default is NIL.")
286    (db-info
287     :accessor view-class-slot-db-info
288     :initarg :db-info
289     :documentation "Description of the join.")
290    (specified-type
291     :accessor specified-type
292     :initarg specified-type
293     :initform nil
294     :documentation "Internal slot storing the :type specified by user.")
295    (autoincrement-sequence
296     :accessor view-class-slot-autoincrement-sequence
297     :initarg :autoincrement-sequence
298     :initform nil
299     :documentation "A string naming the (possibly automatically generated) sequence
300 for a slot with an :auto-increment constraint.")))
301
302 (defparameter *db-info-lambda-list*
303   '(&key join-class
304          home-key
305          foreign-key
306          (key-join nil)
307          (target-slot nil)
308          (retrieval :immmediate)
309          (set nil)))
310
311 (defun parse-db-info (db-info-list)
312   (destructuring-bind
313         (&key join-class home-key key-join foreign-key (delete-rule nil)
314               (target-slot nil) (retrieval :deferred) (set t))
315       db-info-list
316     (let ((ih (make-hash-table :size 6)))
317       (if join-class
318           (setf (gethash :join-class ih) join-class)
319           (error "Must specify :join-class in :db-info"))
320       (if home-key
321           (setf (gethash :home-key ih) home-key)
322           (error "Must specify :home-key in :db-info"))
323       (when delete-rule
324         (setf (gethash :delete-rule ih) delete-rule))
325       (if foreign-key
326           (setf (gethash :foreign-key ih) foreign-key)
327           (error "Must specify :foreign-key in :db-info"))
328       (when key-join
329         (setf (gethash :key-join ih) t))
330       (when target-slot
331         (setf (gethash :target-slot ih) target-slot))
332       (when set
333         (setf (gethash :set ih) set))
334       (when retrieval
335         (progn
336           (setf (gethash :retrieval ih) retrieval)
337           (if (eql retrieval :immediate)
338               (setf (gethash :set ih) nil))))
339       ih)))
340
341 (defclass view-class-direct-slot-definition (view-class-slot-definition-mixin
342                                              standard-direct-slot-definition)
343   ())
344
345 (defclass view-class-effective-slot-definition (view-class-slot-definition-mixin
346                                                 standard-effective-slot-definition)
347   ())
348
349 (defmethod direct-slot-definition-class ((class standard-db-class)
350                                          #+kmr-normal-dsdc &rest
351                                          initargs)
352   (declare (ignore initargs))
353   (find-class 'view-class-direct-slot-definition))
354
355 (defmethod effective-slot-definition-class ((class standard-db-class)
356                                             #+kmr-normal-esdc &rest
357                                             initargs)
358   (declare (ignore initargs))
359   (find-class 'view-class-effective-slot-definition))
360
361 #+openmcl
362 (when (not (symbol-function 'compute-class-precedence-list))
363   (eval
364    (defun compute-class-precedence-list (class)
365      (class-precedence-list class))))
366
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
370 implementations."
371   (let ((slots (call-next-method))
372         desired-sequence
373         output-slots)
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)))
381         (assert slot)
382         (push slot output-slots)))
383     output-slots))
384
385 (defun compute-lisp-type-from-specified-type (specified-type db-constraints)
386   "Computes the Lisp type for a user-specified type."
387   (let ((type
388          (cond
389            ((consp specified-type)
390             (let* ((first (first specified-type))
391                    (name (etypecase first
392                            (symbol (symbol-name first))
393                            (string first))))
394               (cond
395                ((or (string-equal name "string")
396                     (string-equal name "varchar")
397                     (string-equal name "char"))
398                 'string)
399                (t
400                 specified-type))))
401            ((eq (ensure-keyword specified-type) :bigint)
402             'integer)
403            ((eq (ensure-keyword specified-type) :char)
404             'character)
405            ((eq (ensure-keyword specified-type) :varchar)
406             'string)
407            (t
408             specified-type))))
409     (if (and type (not (member :not-null (listify db-constraints))))
410         `(or null ,type)
411         (or type t))))
412
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.
416
417 (declaim (inline delistify))
418 (defun delistify (list)
419   "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
420   (if (listp list)
421       (car list)
422       list))
423
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
433          'specified-type type
434          :type (if (and (eql db-kind :virtual) (null type))
435                    t
436                    (compute-lisp-type-from-specified-type
437                     type db-constraints))
438          initargs))
439
440 (defun compute-column-name (arg)
441   (database-identifier arg nil))
442
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)))
450             (etypecase info
451               (hash-table info)
452               (atom info)
453               (list
454                (cond ((and (> (length info) 1)
455                            (atom (car info)))
456                       (parse-db-info info))
457                      ((and (= 1 (length info))
458                            (listp (car info)))
459                       (parse-db-info (car info)))
460                      (t info))))))))
461
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)
467         (dequote
468          (view-class-slot-autoincrement-sequence obj)))
469   (%convert-db-info-to-hash obj))
470
471 (defmethod compute-effective-slot-definition ((class standard-db-class)
472                                               #+kmr-normal-cesd slot-name
473                                               direct-slots)
474   #+kmr-normal-cesd (declare (ignore slot-name))
475
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)))
480       (typecase dsd
481         (view-class-slot-definition-mixin
482          (setf (slot-value esd 'column) (compute-column-name dsd))
483
484          (macrolet
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)))
490                      ,default)))))
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
497            ;; database)
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))
502
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))
511                               '(cl:and) '(cl:or)))
512          (setf (slot-value esd 'sb-pcl::%type-check-function) nil)
513
514          )
515         ;; all other slots
516         (t
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")
519
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
523                                               #+allegro :name
524                                               #+allegro (slot-definition-name dsd))
525              #+openmcl (setf (slot-value esd 'ccl::type-predicate)
526                              type-predicate)))
527
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)))
533         )
534       esd)))
535
536 (defun slotdefs-for-slots-with-class (slots class)
537   (let ((result nil))
538     (dolist (s slots)
539       (let ((c (slotdef-for-slot-with-class s class)))
540         (if c (setf result (cons c result)))))
541     result))
542
543 (defun slotdef-for-slot-with-class (slot class)
544   (typecase slot
545     (standard-slot-definition slot)
546     (symbol (find-slot-by-name class slot))))
547
548 #+ignore
549 (eval-when (:compile-toplevel :load-toplevel :execute)
550   #+kmr-normal-cesd
551   (setq cl:*features* (delete :kmr-normal-cesd cl:*features*))
552   #+kmr-normal-dsdc
553   (setq cl:*features* (delete :kmr-normal-dsdc cl:*features*))
554   #+kmr-normal-esdc
555   (setq cl:*features* (delete :kmr-normal-esdc cl:*features*))
556   )
557
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))
564
565 (defmethod database-identifier ((name view-class-slot-definition-mixin)
566                                 &optional database find-class-p)
567   (declare (ignore find-class-p))
568   (database-identifier
569    (if (slot-boundp name 'column)
570        (delistify-dsd (view-class-slot-column name))
571        (slot-definition-name name))
572    database))
573
574 (defun find-standard-db-class (name &aux cls)
575   (and (setf cls (ignore-errors (find-class name)))
576        (typep cls 'standard-db-class)
577        cls))
578
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)))
585
586
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)))
590
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)))
594
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)))))
600
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))
604
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))
608
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))))
613
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)))
617
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)
622        (normalizedp class)
623        (member slot-name (ordered-class-direct-slots class)
624                :key #'slot-definition-name)))
625
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)
630        (normalizedp class)
631        (not (member slot-name (ordered-class-direct-slots class)
632                     :key #'slot-definition-name))))
633
634 (defun slot-has-default-p (slot)
635   "returns nil if the slot does not have a default constraint"
636   (let* ((constraints
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)))
641