tags:

views:

37

answers:

0

I am trying to write a custom propercase user defined function for mysql so i took the str_ucwords function from http://www.mysqludf.org/index.php as an example to build my own.

my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    /* make sure user has provided exactly one string argument */
    if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT || args->args[0] == NULL)
    {
        strcpy(message,"str_ucwords requires one string argument");
        return 1;
    }

    /* str_ucwords() will not be returning null */
    initid->maybe_null=0;

    return 0;
}

char *str_ucwords(UDF_INIT *initid, UDF_ARGS *args,
    char *result, unsigned long *res_length, 
    char *null_value, char *error)
{
    int i;
    int new_word = 0;

    // copy the argument string into result
    strncpy(result, args->args[0], args->lengths[0]);

    *res_length = args->lengths[0];

    // capitalize the first character of each word in the string
    for (i = 0; i < *res_length; i++)
    {
        if (my_isalpha(&my_charset_latin1, result[i]))
        {
            if (!new_word)
            {
                new_word = 1;
                result[i] = my_toupper(&my_charset_latin1, result[i]);
            }
        }
        else
        {
            new_word = 0;
        }
    }

    return result;
}

This works fine if I try select str_ucwords("test string"); but if I try to select a fields from a database like select str_ucwords(name) from name; I get nothing returned.

How do I change this function so it allows me to pull data from fields on the database?

I have already tried removing args->arg_type[0] != STRING_RESULT from the init function.