X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql%2Fmetaclasses.lisp;h=df3c36e0e12f51836fc21e9c3557e0e02ce7cee3;hb=d86f73be9a261b9c071ab905aeff5d1ee30a3f31;hp=a9e3ccd84efe2290fdd74e1f705e203981472634;hpb=1eb686cfa4935e1252b2813ec6391bd781e88508;p=clsql.git diff --git a/sql/metaclasses.lisp b/sql/metaclasses.lisp index a9e3ccd..df3c36e 100644 --- a/sql/metaclasses.lisp +++ b/sql/metaclasses.lisp @@ -46,6 +46,9 @@ (key-slots :accessor key-slots :initform nil) + (normalizedp + :accessor normalizedp + :initform nil) (class-qualifier :accessor view-class-qualifier :initarg :qualifier @@ -80,15 +83,6 @@ ((stringp arg) (sql-escape arg)))) -(defun column-name-from-arg (arg) - (cond ((symbolp arg) - arg) - ((typep arg 'sql-ident) - (slot-value arg 'name)) - ((stringp arg) - (intern (symbol-name-default-case arg))))) - - (defun remove-keyword-arg (arglist akey) (let ((mylist arglist) (newlist ())) @@ -109,10 +103,21 @@ base-table)) (class-name class))))) +(defgeneric ordered-class-direct-slots (class)) +(defmethod ordered-class-direct-slots ((self standard-db-class)) + (let ((direct-slot-names + (mapcar #'slot-definition-name (class-direct-slots self))) + (ordered-direct-class-slots '())) + (dolist (slot (ordered-class-slots self)) + (let ((slot-name (slot-definition-name slot))) + (when (find slot-name direct-slot-names) + (push slot ordered-direct-class-slots)))) + (nreverse ordered-direct-class-slots))) + (defmethod initialize-instance :around ((class standard-db-class) &rest all-keys &key direct-superclasses base-table - qualifier + qualifier normalizedp &allow-other-keys) (let ((root-class (find-class 'standard-db-object nil)) (vmc 'standard-db-class)) @@ -129,17 +134,19 @@ (remove-keyword-arg all-keys :direct-superclasses))) (call-next-method)) (set-view-table-slot class base-table) + (setf (normalizedp class) (car normalizedp)) (register-metaclass class (nth (1+ (position :direct-slots all-keys)) all-keys)))) (defmethod reinitialize-instance :around ((class standard-db-class) &rest all-keys - &key base-table + &key base-table normalizedp direct-superclasses qualifier &allow-other-keys) (let ((root-class (find-class 'standard-db-object nil)) (vmc 'standard-db-class)) (set-view-table-slot class base-table) + (setf (normalizedp class) (car normalizedp)) (setf (view-class-qualifier class) (car qualifier)) (if (and root-class (not (equal class root-class))) @@ -194,14 +201,18 @@ (setf (key-slots class) (remove-if-not (lambda (slot) (eql (slot-value slot 'db-kind) :key)) - (ordered-class-slots class))))) + (if (normalizedp class) + (ordered-class-direct-slots class) + (ordered-class-slots class)))))) #+(or sbcl allegro) (defmethod finalize-inheritance :after ((class standard-db-class)) (setf (key-slots class) (remove-if-not (lambda (slot) (eql (slot-value slot 'db-kind) :key)) - (ordered-class-slots class)))) + (if (normalizedp class) + (ordered-class-direct-slots class) + (ordered-class-slots class))))) ;; return the deepest view-class ancestor for a given view class @@ -294,7 +305,13 @@ column definition in the database.") :accessor specified-type :initarg specified-type :initform nil - :documentation "Internal slot storing the :type specified by user."))) + :documentation "Internal slot storing the :type specified by user.") + (autoincrement-sequence + :accessor view-class-slot-autoincrement-sequence + :initarg :autoincrement-sequence + :initform nil + :documentation "A string naming the (possibly automatically generated) sequence +for a slot with an :auto-increment constraint."))) (defparameter *db-info-lambda-list* '(&key join-class @@ -405,7 +422,7 @@ implementations." specified-type)))) (if (and type (not (member :not-null (listify db-constraints)))) `(or null ,type) - type))) + (or type t)))) ;; Compute the slot definition for slots in a view-class. Figures out ;; what kind of database value (if any) is stored there, generates and @@ -419,12 +436,7 @@ implementations." list)) (declaim (inline delistify-dsd)) -(defun delistify-dsd (list) - "Some MOPs, like openmcl 0.14.2, cons attribute values in a list." - (if (and (listp list) (null (cdr list))) - (car list) - list)) - +;; there is an :after method below too (defmethod initialize-instance :around ((obj view-class-direct-slot-definition) &rest initargs &key db-constraints db-kind type &allow-other-keys) @@ -433,10 +445,20 @@ implementations." (slot-definition-name obj))) (apply #'call-next-method obj 'specified-type type - :type (compute-lisp-type-from-specified-type - type db-constraints) + :type (if (and (eql db-kind :virtual) (null type)) + t + (compute-lisp-type-from-specified-type + type db-constraints)) initargs)) +(defun compute-column-name (arg) + (database-identifier arg nil)) + +(defmethod initialize-instance :after + ((obj view-class-direct-slot-definition) + &key &allow-other-keys) + (setf (view-class-slot-column obj) (compute-column-name obj))) + (defmethod compute-effective-slot-definition ((class standard-db-class) #+kmr-normal-cesd slot-name direct-slots) @@ -448,15 +470,7 @@ implementations." (let ((esd (call-next-method))) (typecase dsd (view-class-slot-definition-mixin - ;; Use the specified :column argument if it is supplied, otherwise - ;; the column slot is filled in with the slot-name, but transformed - ;; to be sql safe, - to _ and such. - (setf (slot-value esd 'column) - (column-name-from-arg - (if (slot-boundp dsd 'column) - (delistify-dsd (view-class-slot-column dsd)) - (column-name-from-arg - (sql-escape (slot-definition-name dsd)))))) + (setf (slot-value esd 'column) (compute-column-name dsd)) (setf (slot-value esd 'db-type) (when (slot-boundp dsd 'db-type) @@ -504,6 +518,14 @@ implementations." (setf (specified-type esd) (delistify-dsd (specified-type dsd))) + ;; In older SBCL's the type-check-function is computed at + ;; defclass expansion, which is too early for the CLSQL type + ;; conversion to take place. This gets rid of it. It's ugly + ;; but it's better than nothing -wcp10/4/10. + #+(and sbcl #.(cl:if (cl:and (cl:find-package :sb-pcl) + (cl:find-symbol "%TYPE-CHECK-FUNCTION" :sb-pcl)) + '(cl:and) '(cl:or))) + (setf (slot-value esd 'sb-pcl::%type-check-function) nil) ) ;; all other slots @@ -519,10 +541,8 @@ implementations." #+openmcl (setf (slot-value esd 'ccl::type-predicate) type-predicate))) - (setf (slot-value esd 'column) - (column-name-from-arg - (sql-escape (slot-definition-name dsd)))) - + ;; has no column name if it is not a database column + (setf (slot-value esd 'column) nil) (setf (slot-value esd 'db-info) nil) (setf (slot-value esd 'db-kind) :virtual) (setf (specified-type esd) (slot-definition-type dsd))) @@ -537,8 +557,11 @@ implementations." result)) (defun slotdef-for-slot-with-class (slot class) - (find-if #'(lambda (d) (eql slot (slot-definition-name d))) - (class-slots class))) + (typecase slot + (standard-slot-definition slot) + (symbol + (find-if #'(lambda (d) (eql slot (slot-definition-name d))) + (class-slots class))))) #+ignore (eval-when (:compile-toplevel :load-toplevel :execute) @@ -549,3 +572,22 @@ implementations." #+kmr-normal-esdc (setq cl:*features* (delete :kmr-normal-esdc cl:*features*)) ) + +(defmethod database-identifier ( (name standard-db-class) + &optional database find-class-p) + "the majority of this function is in expressions.lisp + this is here to make loading be less painful (try-recompiles) in SBCL" + (database-identifier (view-table name) database)) + +(defmethod database-identifier ((name view-class-slot-definition-mixin) + &optional database find-class-p) + (database-identifier + (if (slot-boundp name 'column) + (delistify-dsd (view-class-slot-column name)) + (slot-definition-name name)) + database)) + +(defun find-standard-db-class (name &aux cls) + (and (setf cls (ignore-errors (find-class name))) + (typep cls 'standard-db-class) + cls))