The main function should be declared as shown below. Note that the return
type and parameters differ, depending on whether you will declare the SQL
function XXX()
to return STRING
, INTEGER
or REAL
in the CREATE FUNCTION
statement:
For STRING
functions:
char *xxx(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error);
For INTEGER
functions:
long long xxx(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error);
For REAL
functions:
double xxx(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error);
The initialization and deinitialization functions are declared like this:
my_bool xxx_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void xxx_deinit(UDF_INIT *initid);
The initid
parameter is passed to all three functions. It points to a
UDF_INIT
structure that is used to communicate information between
functions. The UDF_INIT
structure members are listed below. The
initialization function should fill in any members that it wishes to change.
(To use the default for a member, leave it unchanged.)
my_bool maybe_null
-
xxx_init()
should set maybe_null
to 1
if xxx()
can return NULL
. The default value is 1
if any of the
arguments are declared maybe_null
.
unsigned int decimals
-
Number of decimals. The default value is the maximum number of decimals in
the arguments passed to the main function. (For example, if the function is
passed
1.34
, 1.345
and 1.3
, the default would be 3,
since 1.345
has 3 decimals.
unsigned int max_length
-
The maximum length of the string result. The default value differs depending
on the result type of the function. For string functions, the default is the
length of the longest argument. For integer functions, the default is 21
digits. For real functions, the default is 13 plus the number of decimals
indicated by
initid->decimals
. (For numeric functions, the length
includes any sign or decimal point characters.)
char *ptr
-
A pointer that the function can use for its own purposes. For example,
functions can use
initid->ptr
to communicate allocated memory
between functions. In xxx_init()
, allocate the memory and assign it
to this pointer:
initid->ptr = allocated_memory;
In xxx()
and xxx_deinit()
, refer to initid->ptr
to use
or deallocate the memory.