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

Skip to content

Commit 9c19355

Browse files
committed
Access to temporary tables improvements.
Moved temp tables cleanup into ut_utils (authid definer), so that we can use truncate rather then delete. Refactored ut_coverage and ut_coverage_helper. Changed the RunAll script to use packaged version of mystats.
1 parent 55fe15f commit 9c19355

9 files changed

Lines changed: 91 additions & 61 deletions

File tree

source/api/ut_runner.pkb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ create or replace package body ut_runner is
4343
return ut_utils.gc_version;
4444
end;
4545

46-
procedure cleanup_temp_tables is
47-
pragma autonomous_transaction;
48-
begin
49-
delete from ut_cursor_data where 1 = 1;
50-
commit;
51-
end;
52-
5346
procedure run(
5447
a_paths ut_varchar2_list, a_reporters ut_reporters, a_color_console boolean := false,
5548
a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null,
@@ -78,11 +71,11 @@ create or replace package body ut_runner is
7871
);
7972
l_items_to_run.do_execute(l_listener);
8073

81-
cleanup_temp_tables;
74+
ut_utils.cleanup_temp_tables;
8275
ut_output_buffer.close(l_listener.reporters);
8376
exception
8477
when others then
85-
cleanup_temp_tables;
78+
ut_utils.cleanup_temp_tables;
8679
ut_output_buffer.close(l_listener.reporters);
8780
dbms_output.put_line(dbms_utility.format_error_backtrace);
8881
dbms_output.put_line(dbms_utility.format_error_stack);

source/core/coverage/ut_coverage.pkb

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ create or replace package body ut_coverage is
2828
-- The subquery is optimized by:
2929
-- - COALESCE function -> it will execute only for TRIGGERS
3030
-- - scalar subquery cache -> it will only execute once for one trigger source code.
31-
function populate_sources_tmp_table(a_coverage_options ut_coverage_options) return varchar2 is
31+
function get_populate_sources_tmp_sql(a_coverage_options ut_coverage_options) return varchar2 is
3232
l_result varchar2(32767);
3333
l_full_name varchar2(100);
3434
begin
@@ -98,17 +98,46 @@ create or replace package body ut_coverage is
9898
return l_result;
9999
end;
100100

101+
function is_tmp_table_populated return boolean is
102+
l_result integer;
103+
begin
104+
select 1 into l_result from ut_coverage_sources_tmp where rownum = 1;
105+
return (l_result = 1);
106+
exception
107+
when no_data_found then
108+
return false;
109+
end;
110+
111+
procedure populate_tmp_table(a_coverage_options ut_coverage_options, a_skipped_objects ut_object_names) is
112+
pragma autonomous_transaction;
113+
l_schema_names ut_varchar2_rows;
114+
begin
115+
delete from ut_coverage_sources_tmp;
116+
l_schema_names := coalesce(a_coverage_options.schema_names, ut_varchar2_rows(sys_context('USERENV','CURRENT_SCHEMA')));
117+
if a_coverage_options.file_mappings is not empty then
118+
execute immediate get_populate_sources_tmp_sql(a_coverage_options) using a_coverage_options.file_mappings, a_skipped_objects, a_coverage_options.include_objects;
119+
else
120+
execute immediate get_populate_sources_tmp_sql(a_coverage_options) using l_schema_names, a_skipped_objects, a_coverage_options.include_objects;
121+
end if;
122+
commit;
123+
end;
124+
125+
101126
/**
102127
* Public functions
103128
*/
104129
procedure coverage_start is
105130
begin
106-
ut_coverage_helper.coverage_start('utPLSQL Code coverage run '||ut_utils.to_string(systimestamp));
131+
if not ut_coverage_helper.is_started() then
132+
ut_coverage_helper.coverage_start('utPLSQL Code coverage run '||ut_utils.to_string(systimestamp));
133+
end if;
107134
end;
108135

109136
procedure coverage_start_develop is
110137
begin
111-
ut_coverage_helper.coverage_start_develop();
138+
if not ut_coverage_helper.is_started() then
139+
ut_coverage_helper.coverage_start_develop();
140+
end if;
112141
end;
113142

114143
procedure coverage_pause is
@@ -132,41 +161,24 @@ create or replace package body ut_coverage is
132161
end;
133162

134163
function get_coverage_data(a_coverage_options ut_coverage_options) return t_coverage is
135-
136-
pragma autonomous_transaction;
137-
138-
type t_coverage_row is record(
139-
name varchar2(500),
140-
line_number integer,
141-
total_occur number(38,0)
142-
);
143-
type tt_coverage_rows is table of t_coverage_row;
144164
l_line_calls ut_coverage_helper.unit_line_calls;
145165
l_result t_coverage;
146166
l_new_unit t_unit_coverage;
147167
l_skipped_objects ut_object_names := ut_object_names();
148168

149169
type t_source_lines is table of binary_integer;
150-
l_source_lines t_source_lines;
151170
line_no binary_integer;
152-
l_schema_names ut_varchar2_rows;
153-
l_query varchar2(32767);
154171
begin
155-
l_schema_names := coalesce(a_coverage_options.schema_names, ut_varchar2_rows(sys_context('USERENV','CURRENT_SCHEMA')));
156172

157173
if not ut_coverage_helper.is_develop_mode() then
158174
--skip all the utplsql framework objects and all the unit test packages that could potentially be reported by coverage.
159175
l_skipped_objects := ut_utils.get_utplsql_objects_list() multiset union all coalesce(a_coverage_options.exclude_objects, ut_object_names());
160176
end if;
161177

162178
--prepare global temp table with sources
163-
delete from ut_coverage_sources_tmp;
164-
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
165-
execute immediate populate_sources_tmp_table(a_coverage_options) using a_coverage_options.file_mappings, l_skipped_objects, a_coverage_options.include_objects;
166-
else
167-
execute immediate populate_sources_tmp_table(a_coverage_options) using l_schema_names, l_skipped_objects, a_coverage_options.include_objects;
179+
if ut_coverage_helper.is_develop_mode() or not is_tmp_table_populated() then
180+
populate_tmp_table(a_coverage_options, l_skipped_objects);
168181
end if;
169-
commit;
170182

171183
for src_object in (
172184
select o.owner, o.name, o.full_name, max(o.line) lines_count,

source/core/coverage/ut_coverage_helper.pkb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,41 @@ create or replace package body ut_coverage_helper is
1616
limitations under the License.
1717
*/
1818

19-
g_coverage_id integer;
20-
g_develop_mode boolean;
19+
g_coverage_id integer;
20+
g_develop_mode boolean := false;
21+
g_is_started boolean := false;
2122

22-
function is_develop_mode return boolean is
23+
function is_develop_mode return boolean is
2324
begin
2425
return g_develop_mode;
2526
end;
2627

28+
function is_started return boolean is
29+
begin
30+
return g_is_started;
31+
end;
32+
2733
procedure coverage_start_internal(a_run_comment varchar2) is
2834
begin
2935
dbms_profiler.start_profiler(run_comment => a_run_comment, run_number => g_coverage_id);
36+
g_is_started := true;
3037
coverage_pause();
3138
end;
3239

3340
procedure coverage_start(a_run_comment varchar2) is
3441
begin
35-
if g_develop_mode is null then
42+
if not g_is_started then
3643
g_develop_mode := false;
3744
coverage_start_internal(a_run_comment);
3845
end if;
3946
end;
4047

4148
procedure coverage_start_develop is
4249
begin
43-
g_develop_mode := true;
44-
coverage_start_internal('utPLSQL Code coverage run in development MODE '||ut_utils.to_string(systimestamp));
50+
if not g_is_started then
51+
g_develop_mode := true;
52+
coverage_start_internal('utPLSQL Code coverage run in development MODE '||ut_utils.to_string(systimestamp));
53+
end if;
4554
end;
4655

4756
procedure coverage_pause is
@@ -59,17 +68,18 @@ create or replace package body ut_coverage_helper is
5968
end;
6069

6170
procedure coverage_stop is
62-
l_return_code binary_integer;
6371
begin
6472
if not g_develop_mode then
65-
l_return_code := dbms_profiler.stop_profiler();
73+
g_is_started := false;
74+
dbms_profiler.stop_profiler();
6675
end if;
6776
end;
6877

6978
procedure coverage_stop_develop is
70-
l_return_code binary_integer;
7179
begin
72-
l_return_code := dbms_profiler.stop_profiler();
80+
g_develop_mode := false;
81+
g_is_started := false;
82+
dbms_profiler.stop_profiler();
7383
end;
7484

7585
function get_raw_coverage_data(a_object_owner varchar2, a_object_name varchar2) return unit_line_calls is

source/core/coverage/ut_coverage_helper.pks

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ create or replace package ut_coverage_helper authid definer is
1717
*/
1818

1919
--table of line calls indexed by line number
20-
-- table is sparse!!!
20+
--!!! this table is sparse!!!
2121
type unit_line_calls is table of number(38,0) index by binary_integer;
2222

2323
function is_develop_mode return boolean;
2424

25+
function is_started return boolean;
26+
2527
procedure coverage_start(a_run_comment varchar2);
2628

2729
/*

source/core/ut_utils.pkb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,5 +341,13 @@ create or replace package body ut_utils is
341341
return l_result;
342342
end;
343343

344+
procedure cleanup_temp_tables is
345+
pragma autonomous_transaction;
346+
begin
347+
execute immediate 'truncate table ut_cursor_data';
348+
execute immediate 'truncate table ut_coverage_sources_tmp$';
349+
commit;
350+
end;
351+
344352
end ut_utils;
345353
/

source/core/ut_utils.pks

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ create or replace package ut_utils authid definer is
7979
ex_invalid_rep_event_name exception;
8080
gc_invalid_rep_event_name constant pls_integer := -20211;
8181
pragma exception_init(ex_invalid_rep_event_name, -20211);
82-
82+
8383
-- Any of tests failed
8484
ex_some_tests_failed exception;
8585
gc_some_tests_failed constant pls_integer := -20213;
@@ -219,5 +219,7 @@ create or replace package ut_utils authid definer is
219219

220220
function convert_collection(a_collection ut_varchar2_list) return ut_varchar2_rows;
221221

222+
procedure cleanup_temp_tables;
223+
222224
end ut_utils;
223225
/

tests/RunAll.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
echo off
22
set UT3_OWNER=ut3
3-
set UT3_OWNER_PASSWORD=ut3
3+
set UT3_OWNER_PASSWORD=XNtxj8eEgA6X6b6f
44
set ORACLE_SID=XE
55
if not [%1] == [] (set UT3_OWNER=%1)
66
if not [%2] == [] (set UT3_OWNER_PASSWORD=%2)

tests/RunAll.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ set longchunksize 1000000
1010
set serveroutput on size unlimited format truncated
1111
@@lib/RunVars.sql
1212

13-
@@lib/mystats/mystats start
13+
@@lib/mystats/mystats_pkg.sql
14+
exec mystats_pkg.ms_start;
1415

1516
spool RunAll.log
1617

@@ -557,7 +558,9 @@ spool coverage.html
557558
exec ut_output_buffer.lines_to_dbms_output(:html_reporter_id);
558559
spool off
559560

560-
@@lib/mystats/mystats stop t=1000
561+
spool stats.log
562+
exec mystats_pkg.ms_stop(1000);
563+
spool off
561564

562565
--can be used by CI to check for tests status
563566
exit :failures_count

tests/lib/mystats/mystats_pkg.sql

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ create or replace package mystats_pkg authid current_user as
1313
||
1414
|| Author: Adrian Billington
1515
|| www.oracle-developer.net
16-
|| (c) oracle-developer.net
16+
|| (c) oracle-developer.net
1717
||
1818
|| Description: PL/SQL-only version of Jonathan Lewis's SNAP_MY_STATS package.
19-
|| This package is used to output the resource usage as recorded
19+
|| This package is used to output the resource usage as recorded
2020
|| in V$MYSTAT and V$LATCH.
2121
||
2222
|| Key Differences
@@ -50,7 +50,7 @@ create or replace package mystats_pkg authid current_user as
5050
|| exec mystats_pkg.ms_stop;
5151
||
5252
|| 2. Output statistics with delta values >= 1,000
53-
|| -------------------------------------------------------------
53+
|| -------------------------------------------------------------
5454
|| exec mystats_pkg.ms_start;
5555
|| --<do some work>--
5656
|| exec mystats_pkg.ms_stop(1000);
@@ -61,21 +61,21 @@ create or replace package mystats_pkg authid current_user as
6161
|| --<do some work>--
6262
|| exec mystats_pkg.ms_stop(mystats_pkg.statname_ntt('redo size', 'user commits'));
6363
||
64-
|| 4. Output statistics for those containing the word 'memory'
64+
|| 4. Output statistics for those containing the word 'memory'
6565
|| -----------------------------------------------------------
6666
|| exec mystats_pkg.ms_start;
6767
|| --<do some work>--
6868
|| exec mystats_pkg.ms_stop('memory');
6969
||
7070
|| Notes: 1. Serveroutput must be on (and set higher than default);
71-
||
71+
||
7272
|| 2. See http://www.jlcomp.demon.co.uk/snapshot.html for original
7373
|| version.
7474
||
75-
|| 3. A free-standing, SQL*Plus-script version of MyStats is also
75+
|| 3. A free-standing, SQL*Plus-script version of MyStats is also
7676
|| available. The script version works without creating any
7777
|| database objects.
78-
||
78+
||
7979
|| Disclaimer: http://www.oracle-developer.net/disclaimer.php
8080
||
8181
|| ----------------------------------------------------------------------------
@@ -183,7 +183,7 @@ create or replace package body mystats_pkg as
183183
union all
184184
select 'LATCH'
185185
, name
186-
, gets
186+
, gets
187187
from v$latch
188188
union all
189189
select 'TIME'
@@ -221,7 +221,7 @@ create or replace package body mystats_pkg as
221221

222222
/*
223223
|| Downside of using associative arrays is that we have to sort
224-
|| the output. So here's a couple of types and a variable to enable us
224+
|| the output. So here's a couple of types and a variable to enable us
225225
|| to do that...
226226
*/
227227
type aat_mystats_output is table of st_output
@@ -315,13 +315,13 @@ create or replace package body mystats_pkg as
315315
v_value := ga_mystats(c_run2)(v_name).value - ga_mystats(c_run1)(v_name).value;
316316

317317
/*
318-
|| If it's greater than the threshold or a statistic we are interested in,
318+
|| If it's greater than the threshold or a statistic we are interested in,
319319
|| then output it. The downside of using purely associative arrays is that
320320
|| we don't have any easy way of sorting. So we have to do it ourselves...
321321
*/
322322
if (p_threshold is not null and abs(v_value) >= p_threshold)
323323
or (p_statnames is not empty and v_name member of p_statnames)
324-
or (p_statname_like is not null and v_name like '%'||p_statname_like||'%')
324+
or (p_statname_like is not null and v_name like '%'||p_statname_like||'%')
325325
then
326326
/*
327327
|| Fix for bug 1713403. If redo goes over 2Gb then it is reported as a negative
@@ -334,7 +334,7 @@ create or replace package body mystats_pkg as
334334
sort(v_type, v_name, v_value);
335335
end if;
336336
end if;
337-
337+
338338
/*
339339
|| Next statname please...
340340
*/
@@ -402,7 +402,7 @@ create or replace package body mystats_pkg as
402402
then ms_report(p_statnames => p_statnames);
403403
when p_statname_like is not null
404404
then ms_report(p_statname_like => p_statname_like);
405-
else ms_report;
405+
else ms_report;
406406
end case;
407407
ms_reset;
408408
else
@@ -438,5 +438,5 @@ create or replace package body mystats_pkg as
438438
end mystats_pkg;
439439
/
440440

441-
create or replace public synonym mystats_pkg for mystats_pkg;
442-
grant execute on mystats_pkg to public;
441+
-- create or replace public synonym mystats_pkg for mystats_pkg;
442+
-- grant execute on mystats_pkg to public;

0 commit comments

Comments
 (0)