2012-10-19 27 views
2

我一直在PL/SQL中進行一些數據轉換/處理,並且想從我的包中刪除重複代碼的。這是相關的代碼部分。回覆函數在Oracle PL/SQL中

輸入類型的表函數:

type t_legs_cur is ref cursor return legs%rowtype; 

過程,處理一條記錄:

procedure discontinuity_rule(p_leg in out nocopy legs%rowtype) as 
begin 
    null; --business logic here 
end discontinuity_rule; 

表函數,在一個光標,處理每一行中的光標和管道的輸出迭代(如果任何):

function apply_discontinuity_rule(p_cur t_legs_cur) 
    return t_legs pipelined 
    order p_cur by (/* some fields */)  
    parallel_enable (partition p_cur by range (/* some fields */)) 
as 
    v_leg legs%rowtype; 
begin 
    loop 
    fetch p_cur into v_leg; 
    exit when p_cur%notfound; 

    discontinuity_rule(v_leg); --call back 

    if v_leg.id is not null then 
     pipe row (v_leg); 
    end if; 

    end loop; 
end apply_discontinuity_rule; 

轉換/處理有幾個步驟,例如我將運行以下選擇做一些處理,並應用一些規則給定的順序:

select * from table(trip_rules.generate_trips_from_legs(cursor(
    select * from table(trip_rules.apply_5_legs_rule(cursor(
    select * from table (trip_rules.apply_previous_city_rule(cursor(
     select * from table (trip_rules.apply_backhaul_rule(cursor(
     select * from table(trip_rules.apply_connection_time_rule(cursor(
      select * from table(trip_rules.apply_discontinuity_rule(cursor(
      select * from table(trip_rules.generate_legs_from_input(cursor(
       select * from INPUT_DATA 
      ))) 
     ))) 
     ))) 
    ))) 
    ))) 
))) 
))); 

這是所有罰款和花花公子,唯一的問題是,我trip_rule包中包含許多apply_*_rule功能。它們都與示例apply_discontinuity_rule類似。唯一的區別是他們回撥的實際程序(discontinuity_rule)。

因此,我的問題是,我該如何避免複製apply_*函數的代碼。有沒有更優雅的方式來做到這一點,然後用一個大的,如果:if p_rule_name == 'discontinuity_rule' then

function apply_rule(p_cur t_legs_cur, p_rule_name in varchar2) 
    return t_legs pipelined 
    order p_cur by (/* some fields */)  
    parallel_enable (partition p_cur by range (/* some fields */)) 
as 
    v_leg legs%rowtype; 
begin 
    loop 
    fetch p_cur into v_leg; 
    exit when p_cur%notfound; 

    if p_rule_name == 'discontinuity_rule' then 
     discontinuity_rule(v_leg); 
    elsif p_rule_name == 'other_rule' then 
     other_rule(v_leg); 
    elsif p_rule_name == 'totally_other_rule' then 
     totally_other_rule(v_leg); 
    -- and so on... 
    end if; 

    if v_leg.id is not null then 
     pipe row (v_leg); 
    end if; 

    end loop; 
end apply_rule; 

我也明白,這將是可能的使用過程名來動態創建一個匿名PL/SQL塊,並執行它動態SQL。我想知道是否可以正確完成,而不會殺死我的表現。任何想法是讚賞。

回答

3

您的巨大IF語句不是重複的代碼。

誠然,它具有彼此相似,但是這部分...

elsif p_rule_name == 'other_rule' then 
    other_rule(v_leg); 

...是最肯定不一樣,這...

elsif p_rule_name == 'totally_other_rule' then 
    totally_other_rule(v_leg); 

動態PL/SQL除非沒有別的選擇,否則我們應該引起注意。這裏沒有必要。

+0

感謝您的回覆。你是對的,如果我在開始發佈這個問題之前就已經擁有了重複的apply _ * _ rule'函數,那麼這個問題就不會那麼糟糕。當我說出這個問題的時候,我想出了大個子。除非有更好的選擇,否則我會這樣做。 – bpgergo