Previous: Selected Notes About Some Types, Up: Types Conversion   [Contents][Index]


11.3 Defining New Type Converters

To define a new non-array type means associating with the type name a procedure for converting from a PostgreSQL string representation to a Scheme object (the objectifier), a procedure to do the conversion in the opposite direction (the stringifier), and a default string to use if none is specified (for example, during an INSERT operation). For this, use the define-db-col-type procedure.

Procedure: define-db-col-type name default stringifier objectifier

Register type name with default, stringifier and objectifier procs. name is a symbol. default is a string to use if the Scheme object is #f. stringifier is a proc that takes a Scheme object and returns a string suitable for use in an INSERT VALUES SQL command. objectifier is a proc that takes a string and returns the Scheme object parsed out of it.

Both stringifier and objectifier need not worry about SQL-style quoting (using single quotes) and related quote escaping.

If name already exists, it is redefined. See also dbcoltype-lookup.

To express conversion of arrays (no matter the dimensionality) of non-array types, use the define-db-col-type-array-variant procedure.

Procedure: define-db-col-type-array-variant composed simple [procs…]

Register type composed, an array variant of simple, with optional procs. simple should be a type name already registered using define-db-col-type. composed is conventionally formed by appending simple with one or more pairs of ‘[]’ (square braces), with the number of pairs indicating the array dimensionality. For example, if simple is text, a two-dimensional text array would be named text[][].

Optional arg procs is a list specifying alternative stringifier and objectifier procedures (in that order). If unspecified, simple is looked up and its stringifier and objectifier are used. See dbcoltype-lookup.

The default value of all array types is ‘{}’ and cannot be changed.


Previous: Selected Notes About Some Types, Up: Types Conversion   [Contents][Index]