필자는 plpgsql 함수가 여러 다른 함수의 래퍼 역할을하며,이 함수는 서로 다른 데이터 유형의 함수를 반환하도록 설정되어 있습니다. 이 함수를 통해 모든 데이터 형식 집합을 반환 할 수있는 함수를 반환하는 일반 집합을 얻으려고합니다. 레코드의 데이터 유형은 입력 테이블 이름과 열 이름을 기반으로 결정됩니다. 열 유형 정수 (대응하는 C 타입 INT64된다), loadfunc이다일반 세트 반환 함수
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;
가정하는가
select * from cs_get_int64(tablename, colname)
cs_get_int64 한 세트 인 것 : 여기
는 코드이며 -int64
의 값을 리턴하는 C 라이브러리에 정의 된 리턴 함수.
그러나,이
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)
으로 대체하려고 시도했다. 그러나 이것은 도움이되지 못했습니다.
내 질문의 현재 : 함수를 반환하는 일반적인 집합을 만들 수 있습니까? 그렇다면 어떻게?