Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6306af6

Browse files
committed
Rework dynamic sql generation to more readible format.
1 parent faf3d12 commit 6306af6

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

source/expectations/data_values/ut_compound_data_helper.pkb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ create or replace package body ut_compound_data_helper is
2121
g_type_name_map t_type_name_map;
2222
g_anytype_name_map t_type_name_map;
2323

24-
gc_compare_sql_template varchar2(4000) :=
24+
g_compare_sql_template varchar2(4000) :=
2525
q'[
2626
with exp as (
2727
select
@@ -257,10 +257,10 @@ create or replace package body ut_compound_data_helper is
257257
procedure gen_sql_pieces_out_of_cursor(
258258
a_data_info ut_cursor_column_tab,
259259
a_pk_table ut_varchar2_list,
260+
a_unordered boolean,
260261
a_xml_stmt out nocopy clob,
261262
a_select_stmt out nocopy clob,
262263
a_partition_stmt out nocopy clob,
263-
a_equal_stmt out nocopy clob,
264264
a_join_by_stmt out nocopy clob,
265265
a_not_equal_stmt out nocopy clob
266266
) is
@@ -304,12 +304,22 @@ create or replace package body ut_compound_data_helper is
304304
a_select_stmt := nullif(','||ut_utils.table_to_clob(l_select_list, ' , '),',');
305305
l_partition_tmp := ut_utils.table_to_clob(l_partition_list, ' , ');
306306
ut_utils.append_to_clob(a_partition_stmt,' row_number() over (partition by '||l_partition_tmp||' order by '||l_partition_tmp||' ) ');
307-
a_equal_stmt := ut_utils.table_to_clob(l_equal_list, ' and ');
308-
a_join_by_stmt := ut_utils.table_to_clob(l_join_by_list, ' and ');
307+
308+
if a_pk_table.count > 0 then
309+
-- If key defined do the join or these and where on diffrences
310+
a_join_by_stmt := ut_utils.table_to_clob(l_join_by_list, ' and ');
311+
elsif a_unordered then
312+
-- If no key defined do the join on all columns
313+
a_join_by_stmt := ' e.dup_no = a.dup_no and '||ut_utils.table_to_clob(l_equal_list, ' and ');
314+
else
315+
-- Else join on rownumber
316+
a_join_by_stmt := 'a.item_no = e.item_no ';
317+
end if;
309318
a_not_equal_stmt := ut_utils.table_to_clob(l_not_equal_list, ' or ');
310319
else
311320
--Partition by piece when no data
312321
ut_utils.append_to_clob(a_partition_stmt,' 1 ');
322+
a_join_by_stmt := 'a.item_no = e.item_no ';
313323
end if;
314324
end;
315325

@@ -352,12 +362,12 @@ create or replace package body ut_compound_data_helper is
352362
begin
353363
dbms_lob.createtemporary(l_compare_sql, true);
354364
--Initiate a SQL template with placeholders
355-
ut_utils.append_to_clob(l_compare_sql, gc_compare_sql_template);
365+
ut_utils.append_to_clob(l_compare_sql, g_compare_sql_template);
356366
--Generate a pieceso of dynamic SQL that will substitute placeholders
357367
gen_sql_pieces_out_of_cursor(
358-
a_other.cursor_details.cursor_columns_info, a_join_by_list,
359-
l_xmltable_stmt, l_select_stmt, l_partition_stmt, l_equal_stmt,
360-
l_join_on_stmt, l_not_equal_stmt
368+
a_other.cursor_details.cursor_columns_info, a_join_by_list, a_unordered,
369+
l_xmltable_stmt, l_select_stmt, l_partition_stmt, l_join_on_stmt,
370+
l_not_equal_stmt
361371
);
362372

363373
l_compare_sql := replace(l_compare_sql,'{:duplicate_number:}',l_partition_stmt);
@@ -367,24 +377,13 @@ create or replace package body ut_compound_data_helper is
367377
l_compare_sql := replace(l_compare_sql,'{:xml_to_columns:}',l_xmltable_stmt);
368378
l_compare_sql := replace(l_compare_sql,'{:item_no_select:}',case when a_unordered then 'rownum' else 'nvl(e.item_no,a.item_no)' end);
369379
l_compare_sql := replace(l_compare_sql,'{:join_type:}',get_join_type(a_inclusion_type,a_is_negated));
370-
371-
--TODO : Refactor as equal and join is single stmt
372-
if (a_join_by_list.count > 0) then
373-
-- If key defined do the join or these and where on diffrences
374-
l_compare_sql := replace(l_compare_sql,'{:join_condition:}',l_join_on_stmt);
375-
elsif a_unordered then
376-
-- If no key defined do the join on all columns
377-
l_compare_sql := replace(l_compare_sql,'{:join_condition:}',' e.dup_no = a.dup_no and '||l_equal_stmt);
378-
else
379-
l_compare_sql := replace(l_compare_sql,'{:join_condition:}','a.item_no = e.item_no ');
380-
end if;
380+
l_compare_sql := replace(l_compare_sql,'{:join_condition:}',l_join_on_stmt);
381381

382382
if l_not_equal_stmt is not null then
383383
if (a_join_by_list.count > 0 and not a_is_negated) or (not a_unordered) then
384384
ut_utils.append_to_clob(l_where_stmt,' ( '||l_not_equal_stmt||' ) or ');
385385
end if;
386386
end if;
387-
388387
--If its inclusion we expect a actual set to fully match and have no extra elements over expected
389388
if a_inclusion_type then
390389
ut_utils.append_to_clob(l_where_stmt,case when a_is_negated then ' 1 = 1 ' else ' ( a.data_id is null ) ' end);
@@ -394,7 +393,6 @@ create or replace package body ut_compound_data_helper is
394393

395394
l_compare_sql := replace(l_compare_sql,'{:where_condition:}',l_where_stmt);
396395

397-
--dbms_output.put_line(l_compare_sql);
398396
return l_compare_sql;
399397
end;
400398

0 commit comments

Comments
 (0)