Handling fixed-length opaque-type arguments
For UDR arguments that are fixed-length opaque types, the routine manager passes a pointer to the internal format of the opaque type to the C UDR. If the fixed-length opaque type is defined as passed by value, however, the routine manager passes the actual internal format.
For more information, see Determine the passing mechanism for an opaque type.
For example, suppose you want to define a user-defined function
named circle_area() that accepts a fixed-length opaque type
named circle (which is defined in Figure 1) and
computes its area. The following CREATE FUNCTION statement registers
the circle_area() function in the database:
CREATE FUNCTION circle_area(arg1 circle)
RETURNS FLOAT
EXTERNAL NAME '/usr/udrs/circle/circle.so'
LANGUAGE C;
Because circle is a fixed-length data type that cannot
fit into an MI_DATUM structure, the following declaration of circle_area() specifies
a pointer to the internal format of circle:
/* Valid C UDR declaration for fixed-length opaque-type
* parameter
*/
mi_double_precision *circle_area(circle_ptr)
circle_t *circle_ptr;
The following code fragment shows the implementation of the circle_area() function.
#include <mi.h>
#include <ctype.h>
#include <circle.h>
mi_double_precision *circle_area(circle)
circle_t *circle;
{
mi_double_precision *area;
/* Allocate memory for mi_double_precision return
* value
*/
area = mi_alloc(sizeof(mi_double_precision));
/* Calculate circle area using radius from circle_t
* structure and constant PI_CONSTANT (defined in
* circle.h).
*/
*area = (circle->radius * circle->radius) * PI_CONSTANT;
return ( area )
}
Tip: A C UDR that returns fixed-length opaque data must
return a pointer to the internal format (unless the internal format
can fit into an MI_DATUM structure and is declared to be passed
by value). For more information, see Return opaque-type values.