r9169: allow :metaclass in def-view-class
[clsql.git] / sql / objects.lisp
index e1bc241e09cdf26c4be92f3e9d1ff7517052063a..d8181d1fb763b6ae6034cd4a3d7c4a6dc631a4bb 100644 (file)
 (defmethod database-pkey-constraint ((class standard-db-class) database)
   (let ((keylist (mapcar #'view-class-slot-column (keyslots-for-class class))))
     (when keylist 
-      (format nil "CONSTRAINT ~APK PRIMARY KEY~A"
-              (database-output-sql (view-table class) database)
-              (database-output-sql keylist database)))))
+      (convert-to-db-default-case
+       (format nil "CONSTRAINT ~APK PRIMARY KEY~A"
+              (database-output-sql (view-table class) database)
+              (database-output-sql keylist database))
+       database))))
 
 
 (defun create-view-from-class (view-class-name
@@ -157,7 +159,7 @@ returns a list of all the classes connected to the default database,
 ;; Define a new view class
 ;;
 
-(defmacro def-view-class (class supers slots &rest options)
+(defmacro def-view-class (class supers slots &rest cl-options)
   "Extends the syntax of defclass to allow special slots to be mapped
 onto the attributes of database views. The macro DEF-VIEW-CLASS
 creates a class called CLASS which maps onto a database view. Such a
@@ -170,9 +172,11 @@ instances are filled with attribute values from the database. If
 SUPERS is nil then STANDARD-DB-OBJECT automatically becomes the
 superclass of the newly-defined View Class."
   `(progn
-     (defclass ,class ,supers ,slots ,@options
-              (:metaclass standard-db-class))
-     (finalize-inheritance (find-class ',class))))
+    (defclass ,class ,supers ,slots 
+      ,@(if (find :metaclass `,cl-options :key #'car)
+           `,cl-options
+           (cons '(:metaclass clsql-sys::standard-db-class) `,cl-options)))
+    (finalize-inheritance (find-class ',class))))
 
 (defun keyslots-for-class (class)
   (slot-value class 'key-slots))
@@ -234,6 +238,7 @@ superclass of the newly-defined View Class."
     (let ((cdef
            (list (sql-expression :attribute (view-class-slot-column slotdef))
                  (slot-type slotdef))))
+      (setf cdef (append cdef (list (view-class-slot-db-type slotdef))))
       (let ((const (view-class-slot-db-constraints slotdef)))
         (when const 
           (setq cdef (append cdef (list const)))))
@@ -543,7 +548,8 @@ DATABASE-NULL-VALUE on the type of the slot."))
 
 (defmethod database-get-type-specifier (type args database)
   (declare (ignore type args))
-  (if (member (database-type database) '(:postgresql :postgresql-socket))
+  (if (clsql-base-sys::in (database-underlying-type database)
+                         :postgresql :postgresql-socket)
           "VARCHAR"
           "VARCHAR(255)"))
 
@@ -558,31 +564,32 @@ DATABASE-NULL-VALUE on the type of the slot."))
                                         database)
   (if args
       (format nil "VARCHAR(~A)" (car args))
-      (if (member (database-type database) '(:postgresql :postgresql-socket))
-          "VARCHAR"
-          "VARCHAR(255)")))
+    (if (clsql-base-sys::in (database-underlying-type database) 
+                           :postgresql :postgresql-socket)
+       "VARCHAR"
+      "VARCHAR(255)")))
 
 (defmethod database-get-type-specifier ((type (eql 'simple-string)) args
                                         database)
   (if args
       (format nil "VARCHAR(~A)" (car args))
-      (if (member (database-type database) '(:postgresql :postgresql-socket))
-          "VARCHAR"
-          "VARCHAR(255)")))
+    (if (clsql-base-sys::in (database-underlying-type database) 
+                           :postgresql :postgresql-socket)
+       "VARCHAR"
+      "VARCHAR(255)")))
 
 (defmethod database-get-type-specifier ((type (eql 'string)) args database)
   (if args
       (format nil "VARCHAR(~A)" (car args))
-      (if (member (database-type database) '(:postgresql :postgresql-socket))
-          "VARCHAR"
-          "VARCHAR(255)")))
+    (if (clsql-base-sys::in (database-underlying-type database) 
+                           :postgresql :postgresql-socket)
+       "VARCHAR"
+      "VARCHAR(255)")))
 
 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database)
   (declare (ignore args))
-  (case (database-type database)
-    (:postgresql
-     "TIMESTAMP WITHOUT TIME ZONE")
-    (:postgresql-socket
+  (case (database-underlying-type database)
+    ((:postgresql :postgresql-socket)
      "TIMESTAMP WITHOUT TIME ZONE")
     (:mysql
      "DATETIME")
@@ -701,13 +708,14 @@ DATABASE-NULL-VALUE on the type of the slot."))
 (defmethod read-sql-value (val (type (eql 'keyword)) database)
   (declare (ignore database))
   (when (< 0 (length val))
-    (intern (string-upcase val) "KEYWORD")))
+    (intern (symbol-name-default-case val) 
+           (find-package '#:keyword))))
 
 (defmethod read-sql-value (val (type (eql 'symbol)) database)
   (declare (ignore database))
   (when (< 0 (length val))
-    (unless (string= val "NIL")
-      (intern (string-upcase val)
+    (unless (string= val (clsql-base-sys:symbol-name-default-case "NIL"))
+      (intern (clsql-base-sys:symbol-name-default-case val)
               (symbol-package *update-context*)))))
 
 (defmethod read-sql-value (val (type (eql 'integer)) database)