0

This is my first time trying to create an udf for mysql. The docs state that my_func_init gets called prior to executing the main function, yet in my environment this does not seem to happen.

long long charmatch(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
    return 42;
}

my_bool charmatch_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if (args->arg_count != 2)
    {
        strncpy(message, "charmatch() requires two arguments", 34);
        return EXIT_FAILURE;
        // was return EXIT_SUCCESS;
    }

    if(args->arg_type[0] != STRING_RESULT)
    {
        strncpy(message, "argument 1 must be a string", 27);
        return EXIT_FAILURE;
        // was return EXIT_SUCCESS;
    }

    if(args->arg_type[0] != STRING_RESULT)
    {
        strncpy(message, "argument 2 must be a string", 27);
        return EXIT_FAILURE;
        // was return EXIT_SUCCESS;
    }

    return EXIT_SUCCESS;
}

As you can see, I'm checking whether there are two arguments. In theory SELECT CHARMATCH() should return a message saying I must set two arguments, but it's not: it return 42.

Why doesn't it return the message?

EDIT Seems like the problem was in my return values for errors being 0 instead of 1. I fixed it and now calling SELECT CHARMATCH() with the wrong number of arguments returns an empty set. Why are the errors not being displayed?

1 Answer 1

1

You always return EXIT_SUCCESS in charmatch_init(), but

"The initialization function should return 0 if no error occurred and 1 otherwise. If an error occurs, xxx_init() should store a null-terminated error message in the message parameter. "

http://dev.mysql.com/doc/refman/5.0/en/udf-return-values.html

I guess that's why your error messages are ignored.

5
  • Thanks, that was the problem. I changed my return values accordingly, but the error message is not displayed.
    – kgilden
    Sep 10, 2011 at 0:51
  • My second guess would be that it has something to do with strncpy(), with this function, destination will only be null-terminated if the length of the C string in source is less than number passed. Can you try to set length for strncpy() as MYSQL_ERRMSG_SIZE?
    – istepura
    Sep 10, 2011 at 1:18
  • do you also know why mysql displays the error for my udf in cli, but not in a php script (phpMyAdmin for example)?
    – kgilden
    Sep 10, 2011 at 1:35
  • Sorry, I was a bit too vague. It returns a perfectly valid response, albeit an empty one. There are no errors returned. No results are also not returned when I call SELECT CHARMATCH(), foo.* FROM foo;. It's getting late so I can't test this in any of my application, but I'm assuming they would break silently as well.
    – kgilden
    Sep 10, 2011 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.