-
Notifications
You must be signed in to change notification settings - Fork 6
/
apex-diff.sql
158 lines (130 loc) · 4.45 KB
/
apex-diff.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- This file generates a second file which will be used to create the JSON output.
set serveroutput on size 1000000
set feedback off
set verify off
set termout off
define APP_ID = '&1'
define SPOOL_FILENAME = '&2'
spool &SPOOL_FILENAME.
declare
l_sql clob;
l_sql_template clob;
l_app_id apex_applications.application_id%type := &APP_ID.;
type rec_apex_info is record(
apex_view_name apex_dictionary.apex_view_name%type,
cols varchar2(4000),
order_by varchar2(4000),
rn number,
cnt number
);
type tab_apex_info is table of rec_apex_info index by pls_integer;
l_apex_info tab_apex_info;
begin
-- Templates
l_sql_template :=
'cursor(select %COLUMNS%
from %APEX_VIEW_NAME%
where application_id = %APP_ID%
order by %ORDER_BY%) "%APEX_VIEW_NAME%"';
l_sql_template := replace(l_sql_template, '%APP_ID%', chr(38) || 'APP_ID.');
dbms_output.put_line('-- File genereted from apex-diff.sql');
dbms_output.put_line('-- DO NOT MODIFY THIS FILE');
dbms_output.put_line('');
dbms_output.put_line('set sqlformat json');
dbms_output.put_line('set feedback off');
dbms_output.put_line('set termout off');
dbms_output.put_line('set verify off'); -- Removes the old/new sub string
dbms_output.put_line('');
-- Variables (this is for the auto build file)
dbms_output.put_line('define APP_ID = ''' || chr(38) || '1''');
dbms_output.put_line('');
dbms_output.put_line('spool f' || chr(38) || 'APP_ID..json');
-- Creating parent select statement
dbms_output.put_line('select ');
-- Get all the APEX views, and their columns
select
ad.apex_view_name,
listagg(atc.column_name, ',') within group (order by adc.column_id) cols,
-- order by columns will use APEX dictionary recommended list (ignore LOBs)
listagg(
case
when atc.data_type like '%LOB' then null -- can't order by LOBs
else atc.column_name
end, ',')
within group (order by adc.column_id) order_by,
row_number() over (order by ad.apex_view_name) rn,
count(1) over () cnt
bulk collect into l_apex_info
from
apex_dictionary ad,
all_tab_columns atc,
(
-- For column ids (order by)
select apex_view_name, column_name, column_id
from apex_dictionary
where column_id != 0) adc
where 1=1
and ad.column_id = 0
-- APEX views
and ad.apex_view_name not like 'APEX_UI%' -- TODO mdsouza: exclude?
and ad.apex_view_name not like 'APEX_TEAM%'
and ad.apex_view_name not like 'APEX_WORKSPACE%'
and ad.apex_view_name not like 'APEX_WS%'
and ad.apex_view_name not in (
'APEX_APPLICATIONS',
'APEX_APPLICATION_GROUPS',
'APEX_THEMES',
-- These IR reports are for user level IRs
'APEX_APPLICATION_PAGE_IR_COMP',
'APEX_APPLICATION_PAGE_IR_COND',
'APEX_APPLICATION_PAGE_IR_GRPBY',
'APEX_APPLICATION_PAGE_IR_PIVOT',
'APEX_APPLICATION_PAGE_IR_PVAGG',
'APEX_APPLICATION_PAGE_IR_PVSRT',
'APEX_APPLICATION_PAGE_IR_RPT',
'APEX_APPLICATION_PAGE_IR_SUB'
)
and ad.apex_view_name != 'APEX_APPLICATION_TRANS_MAP' -- doesn't contain application_id
-- APEX dictionary columns
and atc.table_name = adc.apex_view_name(+)
and atc.column_name = adc.column_name(+)
-- Columns
and atc.table_name = upper(ad.apex_view_name)
and atc.owner = apex_application.g_flow_schema_owner
and atc.column_name not in (
'WORKSPACE',
'WORKSPACE_DISPLAY_NAME',
'APPLICATION_ID',
'APPLICATION_NAME',
'COMPONENT_SIGNATURE'
)
and atc.data_type not in ('BLOB')
group by ad.apex_view_name
order by rn;
for i in 1 .. l_apex_info.count loop
l_sql := replace(l_sql_template, '%COLUMNS%', l_apex_info(i).cols);
l_sql := replace(l_sql, '%APEX_VIEW_NAME%', l_apex_info(i).apex_view_name);
l_sql := replace(l_sql, '%ORDER_BY%', l_apex_info(i).order_by);
l_sql := lower(l_sql);
dbms_output.put_line(l_sql);
if l_apex_info(i).rn != l_apex_info(i).cnt then
dbms_output.put_line(','); -- Column/cursor delimeter
end if;
end loop; -- apex_dictionary
-- Closing main query
dbms_output.put_line('from dual;');
dbms_output.put_line('');
dbms_output.put_line('spool off');
dbms_output.put_line('set sqlformat default');
-- TODO mdsouza: make this an option
dbms_output.put_line('');
dbms_output.put_line('exit');
exception
when others then
raise;
end;
/
spool off
@&SPOOL_FILENAME. &APP_ID.
-- TODO mdsouza: add option to export APEX application as well (.sql file)
exit