r5401: *** empty log message ***
[hyperobject.git] / mop.lisp
index 19e2743b011c7c5ab6183a6e349da03dccae7a48..817c29c159572c366d0f324afaad9895e76d95bb 100644 (file)
--- a/mop.lisp
+++ b/mop.lisp
@@ -11,7 +11,7 @@
 ;;;; in Text, HTML, and XML formats. This includes hyperlinking\r
 ;;;; capability and sub-objects.\r
 ;;;;\r
-;;;; $Id: mop.lisp,v 1.76 2003/06/06 21:59:29 kevin Exp $\r
+;;;; $Id: mop.lisp,v 1.85 2003/07/29 20:49:05 kevin Exp $\r
 ;;;;\r
 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg\r
 ;;;; *************************************************************************\r
    (version :initarg :version :initform nil\r
            :accessor version\r
            :documentation "Version number for class")\r
-   (sql-name :initarg :sql-name :initform nil)\r
+   (closures :initarg :closures :initform nil\r
+            :accessor closures\r
+            :documentation "Closures to call on slot chnages")\r
+   (sql-name :initarg :sql-name :accessor sql-name :initform nil\r
+            :documentation "SQL Name for this class")\r
+   (guid :initarg :guid :accessor guid :initform nil\r
+        :documentation "ID string for this class")\r
 \r
    ;;; The remainder of these fields are calculated one time\r
    ;;; in finalize-inheritence.\r
@@ -50,6 +56,9 @@
    (direct-rules :type list :initform nil :initarg :direct-rules\r
                 :accessor direct-rules\r
                 :documentation "List of rules to fire on slot changes.")\r
+   (direct-views :type list :initform nil :initarg :direct-views\r
+                :accessor direct-views\r
+                :documentation "List of views")\r
    (class-id :type integer :initform nil\r
             :accessor class-id\r
             :documentation "Unique ID for the class")\r
   (:documentation "Metaclass for Markup Language classes."))\r
 \r
 (defclass subobject ()\r
-  ((name-class :type symbol :initform nil :initarg :name-class :reader name-class)\r
-   (name-slot :type symbol :initform nil :initarg :name-slot :reader name-slot)\r
-   (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)\r
-   (lookup-keys :type list :initform nil :initarg :lookup-keys\r
-               :reader lookup-keys))\r
-  (:documentation "Contains subobject information"))\r
+  ((name-class :type symbol :initarg :name-class :reader name-class)\r
+   (name-slot :type symbol :initarg :name-slot :reader name-slot)\r
+   (subobj-class :type symbol :initarg :subobj-class :reader subobj-class)\r
+   (lookup :type (or function symbol) :initarg :lookup :reader lookup)\r
+   (lookup-keys :type list :initarg :lookup-keys :reader lookup-keys))\r
+  (:documentation "subobject information")\r
+  (:default-initargs :name-class nil :name-slot nil :subobj-class nil\r
+                    :lookup nil :lookup-keys nil))\r
 \r
 \r
 (defmethod print-object ((obj subobject) (s stream))\r
   (print-unreadable-object (obj s :type t :identity t)\r
     (format s "~S" (name obj))))\r
 \r
-#+(or cmu scl sbcl)\r
 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))\r
   t)\r
 \r
 (defmethod finalize-inheritance :after ((cl hyperobject-class))\r
+  ;; Work-around needed for OpenMCL\r
+  #+ignore\r
+  (unless (find-class (class-name cl))\r
+    (setf (find-class (class-name cl)) cl))\r
+  \r
   (init-hyperobject-class cl)\r
   )\r
 \r
       (when (and ,required (null value))\r
        (error "hyperobject class slot ~A must have a value" name))\r
       (list name `',value))\r
-    #+(or allegro sbcl cmu scl)\r
+    #+(or allegro sbcl cmu scl openmcl)\r
     (declare (ignore slot-name required))\r
     )\r
 \r
     (atom\r
      (ensure-keyword vt))\r
     (cons\r
-     (cons (ensure-keyword (car vt)) (cdr vt)))\r
+     (list (ensure-keyword (car vt)) (cadr vt)))\r
     (t\r
      t)))\r
 \r
-#+ignore\r
-(defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)\r
-  #+allegro (declare (ignore name))\r
+(defmethod compute-effective-slot-definition :around ((cl hyperobject-class)\r
+                                                     #+ho-normal-cesd name\r
+                                                     dsds)\r
+  (declare (ignore #+ho-normal-cesd name))\r
+  (let ((esd (call-next-method)))\r
+    (if (typep esd 'hyperobject-esd)\r
+       (compute-hyperobject-esd esd dsds)\r
+       esd)))\r
+\r
+(defun compute-hyperobject-esd (esd dsds)\r
   (let* ((dsd (car dsds))\r
         (value-type (canonicalize-value-type (slot-value dsd 'value-type))))\r
-    (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)\r
-      (setf (slot-value dsd 'sql-type) sql-type)\r
-      (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))\r
-      (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))\r
-       (apply\r
-        #'make-instance 'hyperobject-esd \r
-        :value-type value-type\r
-        :sql-type sql-type\r
-        :length length\r
-        :print-formatter (slot-value dsd 'print-formatter)\r
-        :subobject (slot-value dsd 'subobject)\r
-        :hyperlink (slot-value dsd 'hyperlink)\r
-        :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)\r
-        :description (slot-value dsd 'description)\r
-        :user-name (slot-value dsd 'user-name)\r
-        :user-name-plural (slot-value dsd 'user-name-plural)\r
-        :index (slot-value dsd 'index)\r
-        :value-constraint (slot-value dsd 'value-constraint)\r
-        :null-allowed (slot-value dsd 'null-allowed)\r
-        ia)))))\r
-\r
-(defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)\r
-  #+ho-normal-cesd (declare (ignore name))\r
-  (let* ((esd (call-next-method))\r
-        (dsd (car dsds))\r
-        (value-type (canonicalize-value-type (slot-value dsd 'value-type))))\r
-    (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)\r
-      (setf (slot-value esd 'sql-type) sql-type)\r
-      (setf (slot-value esd 'length) length)\r
-      (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))\r
-      (setf (slot-value esd 'value-type) value-type)\r
-      (setf (slot-value esd 'user-name)\r
-           (aif (slot-value dsd 'user-name)\r
-                it\r
-                (string-downcase (symbol-name (slot-definition-name dsd)))))\r
-      (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters\r
-                     description value-constraint index null-allowed))\r
-       (setf (slot-value esd name) (slot-value dsd name)))\r
-      esd)))\r
-\r
+    (multiple-value-bind (sql-type sql-length) \r
+       (value-type-to-sql-type value-type)\r
+      (setf (esd-sql-type esd) sql-type)\r
+      (setf (esd-sql-length esd) sql-length))\r
+    (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))\r
+    (setf (esd-value-type esd) value-type)\r
+    (setf (esd-user-name esd)\r
+         (aif (dsd-user-name dsd)\r
+              it\r
+              (string-downcase (symbol-name (slot-definition-name dsd)))))\r
+    (setf (esd-sql-name esd)\r
+         (aif (dsd-sql-name dsd)\r
+              it\r
+              (lisp-name-to-sql-name (slot-definition-name dsd))))\r
+    (setf (esd-sql-name esd)\r
+         (aif (dsd-sql-name dsd)\r
+              it\r
+              (lisp-name-to-sql-name (slot-definition-name dsd))))\r
+    (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters\r
+                   description value-constraint indexed null-allowed\r
+                   unique short-description void-text read-only-groups\r
+                   hidden-groups unit disable-predicate view-type\r
+                   list-of-values stored))\r
+      (setf (slot-value esd name) (slot-value dsd name)))\r
+    esd))\r
+\r
+(defun lisp-name-to-sql-name (lisp)\r
+  "Convert a lisp name (atom or list, string or symbol) into a canonical\r
+SQL name"\r
+  (unless (stringp lisp)\r
+    (setq lisp\r
+         (typecase lisp\r
+           (symbol (symbol-name lisp))\r
+           (t (write-to-string lisp)))))\r
+  (do* ((len (length lisp))\r
+       (sql (make-string len))\r
+       (i 0 (1+ i)))\r
+      ((= i len) (string-upcase sql))\r
+    (declare (fixnum i)\r
+            (simple-string sql))\r
+    (setf (schar sql i)\r
+         (let ((c (char lisp i)))\r
+           (case c\r
+             ((#\- #\$ #\+ #\#) #\_)\r
+             (otherwise c))))))\r
 \r
 #+ho-normal-cesd\r
 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))\r
   (or (eq type 'string)\r
       (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))\r
 \r
+(defun base-value-type (value-type)\r
+  (if (atom value-type)\r
+      value-type\r
+    (car value-type)))\r
+\r
 (defun value-type-to-lisp-type (value-type)\r
-  (case (if (atom value-type)\r
-           value-type\r
-           (car value-type))\r
+  (case (base-value-type value-type)\r
     ((:string :cdata :varchar :char)\r
      '(or null string))\r
     (:character\r
      '(or null fixnum))\r
     (:boolean\r
      '(or null boolean))\r
-    (:integer\r
+    ((:integer :long-integer)\r
      '(or null integer))\r
     ((:float :single-float)\r
      '(or null single-float))\r
 \r
 (defun value-type-to-sql-type (value-type)\r
   "Return two values, the sql type and field length."\r
-  (let ((type (if (atom value-type)\r
-                 value-type\r
-                 (car value-type)))\r
+  (let ((type (base-value-type value-type))\r
        (length (when (consp value-type)\r
                  (cadr value-type))))\r
     (values\r
      (case type\r
-       ((:string :cdata)\r
-       :string)\r
+       ((:char :character)\r
+       :char)\r
+       (:varchar\r
+       :varchar)\r
        ((:fixnum :integer)\r
        :integer)\r
+       (:long-integer\r
+       :long-integer)\r
        (:boolean\r
        :boolean)\r
        ((:float :single-float)\r
 \r
 ;;;; Class initialization function\r
 \r
-;; defines a slot-unbound method for class and slot-name, fills\r
-;; the slot by calling reader function with the slot values of\r
-;; the instance's reader-keys\r
-(defmacro def-lazy-reader (class slot-name reader &rest reader-keys)\r
-  (let* ((the-slot-name (gensym))\r
-        (the-class (gensym))\r
-        (the-instance (gensym))\r
-        (keys '()))\r
-    (dolist (key reader-keys)\r
-      (push (list 'slot-value the-instance (list 'quote key)) keys))\r
-    (setq keys (nreverse keys))\r
-    `(defmethod slot-unbound (,the-class (,the-instance ,class)\r
-                             (,the-slot-name (eql ',slot-name)))\r
-      (declare (ignore ,the-class))\r
-      (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))\r
-\r
-\r
-#+lispworks\r
-(defun intern-eql-specializer (slot)\r
-  `(eql ,slot))\r
-\r
-#+(or sbcl cmu lispworks)\r
-(defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)\r
-  (let ((keys nil)\r
-       (gf (ensure-generic-function 'slot-unbound)))\r
-    (dolist (key reader-keys)\r
-      (push (list 'slot-value 'the-instance (list 'quote key)) keys))\r
-    (setq keys (nreverse keys))\r
-    (multiple-value-bind (method-lambda init-args-values)\r
-       (make-method-lambda\r
-        gf\r
-        (class-prototype (generic-function-method-class gf))\r
-        #-lispworks\r
-        `(lambda (the-class the-instance the-slot-name)\r
-          (declare (ignore the-class))\r
-          (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))\r
-        #+lispworks\r
-        '(the-class the-instance the-slot-name)\r
-        #+lispworks\r
-        nil\r
-        #+lispworks\r
-        `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))\r
-        nil)\r
-      (add-method gf\r
-                 (apply\r
-                  #'make-instance (generic-function-method-class gf)\r
-                  ':specializers (list (class-of (find-class class-name))\r
-                                       (find-class class-name)\r
-                                       (intern-eql-specializer slot-name))\r
-                  ':lambda-list '(the-class the-instance the-slot-name)\r
-                  ':function (compile nil method-lambda)\r
-                  init-args-values)))))\r
-\r
-#+(or allegro scl)\r
-(progn\r
-  ;; One entry for each class with lazy readers defined.  The value is a plist mapping\r
-  ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.\r
-  (defvar *lazy-readers* (make-hash-table))\r
+;; One entry for each class with lazy readers defined.  The value is a plist mapping\r
+;; slot-name to a lazy reader, each of which is a list of a function and slot-names.\r
+(defvar *lazy-readers* (make-hash-table))\r
 \r
 (defmethod slot-unbound :around ((class hyperobject-class) instance slot-name)\r
   (let ((lazy-reader (loop for super in (class-precedence-list class)\r
-                          as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)\r
-                          when lazy-reader return it)))\r
+                        as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)\r
+                        when lazy-reader return it)))\r
     (if lazy-reader\r
        (setf (slot-value instance slot-name)\r
-             (apply (car lazy-reader)\r
-                    (loop for arg-slot-name in (cdr lazy-reader)\r
-                          collect (slot-value instance arg-slot-name))))\r
+             (if (atom lazy-reader)\r
+                 (make-instance lazy-reader)\r
+                 (apply (car lazy-reader)\r
+                        (loop for arg-slot-name in (cdr lazy-reader)\r
+                              collect (slot-value instance arg-slot-name)))))\r
        ;; No lazy reader -- defer to regular slot-unbound handling.\r
        (call-next-method))))\r
 \r
-  ;; The reader is a function and the reader-keys are slot names.  The slot is lazily set to\r
-  ;; the result of applying the function to the slot-values of those slots, and that value\r
-  ;; is also returned.\r
-  (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)\r
-    (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)\r
-         (list* reader (copy-list reader-keys))))\r
+;; The reader is a function and the reader-keys are slot names.  The slot is lazily set to\r
+;; the result of applying the function to the slot-values of those slots, and that value\r
+;; is also returned.\r
+(defun ensure-lazy-reader (cl class-name slot-name subobj-class reader \r
+                          &rest reader-keys)\r
+  (setf (getf (gethash cl *lazy-readers*) slot-name)\r
+    (aif subobj-class\r
+        it\r
+        (list* reader (copy-list reader-keys)))))\r
 \r
-  (defun remove-lazy-reader (class-name slot-name)\r
-    (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)\r
-         nil))\r
-  \r
-  ) ;; #+(or allegro scl)\r
+(defun remove-lazy-reader (class-name slot-name)\r
+  (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)\r
+    nil))\r
 \r
 \r
 (defun finalize-subobjects (cl)\r
                    (make-instance 'subobject\r
                                   :name-class (class-name cl)\r
                                   :name-slot (slot-definition-name slot)\r
-                                  :lookup (if (atom subobj-def)\r
-                                              subobj-def\r
-                                              (car subobj-def))\r
-                                  :lookup-keys (if (atom subobj-def)\r
-                                                   nil\r
-                                                   (cdr subobj-def)))))\r
+                                  :subobj-class (when (atom subobj-def)\r
+                                                  subobj-def)\r
+                                  :lookup (when (listp subobj-def)\r
+                                            (car subobj-def))\r
+                                  :lookup-keys (when (listp subobj-def)\r
+                                                 (cdr subobj-def)))))\r
               (unless (eq (lookup subobject) t)\r
-                #+ignore ;; #-(or sbcl cmu lispworks)\r
-                (eval\r
-                 `(hyperobject::def-lazy-reader ,(name-class subobject)\r
-                   ,(name-slot subobject) ,(lookup subobject)\r
-                   ,@(lookup-keys subobject)))\r
-                #+(or sbcl cmu lispworks allegro scl)\r
-                (apply #'ensure-lazy-reader \r
+                (apply #'ensure-lazy-reader\r
+                       cl\r
                        (name-class subobject) (name-slot subobject)\r
-                       (lookup subobject) (lookup-keys subobject)))\r
-              (push subobject subobjects))))\r
+                       (subobj-class subobject)\r
+                       (lookup subobject) (lookup-keys subobject))\r
+                (push subobject subobjects)))))\r
          ;; sbcl/cmu reverse class-slots compared to the defclass form\r
          ;; so re-reverse on cmu/sbcl\r
          #+(or cmu sbcl) subobjects\r
          #-(or cmu sbcl) (nreverse subobjects)\r
          )))\r
 \r
-\r
 (defun finalize-class-slots (cl)\r
   "Make sure all class slots have an expected value"\r
   (unless (user-name cl)\r
                                   (user-name cl))\r
                    2)))\r
 \r
-  (dolist (name '(user-name description))\r
+  (dolist (name '(user-name description version guid sql-name))\r
     (awhen (slot-value cl name)\r
           (setf (slot-value cl name)\r
-                (etypecase (slot-value cl name)\r
-                  (cons (car it))\r
-                  ((or string symbol) it))))))\r
+            (if (listp it)\r
+                (car it)\r
+              it))))\r
+  \r
+  (unless (sql-name cl)\r
+    (setf (sql-name cl) (lisp-name-to-sql-name (class-name cl))))\r
+  )\r
 \r
 (defun finalize-documentation (cl)\r
   "Calculate class documentation slot"\r