Next: , Previous: Displaying Results, Up: Top   [Contents][Index]


11 Types Conversion

Normally, the result of a pg-exec query is an object from which Scheme strings can be extracted to represent the datum from a particular tuple/field coordinate, using pg-getvalue (see Retrieving Data). If this string is sufficient for your needs, you can skip this chapter, which describes how to use the PostgreSQL-to-Scheme (and back) type conversion support installed with Guile-PG, including how to register new converters.

To get started, load the postgres-types module:

(use-modules (database postgres-types))

But before we get to conversions, recall that the pg-ftype example (see Processing Results) defines a procedure that returns the name of a PostgreSQL type by doing a query on the pg_type (internal) table. Here is a more efficient implementation:

(define CONN (pg-connectdb ...))

(define (pg-ftype-name result fnum)
  (assq-ref (oid-type-name-cache CONN)
            (pg-ftype result fnum)))

It uses a bonus procedure in this module:

Procedure: oid-type-name-cache conn [fresh?]

Query connection conn for oid/type-name info, caching results. Optional arg fresh? non-#f a (re-)query, updating the cache. Return a list of oid/type-name (number/string) pairs.