如何将自定义类型数组传递给 Postgres 函数 [英] How to pass custom type array to Postgres function

查看:49
本文介绍了如何将自定义类型数组传递给 Postgres 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类型

CREATE TYPE mytype as (id uuid, amount numeric(13,4));

我想将它传递给具有以下签名的函数:

I want to pass it to a function with the following signature:

CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[])
  RETURNS BOOLEAN AS...

如何在 postgres 查询中调用它,并且不可避免地从 PHP 调用?

How can I call this in postgres query and inevitably from PHP?

推荐答案

您可以使用带有 数组字面量 而不是数组构造函数,它是一个类似于 Postgres 函数的构造,当您需要时可能会导致麻烦传递 values - 就像在准备好的语句中一样:

You can use the alternative syntax with a array literal instead of the array constructor, which is a Postgres function-like construct and may cause trouble when you need to pass values - like in a prepared statement:

SELECT myschema.myfunc('0d6311cc-0d74-4a32-8cf9-87835651e1ee'
                  , '{"(0d6311cc-0d74-4a32-8cf9-87835651e1ee, 25)"
                    , "(6449fb3b-844e-440e-8973-31eb6bbefc81, 10)"}'::mytype[]);

我在数组中的两个行类型之间添加了一个换行符以进行显示.这是合法的.

I added a line break between the two row types in the array for display. That's legal.

问问 Postgres.这是一个演示:

Just ask Postgres. Here is a demo:

CREATE TABLE mytype (id uuid, amount numeric(13,4));

INSERT INTO mytype VALUES
  ('0d6311cc-0d74-4a32-8cf9-87835651e1ee', 25)
 ,('6449fb3b-844e-440e-8973-31eb6bbefc81', 10);

SELECT ARRAY(SELECT m FROM mytype m);

返回:

{"(0d6311cc-0d74-4a32-8cf9-87835651e1ee,25.0000)","(6449fb3b-844e-440e-8973-31eb6bbefc81,10.0000)"}

db<>fiddle 这里

任何表(包括临时表)都会隐式地创建一个同名的行类型.

Any table (including temporary tables) implicitly creates a row type of the same name.

这篇关于如何将自定义类型数组传递给 Postgres 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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