An integer echoing UDF
The next example is designed to show how to deal with inputs. It is similar to the previous one, but will demonstrate how to perform argument checking. As you remember, this check should happen in the initialization function, so that we can give a proper error message when there is a problem. We will call this UDF, udf_intexample
, and will make it accept only one argument, which has to be an integer for the function to succeed:
#include <string.h> #include <mysql.h> my_bool udf_intexample_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
As before, we include everything needed for this example and create an initialization function with the same prefix as the function name.
if (args->arg_count != 1) { strcpy(message, "udf_intexample() can only accept one argument"); return 1; }
We want to ensure that only one argument is accepted for this UDF so we check that arg_count
is 1. If it is not, we stick an error message into the message
buffer and return...