gpt4 book ai didi

mysql - propercase mysql udf

转载 作者:可可西里 更新时间:2023-11-01 07:08:32 25 4
gpt4 key购买 nike

我正在尝试为 mysql 编写一个自定义的 propercase 用户定义函数,所以我从 http://www.mysqludf.org/index.php 中获取了 str_ucwords 函数作为构建我自己的示例。

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;
}

如果我尝试使用 select str_ucwords("test string"); 则效果很好,但是如果我尝试从数据库中选择一个字段,例如 select str_ucwords(name) from name; 我没有得到任何返回。

如何更改此函数,以便它允许我从数据库中的字段中提取数据?

我已经尝试从 init 函数中删除 args->arg_type[0] != STRING_RESULT

最佳答案

问题不在于参数的类型,而是在调用 str_ucwords_init 时它是 NULL(因为在检索之前调用了 str_ucwords_init任何行)。要使 str_ucwords 与字段一起使用,您必须通过在 _init 函数中将 initid->maybe_null 设置为 1 来支持 NULL 值,并设置当实际参数为 null 时,*null_value 为 1(并且 result 为 NULL,尽管这可能不是必需的)在 str_ucwords 中。

my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
unsigned long res_length;

/* make sure user has provided exactly one argument */
if (args->arg_count != 1) {
snprintf(message, MYSQL_ERRMSG_SIZE, "wrong number of arguments: str_ucwords requires one string argument, got %d arguments", args->arg_count);
return 1;
}
/* make sure user has provided a string argument */
if (args->arg_type[0] != STRING_RESULT) {
snprintf(message, MYSQL_ERRMSG_SIZE, "str_ucwords requires one string argument (expected type %d, got type %d)", STRING_RESULT, args->arg_type[0]);
return 1;
}

res_length = args->lengths[0];

if (SIZE_MAX < res_length) {
snprintf(message, MYSQL_ERRMSG_SIZE, "res_length (%lu) cannot be greater than SIZE_MAX (%zu)", res_length, (size_t) (SIZE_MAX));
return 1;
}

initid->ptr = NULL;

if (res_length > 255) {
char *tmp = (char *) malloc((size_t) res_length); /* This is a safe cast because res_length <= SIZE_MAX. */
if (tmp == NULL) {
snprintf(message, MYSQL_ERRMSG_SIZE, "malloc() failed to allocate %zu bytes of memory", (size_t) res_length);
return 1;
}
initid->ptr = tmp;
}

initid->maybe_null = 1;
initid->max_length = res_length;
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 = 1;

if (args->args[0] == NULL) {
result = NULL;
*res_length = 0;
*null_value = 1;
} else {
if (initid->ptr != NULL) {
result = initid->ptr;
}

// copy the argument string into result
memcpy(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 = 0;
result[i] = my_toupper(&my_charset_latin1, result[i]);
} else {
result[i] = my_tolower(&my_charset_latin1, result[i]);
}
} else {
new_word = 1;
}
}
}
return result;
}

lib_mysqludf_str 的更高版本应该支持函数中的 NULL 值而无需更改,这意味着它们也应该与表列一起使用。

关于mysql - propercase mysql udf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com