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
。但是,這並沒有幫助。
我現在的問題是: 是否有可能創建這樣的通用集返回函數?如果是,如何?