|
| 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 | +/ |
0 commit comments