Tôi đang cố gắng viết một hàm tùy chỉnh người dùng được xác định thích hợp cho mysql vì vậy tôi đã lấy hàm str_ucwords từ http://www.mysqludf.org/index.php làm ví dụ để xây dựng của riêng tôi.mysql mysql udf
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;
}
này hoạt động tốt nếu tôi cố gắng select str_ucwords("test string");
nhưng nếu tôi cố gắng chọn một lĩnh vực từ cơ sở dữ liệu như select str_ucwords(name) from name;
tôi nhận được không có gì trả lại.
Làm cách nào để thay đổi chức năng này để cho phép tôi lấy dữ liệu từ các trường trên cơ sở dữ liệu?
Tôi đã thử xóa args->arg_type[0] != STRING_RESULT
khỏi chức năng init.