db-writer, db-reader

Ng Pheng Siong <[email protected]> Fri, 6 Dec 2002 18:25:32 +0800
Newsgroups gmane.lisp.uncommon-sql
Message-ID <[email protected]>
Hi,

I'm trying out the db-writer/reader interface in USQL and not having much
luck.

USQL and ODCL are freshly downloaded from CVS.

I have the following:

  (defun db-writer (value)
    (princ-to-string value))
  
  (defun db-reader (value)
    (let ((*read-eval* nil))
      (read-from-string value)))
  
  (def-view-class blob ()
    ((id 
      :db-kind :key :type integer :initarg :id :reader id)
     (data
      :accessor data :initarg :data 
      :db-writer #'db-writer 
      :db-reader #'db-reader))
    (:base-table blob))
  
  (defvar b (make-instance 'blob
                           :id 1
                           :data '((a . "a") (b . "2"))))
  
(store-instance b) craps out:
  
  Type-error in KERNEL::OBJECT-NOT-LIST-ERROR-HANDLER:
  "a" is not of type LIST
  
  Restarts:
    0: [ABORT] Return to Top-Level.
  
  Debug  (type H for help)
  
  (MAISQL-SYS::|(PCL::FAST-METHOD OUTPUT-SQL (LIST))|
     #<unused-arg>
     #<unused-arg> (A . "a")
     (#<MAISQL-POSTGRESQL:POSTGRESQL-DATABASE {488BB06D}>))
  Source: 
  ; File: /usr/local/pkg/lisp/uncommonsql/sql/sql.lisp
  (CDR ITEM)
  0]


I traced this to sql/objects.lisp:

  (defun db-value-from-slot (slotdef val database)
    (let ((dbwriter (view-class-slot-db-writer slotdef))
  	(dbtype (slot-definition-type slotdef)))
      (typecase dbwriter
        (string (format nil dbwriter val))
        (function (apply dbwriter (list val)))
        (t
         (typecase dbtype
  	   (cons
  	    (database-output-sql-as-type (car dbtype) val database))
  	   (t
  	    (database-output-sql-as-type dbtype val database)))))))


Going into the debugger just before TYPECASE DBWRITER gives this:

  Debug  (type H for help)
  
  (MAISQL-SYS::DB-VALUE-FROM-SLOT
   #<View-Class-Effective-Slot-Definition DATA {48B5E185}>
   ((A . 1) (B . "b"))
   #<MAISQL-POSTGRESQL:POSTGRESQL-DATABASE {488BB06D}>)
  Source: (ERROR #'INVOKE-DEBUGGER)
  0] l
  MAISQL-SYS:DATABASE  =  #<MAISQL-POSTGRESQL:POSTGRESQL-DATABASE {488BB06D}>
  MAISQL-SYS::DBTYPE  =  T
  MAISQL-SYS::DBWRITER  =  #'DB-WRITER
  MAISQL-SYS::SLOTDEF = #<View-Class-Effective-Slot-Definition DATA {48B5E185}>
  MAISQL-SYS::VAL  =  ((A . 1) (B . "b"))

  0] (typecase maisql-sys::dbwriter
       (string (print "string"))
       (function (print "function"))
       (t (print "whatever")))
  "whatever" 
  "whatever"


Hints appreciated. Cheers.

-- 
Ng Pheng Siong <[email protected]> * http://www.netmemetic.com