db2 编译C 自定义函数

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sqludf.h>


  7. //Macro for iendity the blanks
  8. #define isSpaces(ch) (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\b' || ch == '\f' || ch == '\n')

  9. SQL_API_RC SQL_API_FN TrimChr ( SQLUDF_VARCHAR *in,
  10.                                 SQLUDF_VARCHAR *out,
  11.                                 SQLUDF_NULLIND *inNullInd,
  12.                                 SQLUDF_NULLIND *outNullInd,
  13.                                 SQLUDF_TRAIL_ARGS )
  14. {
  15.   if ( -1 != *inNullInd )
  16.   {
  17.     //pointer of the first char of string
  18.     char *pszHead = in;
  19.     //pointer of the last non-blank char
  20.     char *pszLast = &in[strlen(in)-1];
  21.     //search the location of the last non-blank char
  22.     while (isSpaces(*pszLast))
  23.             --pszLast;
  24.     *(++pszLast) = '\0';
  25.     //search the first non-blank char for left shift the string
  26.     for ( ; (*pszHead != '\0') && (pszHead != pszLast); ++pszHead)
  27.     {
  28.       if (! isSpaces(*pszHead))
  29.       {
  30.         strcpy(out, pszHead);
  31.         break;
  32.       }
  33.     }
  34.   }

  35.   *outNullInd = *inNullInd;

  36.   return (0);
  37. }
复制代码
编译:
cd $HOME/sqllib/samples/c
bldrtn TrimChr <dbname>
cp TrimChr  $HOME/sqllib/function/
重启数据库

简单测试结果:
=> db2 "select * from lt"

C1                       C2
------------------------ --------------------
  +2.00000000000000E+000 eeee
  +4.00000000000000E+000  asdf
  +3.00000000000000E+000 -

  3 record(s) selected.

=> db2 "select c1, trimchr(c2) from lt";

C1                       2
------------------------ ----------
  +2.00000000000000E+000 eeee
  +4.00000000000000E+000  asdf
  +3.00000000000000E+000 -

  3 record(s) selected.


JAVA自定义函数

drop function ids.regexJ;
create function ids.regexJ(regex VARCHAR(2048),src VARCHAR(2048))
returns  integer
fenced
variant
no sql
language java
parameter style java
external name 'RegTest!regexUdf'; 

编写java类程序然后编译成可执行文件UserFunction.class*,chmod 文件权限后

在执行第二步,JAVA类文件放在/home/db2inst1/sqllib/function/ DB2的安装目录


以上2种方式都试过,第一个Pcre编译完成,执行过程有问题,第二个DB2 JAVA版本1.3.1 也是报错。

郁闷


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值