mysql udf 的实现过程

首先编写一个实现curl提交的udf,文件名起名为lib_mysqludf_clearcache.c
mysql udf 编写的相关规范可以查看mysql官方手册
[url]http://dev.mysql.com/doc/refman/5.1/zh/extending-mysql.html#adding-functions[/url]



#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif

#ifdef STANDARD
#include <string.h>
#include <stdlib.h>
#include <time.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

#include <ctype.h>


#define DEST_URL "http://www.web.com/"

my_bool clear_cache_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return 0;
}

void clear_cache_deinit(UDF_INIT *initid)
{

}

char *clear_cache(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error)
{
CURL *curl;
CURLcode res;

// 初始化curl
curl = curl_easy_init();

if(curl != NULL)
{
// 设置目标url
curl_easy_setopt(curl,CURLOPT_URL,DEST_URL);

// 输出header头部信息
curl_easy_setopt(curl,CURLOPT_HEADER,0);

// 执行操作
res = curl_easy_perform(curl);

// 执行回收资源
curl_easy_cleanup(curl);
}

return 0;
}



然后编译生成so文件
gcc -I /usr/include/mysql -shared lib_mysqludf_clearcache.c -o lib_mysqludf_clearcache.so -lcurl

第三步把lib_mysqludf_clearcache.so复制到mysql的插件目录
cp lib_mysqludf_clearcache.so /usr/lib/mysql/plugin/

第四步进入mysql
执行
CREATE FUNCTION clear_cache RETURNS INTEGER SONAME 'lib_mysqludf_clearcache.so';

然后执行 select clear_cache()查看自定义函数是否执行成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值