2017-03-17 67 views
-1

我想知道是否有一些方法來擺脫這樣的代碼:刪除ELSEIF-ELSEIF ..在PostgreSQL函數

CREATE FUNCTION parent_function (json json, OUT response_status integer, OUT response json) RETURNS record 
    LANGUAGE plpgsql 
AS $$ 

DECLARE 
BEGIN 
    if <condition> then 
     <function_call1(json,response_status,response)>;  
     return; 
    elsif <condition> then 
     <function_call2(json,response_status,response)>;  
     return; 
    elsif <condition> then 
     <function_call3(json,response_status,response)>;  
     return; 
    end if; 
END; 
$$ 

我想移動到這些條件不同的功能,其中我想基於什麼函數返回(有一些輸出參數我需要)終止父函數,而不會拋出異常,並捕獲它們在父函數?

我想才達到這樣的代碼:

CREATE FUNCTION parent_function (json json, OUT response_status integer, OUT response json) RETURNS record 
    LANGUAGE plpgsql 
AS $$ 

DECLARE 
BEGIN 
    <check_function1(json,response_status,response)>; 
    <check_function2(json,response_status,response)>; 
    <check_function3(json,response_status,response)>; 
END; 
$$ 

調用從功能設置終止進一步的處理信息和輸出參數後。它有某種可能性嗎?

感謝, 盧卡斯

+1

例如, '如果則返回;結束如果;'? (他們需要返回'boolean'雖然) – pozs

+1

這對我來說還不清楚。什麼是父母功能? ''與''有什麼關係? –

+0

嘿傢伙,謝謝你的回答。我已經修改了一些我的情況,以便更清楚。我有一個從函數返回的參數。我們的項目中有很多if-elseif語句。我的目標是通過將這些條件移動到單獨的函數中使parent_function更易讀,並且如果一個條件失敗,它不應該進入下一個函數。 –

回答

0

你想要的東西,這樣的例子嗎?

CREATE OR REPLACE FUNCTION aa_test_check1( IN test_value integer) 
    RETURNS TABLE(problem boolean , problemmessage text) AS 
$BODY$ 
begin 
problem := false; 
if $1 <10 then 
problem := true; 
problemmessage := 'integer is smaller than 10'; 
end if; 
return next; 
end; 
$BODY$ LANGUAGE plpgsql IMMUTABLE; 

CREATE OR REPLACE FUNCTION aa_test_check2( IN test_value integer) 
    RETURNS TABLE(problem boolean , problemmessage text) AS 
$BODY$ 
begin 
problem := false; 
if $1 >20 then 
problem := true; 
problemmessage := 'integer is greater than 20'; 
end if; 
return next; 
end; 
$BODY$ LANGUAGE plpgsql IMMUTABLE; 

CREATE OR REPLACE FUNCTION aa_test_check3( IN test_value integer) 
    RETURNS TABLE(problem boolean , problemmessage text) AS 
$BODY$ 
begin 

problem := false; 
if $1 =10 then 
problem := true; 
problemmessage := 'something is wrong, it cant be 10'; 
end if; 
return next; 
end; 
$BODY$ LANGUAGE plpgsql IMMUTABLE; 

CREATE OR REPLACE FUNCTION aa_test_main( IN test_value integer) 
    RETURNS TABLE(status_id integer , problemmessage text) AS 
$BODY$ 
declare 
temp_problem boolean; 
temp_problemmessage text; 
begin 

status_id := 0; 
problemmessage := 'no problem, none at all'; 

select * from aa_test_check1($1) 
into temp_problem, temp_problemmessage; 

if temp_problem then 
    status_id := 1; 
    problemmessage := temp_problemmessage; 
    return next; 
    return; 
end if; 

select * from aa_test_check2($1) 
into temp_problem, temp_problemmessage; 

if temp_problem then 
    status_id := 2; 
    problemmessage := temp_problemmessage; 
    return next; 
    return; 
end if; 

select * from aa_test_check3($1) 
into temp_problem, temp_problemmessage; 

if temp_problem then 
    status_id := 3; 
    problemmessage := temp_problemmessage; 
    return next; 
    return; 
end if; 

return next; 

end; 
$BODY$ 
    LANGUAGE plpgsql IMMUTABLE 
    COST 100 
    ROWS 100; 
    select * from aa_test_main(10)