The args parameter points to a
UDF_ARGS structure that has the members
listed here:
unsigned int arg_countThe 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_ARGSmember values that are arrays, array references are zero-based. That is, refer to array members using index values from 0 toargs->arg_count– 1.enum Item_result *arg_typeA pointer to an array containing the types for each argument. The possible type values are
STRING_RESULT,INT_RESULT,REAL_RESULT, andDECIMAL_RESULT.To make sure that arguments are of a given type and return an error if they are not, check the
arg_typearray 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_RESULTare passed as strings, so you should handle them the same way asSTRING_RESULTvalues.As an alternative to requiring your function's arguments to be of particular types, you can use the initialization function to set the
arg_typeelements to the types you want. This causes MySQL to coerce arguments to those types for each call toxxx(). For example, to specify that the first two arguments should be coerced to string and integer, respectively, do this inxxx_init():args->arg_type[0] = STRING_RESULT; args->arg_type[1] = INT_RESULT;
Exact-value decimal arguments such as
1.3orDECIMALcolumn values are passed with a type ofDECIMAL_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 aREAL_RESULTvalue:args->arg_type[2] = REAL_RESULT;
char **argsargs->argscommunicates information to the initialization function about the general nature of the arguments passed to your function. For a constant argumenti,args->args[i]points to the argument value. (See later for instructions on how to access the value properly.) For a nonconstant argument,args->args[i]is0. A constant argument is an expression that uses only constants, such as3or4*7-2orSIN(3.14). A nonconstant argument is an expression that refers to values that may change from row to row, such as column names or functions that are called with nonconstant arguments.For each invocation of the main function,
args->argscontains the actual arguments that are passed for the row currently being processed.If argument
irepresentsNULL,args->args[i]is a null pointer (0). If the argument is notNULL, functions can refer to it as follows:An argument of type
STRING_RESULTis given as a string pointer plus a length, to enable handling of binary data or data of arbitrary length. The string contents are available asargs->args[i]and the string length isargs->lengths[i]. Do not assume that the string is null-terminated.For an argument of type
INT_RESULT, you must castargs->args[i]to along longvalue:long long int_val; int_val = *((long long*) args->args[i]);
For an argument of type
REAL_RESULT, you must castargs->args[i]to adoublevalue:double real_val; real_val = *((double*) args->args[i]);
For an argument of type
DECIMAL_RESULT, the value is passed as a string and should be handled like aSTRING_RESULTvalue.ROW_RESULTarguments are not implemented.
unsigned long *lengthsFor the initialization function, the
lengthsarray indicates the maximum string length for each argument. You should not change these. For each invocation of the main function,lengthscontains the actual lengths of any string arguments that are passed for the row currently being processed. For arguments of typesINT_RESULTorREAL_RESULT,lengthsstill contains the maximum length of the argument (as for the initialization function).char *maybe_nullFor the initialization function, the
maybe_nullarray indicates for each argument whether the argument value might be null (0 if no, 1 if yes).char **attributesargs->attributescommunicates information about the names of the UDF arguments. For argumenti, the attribute name is available as a string inargs->attributes[i]and the attribute length isargs->attribute_lengths[i]. Do not assume that the string is null-terminated.By default, the name of a UDF argument is the text of the expression used to specify the argument. For UDFs, an argument may also have an optional
[AS]clause, in which case the argument name isalias_namealias_name. Theattributesvalue for each argument thus depends on whether an alias was given.Suppose that a UDF
my_udf()is invoked as follows:SELECT my_udf(expr1, expr2 AS alias1, expr3 alias2);
In this case, the
attributesandattribute_lengthsarrays will have these values:args->attributes[0] = "expr1" args->attribute_lengths[0] = 5 args->attributes[1] = "alias1" args->attribute_lengths[1] = 6 args->attributes[2] = "alias2" args->attribute_lengths[2] = 6
unsigned long *attribute_lengthsThe
attribute_lengthsarray indicates the length of each argument name.