This section describes the different functions that you need to define when you create a simple UDF. Section 24.4.2, “Adding a New User-Defined Function”, describes the order in which MySQL calls these functions.
xxx() function should be declared
as shown in this section. Note that the return type and
parameters differ, depending on whether you declare the SQL
XXX() to return
REAL in the
CREATE FUNCTION statement:
char *xxx(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
long long xxx(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
double xxx(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
DECIMAL functions return string
values and should be declared the same way as
functions are not implemented.
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);
initid parameter is passed to all three
functions. It points to a
structure that is used to communicate information between
UDF_INIT structure members
follow. The initialization function should fill in any members
that it wishes to change. (To use the default for a member,
leave it unchanged.)
NULL. The default value is
1if any of the arguments are declared
unsigned int decimals
The number of decimal digits to the right of the decimal point. The default value is the maximum number of decimal digits in the arguments passed to the main function. For example, if the function is passed
1.3, the default would be 3, because
1.345has 3 decimal digits.
For arguments that have no fixed number of decimals, the
decimalsvalue is set to 31, which is 1 more than the maximum number of decimals permitted for the
DOUBLEdata types. This value is available as the constant
decimalsvalue of 31 is used for arguments in cases such as a
DOUBLEcolumn declared without an explicit number of decimals (for example,
FLOAT(10,3)) and for floating-point constants such as
1345E-3. It is also used for string and other nonnumber arguments that might be converted within the function to numeric form.
The value to which the
decimalsmember is initialized is only a default. It can be changed within the function to reflect the actual calculation performed. The default is determined such that the largest number of decimals of the arguments is used. If the number of decimals is
NOT_FIXED_DECfor even one of the arguments, that is the value used for
unsigned int max_length
The maximum length of the result. The default
max_lengthvalue 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 decimal digits indicated by
initid->decimals. (For numeric functions, the length includes any sign or decimal point characters.)
If you want to return a blob value, you can set
max_lengthto 65KB or 16MB. This memory is not allocated, but the value is used to decide which data type to use if there is a need to temporarily store the data.
A pointer that the function can use for its own purposes. For example, functions can use
initid->ptrto communicate allocated memory among themselves.
xxx_init()should allocate the memory and assign it to this pointer:
initid->ptr = allocated_memory;
xxx_deinit(), refer to
initid->ptrto use or deallocate the memory.
xxx()always returns the same value and to