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

Skip to content

Commit 49b1b84

Browse files
committed
Move get_cov_sql function into common package to remove code duplication
1 parent d8b8e87 commit 49b1b84

7 files changed

Lines changed: 108 additions & 246 deletions

source/core/coverage/ut_coverage.pkb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,78 @@ create or replace package body ut_coverage is
1919

2020
type t_source_lines is table of binary_integer;
2121

22+
function get_cov_sources_sql(a_coverage_options ut_coverage_options, a_skipped_lines varchar2 default 'Y') return varchar2 is
23+
l_result varchar2(32767);
24+
l_full_name varchar2(100);
25+
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_source');
26+
begin
27+
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
28+
l_full_name := 'f.file_name';
29+
else
30+
l_full_name := 'lower(s.owner||''.''||s.name)';
31+
end if;
32+
l_result := '
33+
select full_name, owner, name, line, to_be_skipped, text
34+
from (
35+
select '||l_full_name||q'[ as full_name,
36+
s.owner,
37+
s.name,
38+
s.line -
39+
coalesce(
40+
case when type!='TRIGGER' then 0 end,
41+
(select min(t.line) - 1
42+
from ]'||l_view_name||q'[ t
43+
where t.owner = s.owner and t.type = s.type and t.name = s.name
44+
and regexp_like( t.text, '[A-Za-z0-9$#_]*(begin|declare|compound).*','i'))
45+
) as line,
46+
s.text, ]';
47+
if a_skipped_lines = 'Y' then
48+
l_result := l_result ||
49+
q'[case
50+
when
51+
-- to avoid execution of regexp_like on every line
52+
-- first do a rough check for existence of search pattern keyword
53+
(lower(s.text) like '%procedure%'
54+
or lower(s.text) like '%function%'
55+
or lower(s.text) like '%begin%'
56+
or lower(s.text) like '%end%'
57+
or lower(s.text) like '%package%'
58+
) and
59+
regexp_like(
60+
s.text,
61+
'^([\t ]*(((not)?\s*(overriding|final|instantiable)[\t ]*)*(static|constructor|member)?[\t ]*(procedure|function)|package([\t ]+body)|begin|end([\t ]+\S+)*[ \t]*;))', 'i'
62+
)
63+
then 'Y'
64+
end as to_be_skipped ]';
65+
else
66+
l_result := l_result || q'['N' as to_be_skipped ]';
67+
end if;
68+
69+
l_result := l_result ||' from '||l_view_name||q'[ s]';
70+
71+
if a_coverage_options.file_mappings is not empty then
72+
l_result := l_result || '
73+
join table(:file_mappings) f
74+
on s.name = f.object_name
75+
and s.type = f.object_type
76+
and s.owner = f.object_owner
77+
where 1 = 1';
78+
elsif a_coverage_options.include_objects is not empty then
79+
l_result := l_result || '
80+
where (s.owner, s.name) in (select il.owner, il.name from table(:include_objects) il)';
81+
else
82+
l_result := l_result || '
83+
where s.owner in (select upper(t.column_value) from table(:l_schema_names) t)';
84+
end if;
85+
l_result := l_result || q'[
86+
and s.type not in ('PACKAGE', 'TYPE', 'JAVA SOURCE')
87+
--Exclude calls to utPLSQL framework, Unit Test packages and objects from a_exclude_list parameter of coverage reporter
88+
and (s.owner, s.name) not in (select el.owner, el.name from table(:l_skipped_objects) el)
89+
)
90+
where line > 0]';
91+
return l_result;
92+
end;
93+
2294
function get_cov_sources_cursor(a_coverage_options in ut_coverage_options,a_sql in varchar2) return sys_refcursor is
2395
l_cursor sys_refcursor;
2496
l_skip_objects ut_object_names;

source/core/coverage/ut_coverage.pks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ create or replace package ut_coverage authid current_user is
6363
,executions number(38, 0) := 0
6464
,objects tt_program_units);
6565

66+
function get_cov_sources_sql(a_coverage_options ut_coverage_options, a_skipped_lines varchar2 default 'Y') return varchar2;
67+
6668
procedure populate_tmp_table(a_coverage_options ut_coverage_options, a_sql in varchar2);
6769

6870
procedure coverage_start(a_coverage_options ut_coverage_options default null);

source/core/coverage/ut_coverage_block.pkb

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,6 @@ create or replace package body ut_coverage_block is
1919

2020
type t_source_lines is table of binary_integer;
2121

22-
-- The source query has two important transformations done in it.
23-
-- the flag: to_be_skipped ='Y' is set for a line of code that is badly reported by DBMS_PROFILER as executed 0 times.
24-
-- This includes lines that are:
25-
-- - PACKAGE, PROCEDURE, FUNCTION definition line,
26-
-- - BEGIN, END of a block
27-
-- Another transformation is adjustment of line number for TRIGGER body.
28-
-- DBMS_PROFILER is reporting line numbers for triggers not as defined in DBA_SOURCE, its usign line numbers as defined in DBA_TRIGGERS
29-
-- the DBA_TRIGGERS does not contain the trigger specification lines, only lines that define the trigger body.
30-
-- the query adjusts the line numbers for triggers by finding first occurrence of begin|declare|compound in the trigger body line.
31-
-- The subquery is optimized by:
32-
-- - COALESCE function -> it will execute only for TRIGGERS
33-
-- - scalar subquery cache -> it will only execute once for one trigger source code.
34-
function get_cov_sources_sql(a_coverage_options ut_coverage_options) return varchar2 is
35-
l_result varchar2(32767);
36-
l_full_name varchar2(100);
37-
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_source');
38-
begin
39-
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
40-
l_full_name := 'f.file_name';
41-
else
42-
l_full_name := 'lower(s.owner||''.''||s.name)';
43-
end if;
44-
l_result := '
45-
select full_name, owner, name, line, to_be_skipped, text
46-
from (
47-
select '||l_full_name||q'[ as full_name,
48-
s.owner,
49-
s.name,
50-
s.line -
51-
coalesce(
52-
case when type!='TRIGGER' then 0 end,
53-
(select min(t.line) - 1
54-
from ]'||l_view_name||q'[ t
55-
where t.owner = s.owner and t.type = s.type and t.name = s.name
56-
and regexp_like( t.text, '[A-Za-z0-9$#_]*(begin|declare|compound).*','i'))
57-
) as line,
58-
s.text, 'N' as to_be_skipped
59-
from ]'||l_view_name||q'[ s]';
60-
61-
if a_coverage_options.file_mappings is not empty then
62-
l_result := l_result || '
63-
join table(:file_mappings) f
64-
on s.name = f.object_name
65-
and s.type = f.object_type
66-
and s.owner = f.object_owner
67-
where 1 = 1';
68-
elsif a_coverage_options.include_objects is not empty then
69-
l_result := l_result || '
70-
where (s.owner, s.name) in (select il.owner, il.name from table(:include_objects) il)';
71-
else
72-
l_result := l_result || '
73-
where s.owner in (select upper(t.column_value) from table(:l_schema_names) t)';
74-
end if;
75-
l_result := l_result || q'[
76-
and s.type not in ('PACKAGE', 'TYPE', 'JAVA SOURCE')
77-
--Exclude calls to utPLSQL framework, Unit Test packages and objects from a_exclude_list parameter of coverage reporter
78-
and (s.owner, s.name) not in (select el.owner, el.name from table(:l_skipped_objects) el)
79-
)
80-
where line > 0]';
81-
return l_result;
82-
end;
83-
84-
8522
/**
8623
* Public functions
8724
*/
@@ -95,7 +32,7 @@ create or replace package body ut_coverage_block is
9532
l_source_object ut_coverage_helper.t_tmp_table_object;
9633
begin
9734
--prepare global temp table with sources
98-
ut_coverage.populate_tmp_table(a_coverage_options,get_cov_sources_sql(a_coverage_options));
35+
ut_coverage.populate_tmp_table(a_coverage_options,ut_coverage.get_cov_sources_sql(a_coverage_options,'N'));
9936

10037
l_source_objects_crsr := ut_coverage_helper.get_tmp_table_objects_cursor();
10138
loop

source/core/coverage/ut_coverage_extended.pkb

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,9 @@ create or replace package body ut_coverage_extended is
1616
limitations under the License.
1717
*/
1818

19-
function get_cov_sources_sql(a_coverage_options ut_coverage_options) return varchar2 is
20-
l_result varchar2(32767);
21-
l_full_name varchar2(100);
22-
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_source');
23-
begin
24-
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
25-
l_full_name := 'f.file_name';
26-
else
27-
l_full_name := 'lower(s.owner||''.''||s.name)';
28-
end if;
29-
l_result := '
30-
select full_name, owner, name, line, to_be_skipped, text
31-
from (
32-
select '||l_full_name||q'[ as full_name,
33-
s.owner,
34-
s.name,
35-
s.line -
36-
coalesce(
37-
case when type!='TRIGGER' then 0 end,
38-
(select min(t.line) - 1
39-
from ]'||l_view_name||q'[ t
40-
where t.owner = s.owner and t.type = s.type and t.name = s.name
41-
and regexp_like( t.text, '[A-Za-z0-9$#_]*(begin|declare|compound).*','i'))
42-
) as line,
43-
s.text, 'N' as to_be_skipped
44-
from ]'||l_view_name||q'[ s]';
45-
46-
if a_coverage_options.file_mappings is not empty then
47-
l_result := l_result || '
48-
join table(:file_mappings) f
49-
on s.name = f.object_name
50-
and s.type = f.object_type
51-
and s.owner = f.object_owner
52-
where 1 = 1';
53-
elsif a_coverage_options.include_objects is not empty then
54-
l_result := l_result || '
55-
where (s.owner, s.name) in (select il.owner, il.name from table(:include_objects) il)';
56-
else
57-
l_result := l_result || '
58-
where s.owner in (select upper(t.column_value) from table(:l_schema_names) t)';
59-
end if;
60-
l_result := l_result || q'[
61-
and s.type not in ('PACKAGE', 'TYPE', 'JAVA SOURCE')
62-
--Exclude calls to utPLSQL framework, Unit Test packages and objects from a_exclude_list parameter of coverage reporter
63-
and (s.owner, s.name) not in (select el.owner, el.name from table(:l_skipped_objects) el)
64-
)
65-
where line > 0]';
66-
return l_result;
67-
end;
68-
69-
7019
/**
7120
* Public functions
72-
*/
73-
74-
/* Function extend coverage
75-
P - profiler line result, C - coverage line result, X-result
76-
Dla ka?dej linii:X=greatest(P,nvl(C,0))
77-
Czyli:
78-
If P is null - line is irrelevant
79-
If P or X > 0 - line is covered
80-
*/
81-
21+
*/
8222

8323
function get_extended_coverage(a_coverage_options ut_coverage_options) return ut_coverage.t_coverage is
8424
l_result ut_coverage.t_coverage;
@@ -93,7 +33,7 @@ create or replace package body ut_coverage_extended is
9333
l_result_profiler:= ut_coverage_proftab.get_coverage_data_profiler(a_coverage_options => a_coverage_options);
9434

9535

96-
ut_coverage.populate_tmp_table(a_coverage_options,get_cov_sources_sql(a_coverage_options));
36+
ut_coverage.populate_tmp_table(a_coverage_options,ut_coverage.get_cov_sources_sql(a_coverage_options,'N'));
9737

9838
l_source_objects_crsr := ut_coverage_helper.get_tmp_table_objects_cursor();
9939
loop

source/core/coverage/ut_coverage_proftab.pkb

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,7 @@ create or replace package body ut_coverage_proftab is
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
19-
-- The source query has two important transformations done in it.
20-
-- the flag: to_be_skipped ='Y' is set for a line of code that is badly reported by DBMS_PROFILER as executed 0 times.
21-
-- This includes lines that are:
22-
-- - PACKAGE, PROCEDURE, FUNCTION definition line,
23-
-- - BEGIN, END of a block
24-
-- Another transformation is adjustment of line number for TRIGGER body.
25-
-- DBMS_PROFILER is reporting line numbers for triggers not as defined in DBA_SOURCE, its usign line numbers as defined in DBA_TRIGGERS
26-
-- the DBA_TRIGGERS does not contain the trigger specification lines, only lines that define the trigger body.
27-
-- the query adjusts the line numbers for triggers by finding first occurrence of begin|declare|compound in the trigger body line.
28-
-- The subquery is optimized by:
29-
-- - COALESCE function -> it will execute only for TRIGGERS
30-
-- - scalar subquery cache -> it will only execute once for one trigger source code.
31-
function get_cov_sources_sql(a_coverage_options ut_coverage_options) return varchar2 is
32-
l_result varchar2(32767);
33-
l_full_name varchar2(100);
34-
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_source');
35-
begin
36-
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
37-
l_full_name := 'f.file_name';
38-
else
39-
l_full_name := 'lower(s.owner||''.''||s.name)';
40-
end if;
41-
l_result := '
42-
select full_name, owner, name, line, to_be_skipped, text
43-
from (
44-
select '||l_full_name||q'[ as full_name,
45-
s.owner,
46-
s.name,
47-
s.line -
48-
coalesce(
49-
case when type!='TRIGGER' then 0 end,
50-
(select min(t.line) - 1
51-
from ]'||l_view_name||q'[ t
52-
where t.owner = s.owner and t.type = s.type and t.name = s.name
53-
and regexp_like( t.text, '[A-Za-z0-9$#_]*(begin|declare|compound).*','i'))
54-
) as line,
55-
s.text,
56-
case
57-
when
58-
-- to avoid execution of regexp_like on every line
59-
-- first do a rough check for existence of search pattern keyword
60-
(lower(s.text) like '%procedure%'
61-
or lower(s.text) like '%function%'
62-
or lower(s.text) like '%begin%'
63-
or lower(s.text) like '%end%'
64-
or lower(s.text) like '%package%'
65-
) and
66-
regexp_like(
67-
s.text,
68-
'^([\t ]*(((not)?\s*(overriding|final|instantiable)[\t ]*)*(static|constructor|member)?[\t ]*(procedure|function)|package([\t ]+body)|begin|end([\t ]+\S+)*[ \t]*;))', 'i'
69-
)
70-
then 'Y'
71-
end as to_be_skipped
72-
from ]'||l_view_name||q'[ s]';
73-
74-
if a_coverage_options.file_mappings is not empty then
75-
l_result := l_result || '
76-
join table(:file_mappings) f
77-
on s.name = f.object_name
78-
and s.type = f.object_type
79-
and s.owner = f.object_owner
80-
where 1 = 1';
81-
elsif a_coverage_options.include_objects is not empty then
82-
l_result := l_result || '
83-
where (s.owner, s.name) in (select il.owner, il.name from table(:include_objects) il)';
84-
else
85-
l_result := l_result || '
86-
where s.owner in (select upper(t.column_value) from table(:l_schema_names) t)';
87-
end if;
88-
l_result := l_result || q'[
89-
and s.type not in ('PACKAGE', 'TYPE', 'JAVA SOURCE')
90-
--Exclude calls to utPLSQL framework, Unit Test packages and objects from a_exclude_list parameter of coverage reporter
91-
and (s.owner, s.name) not in (select el.owner, el.name from table(:l_skipped_objects) el)
92-
)
93-
where line > 0]';
94-
return l_result;
95-
end;
96-
18+
9719
/**
9820
* Public functions
9921
*/
@@ -107,7 +29,7 @@ create or replace package body ut_coverage_proftab is
10729
begin
10830

10931
--prepare global temp table with sources
110-
ut_coverage.populate_tmp_table(a_coverage_options,get_cov_sources_sql(a_coverage_options));
32+
ut_coverage.populate_tmp_table(a_coverage_options,ut_coverage.get_cov_sources_sql(a_coverage_options,'Y'));
11133

11234
l_source_objects_crsr := ut_coverage_helper.get_tmp_table_objects_cursor();
11335
loop

0 commit comments

Comments
 (0)