用户定义的类型作为PostgreSQL函数中的输入参数 [英] user defined type as input parameters in PostgreSQL function
问题描述
您好,我正在创建一个用于插入元数据的过程.我创建了类型,并在另一种类型中包含了1个类型,在过程中,我对其进行了迭代以获取值.由于我是PostgreSQL的新手,任何人都可以帮助我如何调用该过程.输入参数为类型
Hi I am creating a procedure for insertion of meta data. I created types and I included 1 type in another type and in procedure I am iterating it to get the value. Since I am new to PostgreSQL Can anyone help me on how to call the procedure. The input parameter is type
Create Type Form_details as(
formName character varying(100),
submittedBy numeric,
createdDate date,
updatedBy numeric,
updatedDate date,
comments character varying(500),
Sections Section[]
)
create type Section as (
sectionName character varying(100),
sectionLabel character varying(100),
sectionOrder numeric
)
我编写的过程是
CREATE OR REPLACE FUNCTION form_insertion(formdetails form_details[])
RETURNS character varying AS
$BODY$
DECLARE
form_details_seq integer;
section_seq integer;
formName character varying(100);
submittedBy numeric;
createdDate date;
comments character varying(500);
formStatusId numeric;
sectionOrder numeric;
sectionName character varying(100);
sectionLabel character varying(100);
attributeId numeric;
I integer;
J integer;
begin
FOR I IN 1..formdetails.COUNT
LOOP
formName :=formdetails[I].formName;
formStatusId :=formdetails[I].formStatusId;
comments :=formdetails[I].comments;
RAISE NOTICE '%', formName;
FOR J IN 1..formdetails.Section.COUNT
LOOP
sectionName :=formdetails[I].Section[J].sectionName;
RAISE NOTICE '%', sectionName;
END LOOP ;
END LOOP;
Return formName,sectionName;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
这不是完整的过程.但我正在尝试对此进行测试.您能否让我知道我的方法是否正确,以及如何从数据库方面进行测试.我将如何传递此参数.顺便说一下,我创建的类型来自Java对象.该过程将从Java端调用.任何帮助将不胜感激.
This is not complete procedure. But I am trying to test with this. Can you please let me know whether my approach is correct and how can I test it from DB side. How I will pass this parameter. By the way the type I created is from Java object. this procedure will be calling from Java end. Any help would be greatly appreciated.
推荐答案
要从SQL查询中调用该函数,必须将参数强制转换为自定义类型,如以下示例所示.
To call the function from a SQL query, you must cast the parameters to your custom type as in the following example.
select form_insertion(array[
cast(row('Form 1', 1, current_date, 1, current_date, 'This is form 1',
array[
cast(row('section-1', 'Section One', 1) as section),
cast(row('section-2', 'Section Two', 2) as section),
cast(row('section-3', 'Section Three', 3) as section)
]
) as form_details),
cast(row('Form 2', 2, current_date, 1, current_date, 'This is form 2',
array[
cast(row('section-2', 'Section Two', 2) as section),
cast(row('section-3', 'Section Three', 3) as section)
]
) as form_details),
cast(row('Form 3', 1, current_date, 1, current_date, 'This is form 3',
array[
cast(row('section-1', 'Section One', 1) as section),
cast(row('section-3', 'Section Three', 3) as section)
]
) as form_details)
])
请注意,PostgreSQL数组没有.COUNT属性.您可以使用在索引范围内遍历数组array_upper 函数:
Note that PostgreSQL arrays don't have a .COUNT property. You can iterate through an array by index range with array_upper function:
for i IN 1..array_upper(formdetails, 1)
LOOP
-- your code here
END LOOP;
从PostgreSQL 9.1开始,您可以使用FOREACH语句遍历数组:
Since PostgreSQL 9.1, you can use the FOREACH statement to loop through the array:
create or replace function form_insertion(formdetails form_details[])
returns varchar as $$
declare
detail form_details;
sec section;
begin
foreach detail in array formdetails
LOOP
RAISE NOTICE '%', detail.formName;
foreach sec in array detail.sections
LOOP
raise NOTICE '%', sec.sectionName;
END LOOP;
END LOOP;
return '';
end;$$
language plpgsql;
这篇关于用户定义的类型作为PostgreSQL函数中的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!