Next: , Previous: Types Conversion, Up: Top


12 Column Definitions

A column definition, or def for short, associates a column name, its type and any additional options. It has the form:

     (NAME TYPE [OPTIONS...])

name and type are symbols specifying the name and type of the column, respectively (see Types Conversion). options are strings passed directly to PostgreSQL as part of a CREATE TABLE command. For example, the status output of the rsync(1) program can be specified by the form:

     (define rsync-defs
             '((time            timestamp)
               (error_condition text)
               (files           text[])
               (wrote           int4)          ; bytes
               (read            text[][])
               (rate            float4)
               (total           int4)          ; bytes
               (speedup         float4)
               (etc             int4[])))

Likewise, here is an example that might be useful in keeping a table of expenses (although probably using float4 is not a good idea for monetary values):

     (define expense-ledger-defs
             '((i       serial)
               (date    timestamp)
               (amount  float4)
               (details text[])))

Note that there are no options in these examples. The components of a def can be extracted with procedures in the postgres-col-defs module, which can be loaded like so:

     (use-modules ((database postgres-col-defs)
                   #:renamer (symbol-prefix-proc 'def:)))

In this example, we use the #:renamer clause to systematically prefix "def:" to the names that the client module would see (resulting in def:column-name and so on).

— Procedure: column-name def

Extract column name, a symbol, from def.

— Procedure: type-name def

Extract type name, a symbol, from def.

— Procedure: type-options def

Extract type options, a list, from def. Each option element is either a string (possibly with embedded spaces), or a sub-list of numbers and/or symbols. Typically the sub-list, if any, will be the first option element.

— Procedure: validate-def obj [typecheck]

Check obj and signal error if it does not appear to be a well-formed def. Check that obj has a structure amenable to extraction of components using column-name and type-name: The name must be a symbol using only alphanumeric characters and underscore; the type must be a symbol. Optional second arg typecheck is a procedure that takes the type (a symbol) and can do further checks on it. It should return non-#f to indicate success.

There are two more convenience procedures, the first one useful in transforming the results of a query into Scheme objects (see Result Transforms):

— Procedure: objectifiers defs

Return a list of objectifiers associated with the types in defs.

— Procedure: stringifiers defs

Return a list of stringifiers associated with the types in defs.