Adding a New User-Defined Function
2814
whether the
xxx()
function should return
NULL
. You should not store a string into
*error
!
error
points to a single-byte variable, not to a string buffer.
*is_null
is reset for each group (before calling
xxx_clear()
).
*error
is never reset.
If
*is_null
or
*error
are set when
xxx()
returns, MySQL returns
NULL
as the result for the group
function.
21.2.2.3. UDF Argument Processing
The
args
parameter points to a
UDF_ARGS
structure that has the members listed here:
•
unsigned int arg_count
The number of arguments. Check this value in the initialization function if you require your function to
be called with a particular number of arguments. For example:
if (args->arg_count != 2)
{
strcpy(message,"XXX() requires two arguments");
return 1;
}
For other
UDF_ARGS
member values that are arrays, array references are zero-based. That is, refer
to array members using index values from 0 to
args->arg_count
– 1.
•
enum Item_result *arg_type
A pointer to an array containing the types for each argument. The possible type values are
STRING_RESULT
,
INT_RESULT
,
REAL_RESULT
, and
DECIMAL_RESULT
.
To make sure that arguments are of a given type and return an error if they are not, check the
arg_type
array in the initialization function. For example:
if (args->arg_type[0] != STRING_RESULT ||
args->arg_type[1] != INT_RESULT)
{
strcpy(message,"XXX() requires a string and an integer");
return 1;
}
Arguments of type
DECIMAL_RESULT
are passed as strings, so you should handle them the same
way as
STRING_RESULT
values.
As an alternative to requiring your function's arguments to be of particular types, you can use the
initialization function to set the
arg_type
elements to the types you want. This causes MySQL to
coerce arguments to those types for each call to
xxx()
. For example, to specify that the first two
arguments should be coerced to string and integer, respectively, do this in
xxx_init()
:
args->arg_type[0] = STRING_RESULT;
args->arg_type[1] = INT_RESULT;
Exact-value decimal arguments such as
1.3
or
DECIMAL
column values are passed with a type of
DECIMAL_RESULT
. However, the values are passed as strings. If you want to receive a number, use
the initialization function to specify that the argument should be coerced to a
REAL_RESULT
value:
args->arg_type[2] = REAL_RESULT;
Note
Prior to MySQL 5.0.3, decimal arguments were passed as
REAL_RESULT
values. If you upgrade to a newer version and find that your UDF now
receives string values, use the initialization function to coerce the arguments
to numbers as just described.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...