2
我有一个plpgsql函数,它作为其他几个函数的包装函数,所有函数都设置为返回不同数据类型的函数。通过这个函数,我试图获得一个通用集返回函数,它可以返回任何数据类型的集合。记录的数据类型根据输入表名和列名决定。泛型集返回函数
这里是代码片段:
create function cs_get(tablename text, colname text) returns setof record as $$
declare
type_name text;
ctype text;
loadfunc text;
result record;
begin
select data_type into type_name from information_schema.columns where table_name = tablename and column_name = colname;
if type_name is null then
raise exception 'Table % doesnot exist or does not have attribute % ',tablename, colname;
end if;
ctype := cs_get_ctype(type_name);
loadfunc := 'select * from cs_get_'||ctype||'('''||tablename||''','''||colname||''')';
for result in execute loadfunc loop
return next result;
end loop;
return;
end; $$ language plpgsql;
假设列的类型整数(相应的C类型的Int64)loadfunc的将是
select * from cs_get_int64(tablename, colname)
cs_get_int64是一组在C库中定义的返回函数返回值为int64
的值。
然而,这给出了一个错误说
ERROR: a column definition list is required for functions returning "record" at character 15
实现这一目标最简单的方法是为每一个数据类型返回setof text
。但这当然不是一个干净的方式来做到这一点。我曾尝试与
select cs_get_int64::integer from cs_get_int64(tablename, colname)
这是需要使用记录
更换loadFunc
。但是,这并没有帮助。
我现在的问题是: 是否有可能创建这样的通用集返回函数?如果是,如何?