例如
实现类似mysql 中 find_in_set 函数,按照 “-”分割字符串返回第一次出现的位置
vi myfun.c
#include <stdio.h>
#include <string.h>
#include "mi.h"
mi_integer my_fun(mi_lvarchar * str1,mi_lvarchar *str2,MI_FPARAM* fParam){
if(mi_fp_argisnull(fParam, 0) == MI_TRUE || mi_fp_argisnull(fParam, 1) == MI_TRUE){
return -1;
}
mi_integer index = 1;
mi_lvarchar *lvarch1 = str1;
mi_lvarchar *lvarch2 = str2;
mi_integer len = mi_get_varlen(lvarch1);
char * gstr1 =mi_get_vardata(lvarch1);
char * current =mi_get_vardata(lvarch2);
char * need = strchr(current,'-');
while((!(len == (need-current) && strncmp(gstr1,current,len)==0) && need != NULL)){
current = need +1;
need = strchr(current,'-');
index++;
if(need == NULL){
if(!(len == strlen(current) && strncmp(gstr1,current,len)==0)){
index=0;
}
}
}
return index;
}
编译
gcc -fPIC -DMI_SERVBUILD -I$GBASEDBTDIR/incl/public -I$GBASEDBTDIR/incl -L$GBASEDBTDIR/esql/lib -c myfun.c
ld -G myfun.o -o myfun.so
创建函数
create function my_fun(varchar(255),varchar(255))
returns int
with (HANDLESNULLS,PARALLELIZABLE)
external name '$GBASEDBTDIR/lib/myfun.so(my_fun)'
language c;
在c代码中处理NULL时需要 MI_FPARAM* 结构体指针
HANDLESNULLS 处理NULL值
PARALLELIZABLE 并行处理
测试
select my_fun('adc','aa-ac-adc-adc-af') from dual;
select my_fun(null,'aa-ac-adc-adc-af') from dual;
更多资料参考gbase8s官方手册