Next: , Previous: Column Definitions, Up: Top


13 Result Transforms

To support handling of query results in the Scheme domain, Guile-PG provides various procedures to walk the result object, to extract Scheme objects from the result object, and to collect these in idiomatic data structures (such as the association list).

To get started, load the postgres-resx module:

     (use-modules (database postgres-resx))

The most basic procedure is for-each-tuple, useful for quick searches on the unconverted result strings.

[NOTE: docs missing for for-each-tuple]

The rest of the procedures in this chapter combine conversion of the raw result strings to Scheme objects, with different collection methods. See Types Conversion. (For access to the unconverted result strings, use the procedure identity as the objectifier, which in effect does no conversion at all.)

The degenerate collection method is no collection at all! (A common idiom is to SELECT EXPR; to get a result consisting of one row and one column.)

[NOTE: docs missing for object<-result]

For other collection methods, we include examples to clarify. The examples are based upon a result object that can briefly described as:

The defs (see Column Definitions) for the examples are:

     (define defs '((date timestamp) (note text[])))

Actually the note field in its raw form involves curly braces and extra quoting, but we omit that to reduce clutter.

The result-field->object-list procedure is useful for single-column queries.

[NOTE: docs missing for result-field->object-list]

Example

     (define (1-col result fn type)
       (let ((objectifier (dbcoltype:objectifier (dbcoltype-lookup type))))
         (result-field->object-list result fn objectifier)))
     
     (1-col result 0 'timestamp)
     ⇒ (1042531200 1042444800)
     
     (1-col result 1 'text[])
     ⇒ (("now" "the present") ("yesterday" "the past"))

The result->object-FOO procedures have the same signature but return the data in different ways.

[NOTE: docs missing for result->object-alist]

[NOTE: docs missing for result->object-alists]

[NOTE: docs missing for result->object-rows]

Example

     (use-modules ((database postgres-col-defs) #:select (objectifiers)))
     
     (define odefs (objectifiers defs))
     
     (result->object-alist result odefs)
     ⇒
     ((date 1042531200 1042444800)
      (note ("now" "the present") ("yesterday" "the past")))
     
     (result->object-alists result odefs)
     ⇒
     (((date . 1042531200) (note "now" "the present"))
      ((date . 1042444800) (note "yesterday" "the past")))