chickadee » postgresql » default-type-parsers

default-type-parsers #!optional ALISTparameter

Postgres result values are always just strings, but it is possible to map these to real Scheme objects. With this parameter, you can map your own custom postgresql datatype to Scheme datatypes.

The alist is a mapping of Postgres type names (strings) to procedures accepting a string and returning a Scheme object of the desired type.

The parsers can also be set per connection with the TYPE-PARSERS argument of the connect procedure.

(use postgresql)

(parameterize ((default-type-parsers `(("text" . ,string->symbol))))
  (let ((conn (connect '((dbname . test)))))
    (symbol? (value-at (query conn "SELECT 'hello'::text")))))
 => #t

The default parsers look like this:

`(("text" . ,identity)
  ("bytea" . ,bytea-parser)
  ("char" . ,char-parser)
  ("bpchar" . ,identity)
  ("bool" . ,bool-parser)
  ("int8" . ,numeric-parser)
  ("int4" . ,numeric-parser)
  ("int2" . ,numeric-parser)
  ("float4" . ,numeric-parser)
  ("float8" . ,numeric-parser)
  ("numeric" . ,numeric-parser)
  ("oid" . ,numeric-parser)
  ("record" . ,(make-composite-parser (circular-list identity))))

These parsers are described below. For anything where no parser is found, the value is returned verbatim (which is always a string, or a blob in case of binary data).

Array and composite (row) types are automatically handled; unless a type-specific parser is defined, a parser is automatically created by combining the parsers for their constituent elements.