Registering a C UDR as a cast function

About this task

To register a C UDR as a cast function:

Procedure

  1. Use the CREATE FUNCTION statement to register the C UDR as a cast function.

    For more information, see Register a C UDR.

  2. Use the CREATE CAST statement to register the cast in the database.

    Casts are stored in the syscasts system catalog table. For more information about the syntax of the CREATE CAST statement, see the Informix® Guide to SQL: Syntax

Example

The following lines register the C function, a_to_b(), as an implicit cast from the a to b data type:
CREATE FUNCTION a_to_b(source a)
RETURNS b
EXTERNAL NAME '/usr/udrs/casts.so(a_to_b)'
LANGUAGE C;

CREATE CAST (a AS b WITH a_to_b);
These SQL statements assume that a and b are already registered as user-defined types. These statements only provide the ability to convert from type a to type b. To provide the ability to cast from the b to the a data type, you must create a second cast, as the following sample lines show:
CREATE FUNCTION b_to_a(source b)
RETURNS a
EXTERNAL NAME '/usr/udrs/casts.so(b_to_a)'
LANGUAGE C;

CREATE CAST (b AS a WITH b_to_a);
The following lines declare the C function, a_to_b(), which accepts the a fixed-length opaque type as an argument and returns the b fixed-length opaque type:
b_t *a_to_b(source_type)
   a_t *source_type;
{
   b_t *target;

   target = (b_t *)mi_alloc(sizeof(b_t));

   /* Perform necessary conversions from a to b */

   return ( target );
}