0

I'm not very familiar with PL/SQL but I spotted a bunch of code on a buddy's project that looks very wrong to me, could I get a better alternative?

SET filter_conditions = 
  '
  AND 
  (
    ((
      processes.get_main_GC_id(nov.GCID) IN ('''', ''01'',''02'',''03'',''04'',''05'',''07'',''08'',''09'',''10'',''12'',''13'',''14'',''16'')
      OR (processes.get_main_GC_id(nov.idGrupoCliente) IN (''23'', ''29'') AND ExtraNumber != 0)
      OR (processes.get_main_GC_id(nov.idGrupoCliente) IN (''28'',''35'',''36'',''37''))
      OR (processes.get_main_GC_id(nov.idGrupoCliente) IN (''40'') AND ExtraNumber != 0)
      OR (processes.get_main_GC_id(nov.idGrupoCliente) IN (''151'', ''152'', ''153''))
    ) AND Cortesia !=  ''S'')
    OR
    (Cortesia =  ''S'' AND cortesiaGrupo = 6 AND cortesiaCampania = 5 AND calle != '''')
  )
  ';

Particularly the ridiculous amount of OR-INs. I edited a lot of them out but trust me there were hundreds. Is there a way to use a vector or something and check whether the variable matches?

martincito
  • 71
  • 1
  • 5
  • Looks like the two conditions with `AND ExtraNumber != 0` could be combined, as could the other two `processes.get_main_GC_id(nov.idGrupoCliente) IN ()` lists. What type does `processes.get_main_GC_id()` return? If it's numeric you could lose a ton of quotes. Also `= ''` and `!= ''` will never be true as @GreenT mentioned. The whole thing is a bit unstructured though, so it's hard to see any really systematic way of refactoring it. Also it looks like it's SQL (that is, a query) not PL/SQL (the programming language). – William Robertson Nov 04 '18 at 18:25

1 Answers1

0
declare
  l_filter_conditions VARCHAR2(2000) := q'{
                                            AND (
                                                 (
                                                   (processes.get_main_GC_id(nov.GCID) IN ( '01','02','03','04','05','07','08','09','10','12','13','14','16') OR processes.get_main_GC_id(nov.GCID) is NULL)
                                                OR (processes.get_main_GC_id(nov.idGrupoCliente) IN ('23', '29','40') AND ExtraNumber != 0) 
                                                OR (processes.get_main_GC_id(nov.idGrupoCliente) IN ('28','35','36','37','151','152','153')) 
                                                ) 
                                                AND Cortesia !=  'S' OR (Cortesia,cortesiaGrupo,cortesiaCampania) =  (('S',6,5)) AND calle IS NOT NULL
                                                )
                                        }';
begin
DBMS_OUTPUT.PUT_LINE(l_filter_conditions); 
end;

Statement processed.

AND ( ( (processes.get_main_GC_id(nov.GCID) IN ( '01','02','03','04','05','07','08','09','10','12','13','14','16') OR processes.get_main_GC_id(nov.GCID) is NULL) OR (processes.get_main_GC_id(nov.idGrupoCliente) IN ('23', '29','40') AND ExtraNumber != 0) OR (processes.get_main_GC_id(nov.idGrupoCliente) IN ('28','35','36','37','151','152','153')) ) AND Cortesia != 'S' OR (Cortesia,cortesiaGrupo,cortesiaCampania) = (('S',6,5)) AND calle IS NOT NULL )

I don't know if this answers your question but i took myself some time to re-stylish your code.

GreenT
  • 83
  • 1
  • 6