[postmodern-devel] one-way array/record/coercion support

Daniel Janus dj at danieljanus.pl
Wed Mar 3 01:53:11 UTC 2010


Dear list,

first of all, many thanks to Marijn and all the contributors for the 
excellent piece of code that is Postmodern.

In my project I'm using it to connect to a database that heavily uses
some of the advanced PostgreSQL functionality, including PL/pgSQL functions
taking arrays of records as arguments.  I found S-SQL to be a little
lacking in expressiveness for that, so I've quickly hacked some methods
that let me pass Lisp vectors to SQL and QUERY (they will be rendered
into PostgreSQL array literals):

;;; code starts here

(in-package :cl-postgres)

(defmethod to-sql-string ((arg vector))
  (cond
    ((typep arg '(vector (unsigned-byte 8))) (values (escape-bytes arg) t))
    (t
     (format nil "ARRAY[~{~A~^, ~}]" (mapcar #'s-sql::sql-ize (coerce arg 'list))))))

(defmethod to-sql-string ((arg list))
  (format nil "(~{~A~^, ~})" (mapcar #'s-sql::sql-ize arg)))

(in-package :s-sql)

(def-sql-op :coerce (form type)
  `(,@(sql-expand form) "::" ,type))

;;; code ends here

I'm sending it along in case it is useful to anyone else. Or perhaps it 
might make its way to the main repo?  For the latter, presumably a full
support would be needed, but I didn't find time to dive into Postmodern's
way of passing back PostgreSQL-returned data into Lisp values of proper
types, as I currently don't need that.

Best regards,
Daniel Janus





More information about the postmodern-devel mailing list