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

Skip to content

Commit e099d40

Browse files
authored
Merge pull request #431 from utPLSQL/feature/dba_views_if_accessible
Feature/dba views if accessible
2 parents f510231 + f1ee7ad commit e099d40

4 files changed

Lines changed: 91 additions & 49 deletions

File tree

source/core/coverage/ut_coverage.pkb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ create or replace package body ut_coverage is
3131
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);
34+
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_source');
3435
begin
3536
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
3637
l_full_name := 'f.file_name';
@@ -48,7 +49,7 @@ create or replace package body ut_coverage is
4849
coalesce(
4950
case when type!='TRIGGER' then 0 end,
5051
(select min(t.line) - 1
51-
from all_source t
52+
from ]'||l_view_name||q'[ t
5253
where t.owner = s.owner and t.type = s.type and t.name = s.name
5354
and regexp_like( t.text, '[A-Za-z0-9$#_]*(begin|declare|compound).*','i'))
5455
) as line,
@@ -69,7 +70,7 @@ create or replace package body ut_coverage is
6970
)
7071
then 'Y'
7172
end as to_be_skipped
72-
from all_source s]';
73+
from ]'||l_view_name||q'[ s]';
7374
if a_coverage_options.file_mappings is not null and a_coverage_options.file_mappings.count > 0 then
7475
l_result := l_result || '
7576
join table(:file_mappings) f

source/core/ut_metadata.pkb

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ create or replace package body ut_metadata as
1616
limitations under the License.
1717
*/
1818

19+
type t_cache is table of all_source.text%type;
20+
g_source_cache t_cache;
21+
g_cached_object varchar2(500);
1922
------------------------------
2023
--public definitions
2124

@@ -63,19 +66,20 @@ create or replace package body ut_metadata as
6366
l_schema varchar2(200);
6467
l_package_name varchar2(200);
6568
l_procedure_name varchar2(200);
69+
l_view_name varchar2(200) := get_dba_view('dba_objects');
6670
begin
6771

6872
l_schema := a_owner_name;
6973
l_package_name := a_package_name;
7074

7175
do_resolve(l_schema, l_package_name, l_procedure_name);
7276

73-
select count(decode(status, 'VALID', 1, null)) / count(*)
74-
into l_cnt
75-
from all_objects
76-
where owner = l_schema
77-
and object_name = l_package_name
78-
and object_type in ('PACKAGE');
77+
execute immediate q'[select count(decode(status, 'VALID', 1, null)) / count(*)
78+
from ]'||l_view_name||q'[
79+
where owner = :l_schema
80+
and object_name = :l_package_name
81+
and object_type in ('PACKAGE')]'
82+
into l_cnt using l_schema, l_package_name;
7983

8084
-- expect both package and body to be valid
8185
return l_cnt = 1;
@@ -90,6 +94,7 @@ create or replace package body ut_metadata as
9094
l_schema varchar2(200);
9195
l_package_name varchar2(200);
9296
l_procedure_name varchar2(200);
97+
l_view_name varchar2(200) := get_dba_view('dba_procedures');
9398
begin
9499

95100
l_schema := a_owner_name;
@@ -98,12 +103,10 @@ create or replace package body ut_metadata as
98103

99104
do_resolve(l_schema, l_package_name, l_procedure_name);
100105

101-
select count(*)
102-
into l_cnt
103-
from all_procedures
104-
where owner = l_schema
105-
and object_name = l_package_name
106-
and procedure_name = l_procedure_name;
106+
execute immediate
107+
'select count(*) from '||l_view_name
108+
||' where owner = :l_schema and object_name = :l_package_name and procedure_name = :l_procedure_name'
109+
into l_cnt using l_schema, l_package_name, l_procedure_name;
107110

108111
--expect one method only for the package with that name.
109112
return l_cnt = 1;
@@ -113,38 +116,59 @@ create or replace package body ut_metadata as
113116
end;
114117

115118
function get_package_spec_source(a_owner varchar2, a_object_name varchar2) return clob is
116-
l_lines sys.dbms_preprocessor.source_lines_t;
117-
l_source clob;
119+
l_lines sys.dbms_preprocessor.source_lines_t;
120+
l_cursor sys_refcursor;
121+
l_source clob;
122+
l_view_name varchar2(128) := get_dba_view('dba_source');
118123
begin
119-
begin
120-
l_lines := sys.dbms_preprocessor.get_post_processed_source(object_type => 'PACKAGE',
121-
schema_name => a_owner,
122-
object_name => a_object_name);
123-
124-
for i in 1..l_lines.count loop
125-
ut_utils.append_to_clob(l_source, l_lines(i));
126-
end loop;
127-
128-
end;
124+
open l_cursor for 'select text from '||l_view_name||q'[ s
125+
where s.owner = :a_owner and s.name = :a_object_name and s.type = 'PACKAGE'
126+
order by s.line]' using upper(a_owner), upper(a_object_name);
127+
fetch l_cursor bulk collect into l_lines;
128+
-- we fetch the source explicitly as dbms_preprocessor is very sow on 12.1 and 12.2 when grabbing the sources.
129+
l_lines := sys.dbms_preprocessor.get_post_processed_source(l_lines);
130+
for i in 1..l_lines.count loop
131+
ut_utils.append_to_clob(l_source, l_lines(i));
132+
end loop;
129133
return l_source;
130134
end;
131135

132136
function get_source_definition_line(a_owner varchar2, a_object_name varchar2, a_line_no integer) return varchar2 is
133-
l_line varchar2(4000);
134137
l_cursor sys_refcursor;
138+
l_view_name varchar2(128) := get_dba_view('dba_source');
139+
l_line all_source.text%type;
140+
c_key constant varchar2(500) := a_owner || '.' || a_object_name;
135141
begin
136-
open l_cursor for
137-
select text from all_source s
138-
where s.owner = a_owner and s.name = a_object_name and s.line = a_line_no
139-
-- skip the declarations, consider only definitions
140-
and s.type not in ('PACKAGE','TYPE');
141-
fetch l_cursor into l_line;
142-
close l_cursor;
143-
return ltrim(rtrim( l_line, chr(10) ));
144-
exception
145-
when no_data_found then
146-
return null;
142+
if not nvl(c_key = g_cached_object, false) then
143+
g_cached_object := c_key;
144+
execute immediate
145+
'select trim(text) text
146+
from '||l_view_name||q'[ s
147+
where s.owner = :a_owner
148+
and s.name = :a_object_name
149+
/*skip the declarations, consider only definitions*/
150+
and s.type not in ('PACKAGE', 'TYPE')
151+
order by line]'
152+
bulk collect into g_source_cache
153+
using a_owner, a_object_name;
154+
end if;
155+
156+
if g_source_cache.exists(a_line_no) then
157+
l_line := g_source_cache(a_line_no);
158+
end if;
159+
return l_line;
147160
end;
148161

162+
function get_dba_view(a_view_name varchar2) return varchar2 is
163+
l_invalid_object_name exception;
164+
l_result varchar2(128) := lower(a_view_name);
165+
pragma exception_init(l_invalid_object_name,-44002);
166+
begin
167+
l_result := dbms_assert.sql_object_name(l_result);
168+
return l_result;
169+
exception
170+
when l_invalid_object_name then
171+
return replace(l_result,'dba_','all_');
172+
end;
149173
end;
150174
/

source/core/ut_metadata.pks

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ create or replace package ut_metadata authid current_user as
7979
*/
8080
function get_source_definition_line(a_owner varchar2, a_object_name varchar2, a_line_no integer) return varchar2;
8181

82+
/*
83+
function: get_dba_view
84+
85+
return the dba_xxx view name if it is accessible or all_xxx view otherwise
86+
*/
87+
function get_dba_view(a_view_name varchar2) return varchar2;
88+
8289
end ut_metadata;
8390
/

source/core/ut_suite_manager.pkb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ create or replace package body ut_suite_manager is
3434

3535
function get_schema_info(a_owner_name varchar2) return t_schema_info is
3636
l_info t_schema_info;
37+
l_view_name varchar2(200) := ut_metadata.get_dba_view('all_objects');
3738
begin
39+
execute immediate q'[
3840
select nvl(max(t.last_ddl_time), date '4999-12-31'), count(*)
39-
into l_info
40-
from all_objects t
41-
where t.owner = a_owner_name
42-
and t.object_type in ('PACKAGE');
41+
from ]'||l_view_name||q'[ t
42+
where t.owner = :a_owner_name
43+
and t.object_type in ('PACKAGE')]'
44+
into l_info using a_owner_name;
4345
return l_info;
4446
end;
4547

@@ -206,6 +208,15 @@ create or replace package body ut_suite_manager is
206208
l_root varchar2(4000 char);
207209
l_root_suite ut_logical_suite;
208210

211+
type t_object_name is record(
212+
owner all_objects.owner%type,
213+
object_name all_objects.object_name%type
214+
);
215+
type t_object_names is table of t_object_name;
216+
217+
l_object_names t_object_names;
218+
l_view_name varchar2(200) := ut_metadata.get_dba_view('dba_objects');
219+
209220
l_schema_suites tt_schema_suites;
210221

211222
procedure put(a_root_suite in out nocopy ut_logical_suite, a_path varchar2, a_suite ut_logical_suite, a_parent_path varchar2 default null) is
@@ -270,14 +281,13 @@ create or replace package body ut_suite_manager is
270281

271282
begin
272283
-- form the single-dimension list of suites constructed from parsed packages
273-
for rec in (select t.owner
274-
,t.object_name
275-
from all_objects t
276-
where t.owner = a_owner_name
277-
and t.status = 'VALID' -- scan only valid specifications
278-
and t.object_type in ('PACKAGE')) loop
284+
execute immediate
285+
'select t.owner, t.object_name from '||l_view_name||' t '
286+
||q'[ where t.owner = :a_owner_name and t.status = 'VALID' and t.object_type in ('PACKAGE')]'
287+
bulk collect into l_object_names using a_owner_name;
288+
for i in 1 .. cardinality(l_object_names) loop
279289
-- parse the source of the package
280-
l_suite := config_package(rec.owner, rec.object_name);
290+
l_suite := config_package(l_object_names(i).owner, l_object_names(i).object_name);
281291

282292
if l_suite is not null then
283293
l_all_suites(l_suite.path) := l_suite;

0 commit comments

Comments
 (0)