gbase8s自定义C 函数

本文介绍了如何在C语言中编写函数,模仿MySQL的find_in_set函数,处理字符串按照-分割并查找首次出现位置,同时考虑NULL值的处理和并行性。示例代码展示了创建函数、编译和测试的过程,以及参考了GBaseDBTDIR官方手册。
摘要由CSDN通过智能技术生成

例如

实现类似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官方手册

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值