2012-10-19 13 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