gopgext/subscript
import "gopgext/subscript"
Package subscript implements subscripting of data types in Go: value[key] to read an element and SET col[key] = x (or x[key] := … in PL/pgSQL) to replace one, like hstore’s h[‘key’] or jsonb’s j[‘a’].
The extension registers a Type under the name of its SQL type (in main or a package initializer):
subscript.Register("kv", subscript.Type{
Subscript: "text", // subscripts are coerced to text
Result: "text", // kv[...] is text
Fetch: kvGet, // the element, or ok=false for NULL
Assign: kvSet, // the container with the element replaced
})
and its sql/ files give the type this package’s handler:
CREATE FUNCTION kv_subscript_handler(internal) RETURNS internal
AS 'MODULE_PATHNAME', 'gopgext_subscript_handler' LANGUAGE C STRICT IMMUTABLE;
ALTER TYPE kv SET (SUBSCRIPT = kv_subscript_handler);
Containers, subscripts and elements are variable-length values (types stored like bytea or text): the functions get their bytes, valid during the call, and return new ones. Slices (a[1:2]) aren’t supported.
Index#
Functions#
func Register#
func Register(typeName string, t Type)
Register registers the subscripting of the SQL type named typeName.
Types#
type Type#
type Type struct {
// Subscript is the SQL type subscripts are coerced to (assignment
// casts), e.g. "text".
Subscript string
// Result is the SQL type of an element, e.g. "text".
Result string
// Subscripts is the number of subscripts allowed (0: one).
Subscripts int
// Fetch returns the element at subscripts (one per subscript of the
// expression, never NULL: a NULL container or subscript makes the
// element NULL without calling Fetch); ok=false makes it NULL.
Fetch func(container []byte, subscripts [][]byte) (element []byte, ok bool, err error)
// Assign returns the container with the element at subscripts set to
// element. A nil container, subscript or element is NULL.
Assign func(container []byte, subscripts [][]byte, element []byte) ([]byte, error)
}
Type is the subscripting of a data type. Fetch and Assign are required.