简体   繁体   English

使用动态数量的参数创建MySQL存储函数

[英]Create a MySQL stored function with a dynamic number of arguments

I am trying to create a MySQL function IS_IN_ENUM('value', 'val1', 'val2', 'val3') which return true if 'value' is in ('val1', 'val2', 'val3'). 我正在尝试创建一个MySQL函数IS_IN_ENUM('value', 'val1', 'val2', 'val3') ,如果'value'在('val1','val2','val3')中,则返回true。 I know I can do SELECT 'value' IN ('val1', 'val2', 'val3') but that's less intersting because I just want to learn how to create such functions. 我知道我可以做SELECT 'value' IN ('val1', 'val2', 'val3')但这不是很SELECT 'value' IN ('val1', 'val2', 'val3') ,因为我只是想学习如何创建这样的函数。

I give you an example, consider the following ADD function : 我举个例子,考虑以下ADD函数:

CREATE FUNCTION my_add (
    a DOUBLE,
    b DOUBLE
)
RETURNS DOUBLE
BEGIN

    IF a IS NULL THEN
        SET a = 0;
    END IF;

    IF b IS NULL THEN
        SET b = 0;
    END IF;

    RETURN (a + b);
END;

If I do SELECT my_add(1, 1) , I get 2 (wow!). 如果我做SELECT my_add(1, 1) ,我得到2(哇!)。

How can I improve this function to be able to call : 如何改进此功能才能调用:

SELECT my_add(1, 1); -- 2
SELECT my_add(1, 1, 1); -- 3
SELECT my_add(1, 1, 1, 1); -- 4
SELECT my_add(1, 1, 1, 1, 1, 1, .....); -- n

The function example you show is a Stored Function , not a UDF. 您显示的函数示例是存储函数 ,而不是UDF。 Stored Functions in MySQL don't support a variable number of arguments, as @Enzino answered. MySQL中的存储函数不支持可变数量的参数,正如@Enzino所回答的那样。

MySQL UDFs are written in C or C++, compiled into dynamic object files, and then linked with the MySQL server with a different syntax of CREATE FUNCTION . MySQL UDF用C或C ++编写,编译成动态对象文件,然后用不同的CREATE FUNCTION语法与MySQL服务器链接。

See http://dev.mysql.com/doc/refman/5.5/en/adding-udf.html for details of writing UDFs. 有关编写UDF的详细信息,请参阅http://dev.mysql.com/doc/refman/5.5/en/adding-udf.html But I don't know if you want to get into writing C/C++ code to do this. 但我不知道你是否想要编写C / C ++代码来做到这一点。

MySQL UDFs do support variable number of arguments. MySQL UDF支持可变数量的参数。 In fact, all UDFs implicitly accept any number of arguments, and it's up to you as the programmer to determine if the number and datatypes of the arguments given are valid for your function. 实际上,所有UDF都隐式接受任意数量的参数,并且由程序员决定所给出的参数的数量和数据类型是否对您的函数有效。

Processing function arguments in UDFs is documented in http://dev.mysql.com/doc/refman/5.5/en/udf-arguments.html UDF中的处理函数参数记录在http://dev.mysql.com/doc/refman/5.5/en/udf-arguments.html中

I am trying to create a MySQL function IS_IN_ENUM('value', 'val1', 'val2', 'val3') which return true if 'value' is in ('val1', 'val2', 'val3'). 我正在尝试创建一个MySQL函数IS_IN_ENUM('value','val1','val2','val3'),如果'value'在('val1','val2','val3')中,则返回true。

For this you can use the native function FIELD: 为此,您可以使用本机函数FIELD:

http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_field http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_field

IS_IN_ENUM means FIELD != 0. IS_IN_ENUM表示FIELD!= 0。

Check also FIND_IN_SET 还要检查FIND_IN_SET

http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set

Stored functions do not support variable number of parameters. 存储的函数不支持可变数量的参数。

Now, if you really want to implement such a native function yourself in the MySQL server code, look for subclasses of Create_native_func in sql/item_create.cc 现在,如果你真的想实现这样的本地函数自己在MySQL服务器代码,查找的子类Create_native_funcsql/item_create.cc

老问题,但你不需要创建is_in_enum,因为它已经内置。只需从表中选择true,其中值为IN(1,2,3,4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM