.\" XXX standard disclaimer belongs here....
.\" $Header: RCS/definetype,v 1.8 91/12/02 04:02:26 kemnitz Exp $
.SP "DEFINE TYPE " COMMANDS 6/14/90
.XA 2 "Define Type"
.uh NAME
.lp
define type \*- define a new base data type 
.uh SYNOPSIS
.lp
.nf
\fBdefine type\fP typename (internallength = (number | variable),
	[ externallength = (number | variable), ]
	input = function, output = function
	[ , element = typename]
	[ , delimiter = <character>]
	[ , default = \*(lqstring\*(rq ]
	[ , send = procedure ] [ , receive = procedure ]
	[ , passedbyvalue])

.\" \fBdefine type\fP typename as postquel_commands
.uh DESCRIPTION
.lp
.b "Define type"
allows the user to register a new user data type
with \*(PP for use in the current data base.
The user who defines a type becomes its owner.
.i Typename
is the name of the new type
and must be unique within the types defined for this database. 
.lp
.i "Define type"
requires the registration of two functions (using
.b "define function <C>" )
before defining the type.
The representation of a new base type is determined by the function
.i input ,
which converts the type's external representation to an internal
representation usable by the operators and functions defined for the
type.
Naturally,
.i "output"
performs the reverse transformation.
.lp
New base data types can be fixed length, 
in which case
.i "internal length"
is a positive integer,
or variable length,
in which case \*(PP assumes that the new type has the same format
as the \*(PP-supplied data type, 
.b "text".
To indicate that a type is variable length, set
.i "internal length"
to
.b "-1"
.
Moreover, the external representation is similarly specified using
.i "external length."
.lp
To indicate that a type is an array and to indicate that a type has
array elements, indicate the type of the array element using 
the 
.i "element"
attribute.  For example, to define an array of 4 byte integers (int4), set the
.i "element"
attribute equal to
.b "int4".
.lp
To indicate the delimiter to be used on arrays of this type, the 
.i "delimiter"
attribute can be set to a specific character.  The default delimiter is
the comma (``,'') character.
.lp
A
.i "default"
value is optionally available in case a user wants some specific bit
pattern to mean
.q "data not present."
.lp
The optional functions
.i "send"
and
.i "receive"
are used when the application program requesting \*(PP services
resides on a different machine.
In this case,
the machine on which \*(PP runs may use a different format for the
data type than used on the remote machine.
In this case it is appropriate to convert data items to a standard
form on output
.i "send"
and convert from the standard format to the machine specific format on
input
.i "receive."
If these functions are not specified,
then it is assumed that the internal format of the type is acceptable
on all relevant machine architectures
(for example,
single characters do not have to be converted if passed from a Sun 3 to a
DECstation).
.lp
The optional
.i "passedbyvalue"
flag indicates that operators and functions which use this data type
should be passed an argument by value rather than by reference.
Note that only types whose internal representation is smaller than 
\fBsizeof\fR(\fIchar *\fR),
which is typically four bytes, may be passed by value.
.lp
For new base types, a user can define operators, functions and aggregates
using the appropriate facilities described in this section.
.uh "ARRAY TYPES"
.lp
Two generalized builtin functions,
.b array_in
and
.b array_out,
exist for quick creation of variable length array types.  These functions
operate on any existing Postgres type.
.lp
.uh "LARGE OBJECT TYPES"
.lp
A "regular" Postgres type can only be 8K bytes in length.  If you need
a larger type, then you will want to create a Large Object type.  The
interface for these types is discussed at length in Section 7, the Large
Object Backend Interface.  The length of all large object types is always
.b variable,
meaning the internallength for large objects is always -1.
.lp
.uh EXAMPLES
.lp
.nf
/*
 * This command creates the box data type and then uses the type in a
 * class definition
 */

   define type box (internallength = 8,
 	input = my_procedure_1, output = my_procedure_2)

   create MYBOXES (id = int4, description = box)

/*
 * This command creates a variable length array type with integer
 * elements.
 */
 
   define type int4array
   (input = array_in, output = array_out, internallength = -1, element = int4)

   create MYARRAYS (id = int4, numbers = int4array)

/*
 * This command creates a large object type and uses it in a class
 * definition.
 */

   define type bigobj
   (input = lo_filein, output = lo_fileout, internallength = -1)

   create BIG_OBJS (id = int4, obj = bigobj)

.uh "SEE ALSO"
.lp
define function <C> (commands),
define operator(commands),
remove type(commands),
Large Object Backend Interface.
