MySQL的总UDF(用户定义函数)用C [英] mysql aggregate UDF (user defined function) in C

查看:201
本文介绍了MySQL的总UDF(用户定义函数)用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写用于MySQL 5.x的一个聚合扩展功能(C实现)我已经走遍了文档(包括浏览SQL / udf_example.c),但我没有发现任何东西,是短暂的,给点意见,并表明我正是我需要做的。

I need to write an aggregate extension function (implemented in C) for mySQL 5.x. I have scoured the documentation (including browsing sql/udf_example.c) but I do not find anything that is brief, to the point and shows me just what I need to do.

这就是问题所在:


  1. 我有一个C结构(美孚)

  2. 我有一个C函数,它接受这些美孚结构的数组,执行一个操作,阵列上,并返回一个double。

  1. I have a C struct (Foo)
  2. I have a C function that takes an array of these Foo structs, performs an operation, on the array, and returns a double.

结构FooBar的{
  字符*日期;
  双周岁;
  双怀特;
  双倍工资;
  INT eye_color;
};

struct FooBar { char * date; double age; double wight; double salary; int eye_color; };

/ *处理功能的 /
双processFooBars(结构FooBar的
的foobars,常量为size_t大小);​​

/* Processing function / double processFooBars(struct FooBar foobars, const size_t size);

/ * MySQL表* /
CREATE TABLE foo_bar这样的名称(the_date DATE,双岁,双重量,双薪,INT eye_color};

/* MySQL table */ CREATE TABLE foo_bar( the_date DATE, double age, double weight, double salary, int eye_color};

我希望能够创造这样一个聚合函数:(我也许使用PostgreSQL语法)

I want to be able to create an aggregate function thus: (I maybe using PostgreSQL syntax)

CREATE凝聚函数proc_foobar RETURNS REAL SONAME myshlib.so ALIAS my_wrapper_func

CREATE AGGREGATE FUNCTION proc_foobar RETURNS REAL soname myshlib.so ALIAS my_wrapper_func

然后我就可以在MySQL查询使用这样的:

I can then use it in a MySQL Query thus:

选择proc_foobar()作为喜爱程度从foo_bar这样WHERE the_date'1-JAN-09和1日 - 12月09'之间。

SELECT proc_foobar() as likeability FROM foo_bar WHERE the_date BETWEEN '1-Jan-09' and '1-Dec-09'

这是什么查询应该那么做将是从表foo_bar这样获取所有符合条件的记录,将它们传递到周围processFooBar我的包装功能,然后将提取FooBar的结构从收到的记录,然后将它们传递给C函数该做的工作并返回值。

What this query should then do would be to fetch all the the matching records from the table foo_bar, pass them to my wrapper function around processFooBar, which will then extract FooBar structs from the records received and then pass them to the C function that does the work and returns the value.

它的简单使用(伪)code解释:

Its simpler to explain using (pseudo)code:

#ifdefined __cplusplus
extern "C" {
#endif

  /* this is the wrapper function that mySQL calls and passes the records to */
  double my_wrapper_func(/*Matching rows sent by mySQL + other info .. ?*/)
  {
     /* create FooBar Array from received record */
     struct FooBar ** the_array = ExtractArrayFromRowset(/*some params*/);
     double result = processFooBar(the_array, ARRAY_SIZE_MACRO(*the_array));

     /* free resources */
     FreeFooBarArray(the_array);
     RETURN_DOUBLE(result);  /* or similar macro to return a double in MySQL */
  }

#ifdefined __cplusplus
};
#endif

任何人都可以提供一个小片段(或直接我一个片断),显示我怎么可以写my_wrapper_func - 更重要的一点还是如上所述,作为我如何实现写集合函数所需的功能在C / C ++的扩展功能。

Could anyone provide a little snippet (or direct me to a snippet) that shows me how I can write the my_wrapper_func - or more to the point how I can implement the required functionality of writing an aggregate function as described above, as an extension function in C/C++.

推荐答案

不回答你的问题,但在MySQL UDF文章是pretty好:

Doesn't answer your question but article on MySQL udf is pretty good:

HTTP://www.$c$cproject.com/ KB /数据库/ MySQL的%5FUDFs.aspx

这篇关于MySQL的总UDF(用户定义函数)用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆