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

Skip to content

Commit ca31e98

Browse files
committed
Added missing annotation manager.
Removed unused code from ut_meta_data. Improved code comments.
1 parent dbf2538 commit ca31e98

4 files changed

Lines changed: 271 additions & 76 deletions

File tree

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
create or replace package body ut_annotation_manager as
2+
/*
3+
utPLSQL - Version X.X.X.X
4+
Copyright 2016 - 2017 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
------------------------------
20+
--private definitions
21+
22+
function get_annotated_objects_cursor(a_object_owner varchar2, a_object_type varchar2, a_object_name varchar2) return sys_refcursor is
23+
l_result sys_refcursor;
24+
l_ut_owner varchar2(250) := ut_utils.ut_owner;
25+
l_objects_view varchar2(200) := ut_metadata.get_dba_view('dba_objects');
26+
l_cursor_text long;
27+
begin
28+
l_cursor_text :=
29+
q'[select ]'||l_ut_owner||q'[.ut_annotation_cached_object(
30+
object_owner => o.owner,
31+
object_name => o.object_name,
32+
object_type => o.object_type,
33+
needs_refresh => case when o.last_ddl_time < i.parse_time then 'N' else 'Y' end,
34+
cache_id => i.cache_id
35+
)
36+
from ]'||l_objects_view||q'[ o
37+
left join ut3.ut_annotation_cache_info i
38+
on o.owner = i.object_owner and o.object_name = i.object_name and o.object_type = i.object_type
39+
where o.owner = :a_object_owner
40+
and o.object_type = :a_object_type
41+
and o.status = 'VALID'
42+
and :a_object_name ]'|| case when a_object_name is not null then '= o.object_name' else 'is null' end;
43+
open l_result for l_cursor_text using a_object_owner, a_object_type, a_object_name;
44+
return l_result;
45+
end;
46+
47+
function get_sources_for_annotations(a_object_owner varchar2, a_object_type varchar2, a_object_name varchar2) return sys_refcursor is
48+
l_result sys_refcursor;
49+
l_sources_view varchar2(200) := ut_metadata.get_dba_view('dba_source');
50+
begin
51+
open l_result for
52+
q'[select s.name, s.text
53+
from ]'||l_sources_view||q'[ s
54+
where s.type = :a_object_type
55+
and s.owner = :a_object_owner
56+
and s.name
57+
in (select x.name
58+
from ]'||l_sources_view||q'[ x
59+
where x.type = :a_object_type
60+
and x.owner = :a_object_owner
61+
and x.text like '%--%\%%' escape '\'
62+
and :a_object_name ]'|| case when a_object_name is not null then '= x.name' else 'is null' end || q'[
63+
)
64+
order by name, line]'
65+
using a_object_type, a_object_owner, a_object_type, a_object_owner, a_object_name;
66+
67+
return l_result;
68+
end;
69+
70+
function get_sources_for_annotations(a_object_owner varchar2, a_object_type varchar2, a_objects_to_refresh ut_annotation_cached_objects) return sys_refcursor is
71+
l_result sys_refcursor;
72+
l_sources_view varchar2(200) := ut_metadata.get_dba_view('dba_source');
73+
l_card natural;
74+
begin
75+
l_card := ut_utils.scale_cardinality(cardinality(a_objects_to_refresh));
76+
open l_result for
77+
q'[select /*+ cardinality( r ]'||l_card||q'[ )*/
78+
s.name, s.text
79+
from table(:a_objects_to_refresh) r
80+
join ]'||l_sources_view||q'[ s
81+
on s.name = r.object_name
82+
where s.type = :a_object_type
83+
and s.owner = :a_object_owner
84+
and s.name
85+
in (select /*+ cardinality( x ]'||l_card||q'[ )*/
86+
x.name
87+
from table(:a_objects_to_refresh) t
88+
join ]'||l_sources_view||q'[ x
89+
on x.name = t.object_name
90+
where x.type = :a_object_type
91+
and x.owner = :a_object_owner
92+
and x.text like '%--%\%%' escape '\'
93+
)
94+
order by name, line]'
95+
using a_objects_to_refresh, a_object_type, a_object_owner, a_objects_to_refresh, a_object_type, a_object_owner;
96+
97+
return l_result;
98+
end;
99+
100+
------------------------------------------------------------
101+
--public definitions
102+
------------------------------------------------------------
103+
function get_annotated_objects(a_object_owner varchar2, a_object_type varchar2, a_object_name varchar2 := null) return ut_annotated_objects pipelined is
104+
l_info_rows ut_annotation_cached_objects;
105+
l_in_cache ut_annotation_cached_objects;
106+
l_to_parse ut_annotation_cached_objects := ut_annotation_cached_objects();
107+
l_cursor sys_refcursor;
108+
l_results ut_annotated_objects;
109+
l_result ut_annotated_object;
110+
c_object_fetch_limit constant integer := 10;
111+
c_lines_fetch_limit constant integer := 1000;
112+
l_lines dbms_preprocessor.source_lines_t;
113+
l_names dbms_preprocessor.source_lines_t;
114+
l_name varchar2(250) := '''';
115+
l_object_lines dbms_preprocessor.source_lines_t;
116+
begin
117+
--get information about cached objects
118+
l_cursor := get_annotated_objects_cursor(a_object_owner, a_object_type, a_object_name);
119+
fetch l_cursor bulk collect into l_info_rows;
120+
close l_cursor;
121+
122+
--get list of objects in cache
123+
select value(x) bulk collect into l_in_cache from table(l_info_rows) x where x.needs_refresh = 'N';
124+
125+
--if not all in cache, get list of objects to refresh
126+
if l_in_cache.count <= l_info_rows.count then
127+
select value(x) bulk collect into l_to_parse from table(l_info_rows) x where x.needs_refresh = 'Y';
128+
end if;
129+
130+
--pipe annotations from cache
131+
if l_in_cache.count > 0 then
132+
l_cursor := ut_annotation_cache_manager.get_annotations_for_objects(l_in_cache);
133+
loop
134+
fetch l_cursor bulk collect into l_results limit c_object_fetch_limit;
135+
for i in 1 .. l_results.count loop
136+
pipe row (l_results(i));
137+
end loop;
138+
exit when l_cursor%notfound;
139+
end loop;
140+
close l_cursor;
141+
end if;
142+
143+
--if some source needs parsing
144+
if l_to_parse.count > 0 then
145+
--do we need to parse all of sources
146+
if l_in_cache.count = 0 then
147+
l_cursor := get_sources_for_annotations(a_object_owner, a_object_type, a_object_name);
148+
else
149+
-- sources need to be filtered by objects to parse
150+
l_cursor := get_sources_for_annotations(a_object_owner, a_object_type, l_to_parse);
151+
end if;
152+
153+
--remove cached annotations data for objects that will be refreshed
154+
ut_annotation_cache_manager.cleanup_cache(l_to_parse);
155+
156+
loop
157+
fetch l_cursor bulk collect into l_names, l_lines limit c_lines_fetch_limit;
158+
159+
for i in 1 .. l_names.count loop
160+
161+
if l_names(i) != l_name then
162+
l_result := ut_annotated_object(
163+
a_object_owner, l_name, a_object_type,
164+
ut_annotation_parser.parse_object_annotations(l_object_lines)
165+
);
166+
ut_annotation_cache_manager.update_cache(l_result);
167+
if l_result.annotations.count > 0 then
168+
pipe row (l_result);
169+
end if;
170+
l_object_lines.delete;
171+
end if;
172+
173+
l_name := l_names(i);
174+
l_object_lines(l_object_lines.count+1) := l_lines(i);
175+
176+
end loop;
177+
exit when l_cursor%notfound;
178+
179+
end loop;
180+
181+
if l_name is not null then
182+
l_result := ut_annotated_object(
183+
a_object_owner, l_name, a_object_type,
184+
ut_annotation_parser.parse_object_annotations(l_object_lines)
185+
);
186+
ut_annotation_cache_manager.update_cache(l_result);
187+
if l_result.annotations.count > 0 then
188+
pipe row (l_result);
189+
end if;
190+
l_object_lines.delete;
191+
end if;
192+
193+
close l_cursor;
194+
end if;
195+
196+
end;
197+
198+
end ut_annotation_manager;
199+
/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
create or replace package ut_annotation_manager authid current_user as
2+
/*
3+
utPLSQL - Version X.X.X.X
4+
Copyright 2016 - 2017 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/**
20+
* Builds annotations out of database source code by reading it from cache
21+
*/
22+
23+
/**
24+
* Parses an object or all objects of a specified type for database schema.
25+
* Results are returned in as a pipelined function.
26+
* @param a_object_owner schema name to be parsed
27+
* @param a_object_type type of object to be parsed
28+
* @param a_object_name name of object to be parsed - optional
29+
* @return array containing annotated objects along with annotations for each object (nested)
30+
*/
31+
function get_annotated_objects(a_object_owner varchar2, a_object_type varchar2, a_object_name varchar2 := null) return ut_annotated_objects pipelined;
32+
33+
end ut_annotation_manager;
34+
/

source/core/ut_metadata.pkb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,6 @@ create or replace package body ut_metadata as
115115
return false;
116116
end;
117117

118-
function get_package_spec_source(a_owner varchar2, a_object_name varchar2) return clob is
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');
123-
begin
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, replace(l_lines(i), chr(13)||chr(10), chr(10)));
132-
end loop;
133-
return l_source;
134-
end;
135-
136118
function get_source_definition_line(a_owner varchar2, a_object_name varchar2, a_line_no integer) return varchar2 is
137119
l_cursor sys_refcursor;
138120
l_view_name varchar2(128) := get_dba_view('dba_source');
@@ -165,9 +147,9 @@ create or replace package body ut_metadata as
165147
g_cached_object := null;
166148
end;
167149

168-
function get_dba_view(a_view_name varchar2) return varchar2 is
150+
function get_dba_view(a_dba_view_name varchar2) return varchar2 is
169151
l_invalid_object_name exception;
170-
l_result varchar2(128) := lower(a_view_name);
152+
l_result varchar2(128) := lower(a_dba_view_name);
171153
pragma exception_init(l_invalid_object_name,-44002);
172154
begin
173155
l_result := dbms_assert.sql_object_name(l_result);

source/core/ut_metadata.pks

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,58 @@ create or replace package ut_metadata authid current_user as
1616
limitations under the License.
1717
*/
1818

19-
/*
20-
package: ut_metadata
21-
22-
Common place for all code that reads from the system tables.
23-
24-
*/
25-
26-
/*
27-
function: form_name
28-
29-
forms correct object/subprogram name to call as owner.object[.subprogram]
30-
31-
*/
19+
/**
20+
* Common package for all code that reads from the system tables.
21+
*/
22+
23+
/**
24+
* Forms correct object/subprogram name to call as owner.object[.subprogram]
25+
*
26+
*/
3227
function form_name(a_owner_name varchar2, a_object varchar2, a_subprogram varchar2 default null) return varchar2;
3328

34-
/*
35-
function: package_valid
36-
37-
check if package exists and is VALID.
38-
39-
*/
29+
/**
30+
* Check if package exists and is in a VALID state
31+
*
32+
*/
4033
function package_valid(a_owner_name varchar2, a_package_name in varchar2) return boolean;
4134

42-
/*
43-
function: procedure_exists
44-
45-
check if package exists and is VALID and contains the given procedure.
46-
47-
*/
35+
/**
36+
* Check if package exists and is VALID and contains the given procedure.
37+
*
38+
*/
4839
function procedure_exists(a_owner_name varchar2, a_package_name in varchar2, a_procedure_name in varchar2)
4940
return boolean;
5041

51-
/*
52-
procedure: do_resolve
53-
54-
resolves [owner.]object using dbms_utility.name_resolve and returnes resolved parts
55-
56-
*/
42+
/**
43+
* Resolves [owner.]object using dbms_utility.name_resolve and returns resolved parts
44+
*
45+
*/
5746
procedure do_resolve(a_owner in out nocopy varchar2, a_object in out nocopy varchar2);
5847

59-
/*
60-
procedure: do_resolve
61-
62-
resolves [owner.]object[.procedure] using dbms_utility.name_resolve and returnes resolved parts
63-
64-
*/
48+
/**
49+
* Resolves [owner.]object[.procedure] using dbms_utility.name_resolve and returns resolved parts
50+
*
51+
*/
6552
procedure do_resolve(a_owner in out nocopy varchar2, a_object in out nocopy varchar2, a_procedure_name in out nocopy varchar2);
6653

67-
/*
68-
function: get_package_spec_source
69-
70-
return the text of the package specification for a given package
71-
*/
72-
function get_package_spec_source(a_owner varchar2, a_object_name varchar2) return clob;
73-
74-
75-
/*
76-
function: get_source_definition_line
77-
78-
return the text of the source line for a given object, excludes package spec and type spec
79-
*/
54+
/**
55+
* Return the text of the source line for a given object (body). It excludes package spec and type spec
56+
*/
8057
function get_source_definition_line(a_owner varchar2, a_object_name varchar2, a_line_no integer) return varchar2;
8158

8259

60+
/**
61+
* Invalidates package-level cache for source.
62+
* Caching is used to improve performance of function get_source_definition_line
63+
*/
8364
procedure reset_source_definition_cache;
8465

85-
/*
86-
function: get_dba_view
87-
88-
return the dba_xxx view name if it is accessible or all_xxx view otherwise
89-
*/
90-
function get_dba_view(a_view_name varchar2) return varchar2;
66+
/**
67+
* Returns dba_... view name if it is accessible, otherwise it returns all_xxx view
68+
* @param a_dba_view_name the name of dba view requested
69+
*/
70+
function get_dba_view(a_dba_view_name varchar2) return varchar2;
9171

9272
end ut_metadata;
9373
/

0 commit comments

Comments
 (0)