Postgres:如何重载具有单个记录类型输入参数的函数 [英] Postgres: How to overload functions having single record type input parameter

查看:119
本文介绍了Postgres:如何重载具有单个记录类型输入参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户定义的类型:

I have a user defined type:

create type match_input as (
  _values text[],    
  _name text, 
  _norm_fn text, 
  _operator text
);

并且我将其用作函数的输入参数:

and I use this as the input parameter for a function:

get_matches(mi match_input)

我希望能够调用相同的函数,但要为_values传递单个文本值.所以我定义了一个新类型:

I want to be able to call the same function, but passing a single text value for _values. So I defined a new type:

 create type match_input_simple as (
  _values text,    
  _name text, 
  _norm_fn text, 
  _operator text
);

如果我尝试通过以下方法重载该功能:

If I try and overload the function with the following:

create or replace function get_matches(_mis match_input_simple)
  returns setof contact_index
as $func$
  select get_matches((array[_mis._values], _mis._name, 
_mis._norm_fn, _mis._operator)::match_input);
$func$
language sql strict;

该函数编译时没有抱怨,但是当我运行该函数时,出现此错误:

The function compiles without complaining, but when I run the function I get this error:

ERROR:  function get_matches(record) is not unique
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

因此,在尝试决定要运行哪个函数时,postgres似乎无法分辨出我的两种不同记录类型之间的区别. 我不希望用户每次调用该函数时都必须显式地键入强制转换记录,因为这样会破坏试图简化界面的目的.

So it looks like postgres can't tell the difference between my two different record types when its trying to decide which function to run. I don't want the user to have to explicitly type cast the record each time the function is called, since that kind of defeats the purpose of trying to make the interface simple.

推荐答案

不进行强制转换就无法完成.

It can't be done without casting.

请记住,您可以具有内容完全相同的多种类型,并为每种类型创建一个重载.数据库将如何选择正确的类型和功能?

Remember that you can have multiple types with the exactly same content and create a overload for each one of them. How would the database choose the right type and function?

这篇关于Postgres:如何重载具有单个记录类型输入参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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