-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfn_gen_inserts.sql
More file actions
378 lines (339 loc) · 16.3 KB
/
fn_gen_inserts.sql
File metadata and controls
378 lines (339 loc) · 16.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Example usage:
SPOOL 'C:\TEMP\SCRIPT.SQL'
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET LINESIZE 32767
SET DEFINE OFF
SET TRIM ON
SELECT FN_GEN_INSERTS('SELECT * FROM FRW_CONFIG', 'FRW_CONFIG') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM FRW_CONFIG_URL', 'FRW_CONFIG_URL') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM FRW_MODULI', 'FRW_MODULI') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM FRW_UTENTI', 'FRW_UTENTI') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM T_LANGUAGE', 'T_LANGUAGE') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM FRW_ESECUZIONE_COMANDI', 'FRW_ESECUZIONE_COMANDI') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM T_PROFILE', 'T_PROFILE') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM API_FEE_FEED', 'API_FEE_FEED') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM API_LNK_LINK', 'API_LNK_LINK') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM API_CHA_CHANNEL', 'API_CHA_CHANNEL') FROM DUAL;
SELECT FN_GEN_INSERTS('SELECT * FROM API_PHO_PHOTO', 'API_PHO_PHOTO') FROM DUAL;
SPOOL OFF
*/
CREATE OR REPLACE function fn_gen_inserts
(
p_sql clob,
p_new_table_name varchar2,
p_new_owner_name varchar2 default null
)
return clob
is
l_cur number;
NL varchar2(2) := chr(13)||chr(10);
l_sql clob := p_sql;
l_ret number;
l_col_cnt number;
l_rec_tab dbms_sql.desc_tab;
l_separator char(1) := '!';
l_clob clob;
l_clob_line clob;
l_clob_ins clob;
l_clob_all clob;
l_line clob := '-----------------------------------';
cons_date_frm varchar2(32) := 'DD.MM.YYYY HH24:MI:SS';
cons_timestamp_frm varchar2(32) := 'DD.MM.YYYY HH24:MI:SSXFF';
cons_timestamp_wtz_frm varchar2(32) := 'DD.MM.YYYY HH24:MI:SSXFF TZR';
cons_varchar2_code number := 1;
cons_nvarchar2_code number := 1;
cons_number_code number := 2;
cons_float_code number := 2;
cons_long_code number := 8;
cons_date_code number := 12;
cons_binary_float_code number := 100;
cons_binary_double_code number := 101;
cons_timestamp_code number := 180;
cons_timestamp_wtz_code number := 181;
cons_timestamp_lwtz_code number := 231;
cons_interval_ytm_code number := 182;
cons_interval_dts_code number := 183;
cons_raw_code number := 23;
cons_long_raw_code number := 24;
cons_rowid_code number := 11;
cons_urowid_code number := 208;
cons_char_code number := 96;
cons_nchar_code number := 96;
cons_clob_code number := 112;
cons_nclob_code number := 112;
cons_blob_code number := 113;
cons_bfile_code number := 114;
-------------------------------------
-- Supported types
-------------------------------------
l_varchar2_col varchar2(32767); --1
l_number_col number; --2
--l_long_col long; --8 - not supported
l_date_col date; --12
--l_raw_col raw(2000); --23 - not supported
l_rowid_col rowid; --69
l_char_col char(2000); --96
l_binary_float_col binary_float; --100
l_binary_double_col binary_double; --101
l_clob_col clob; --112
l_timestamp_col timestamp(9); --180
l_timestamp_wtz_col timestamp(9) with time zone; --181
l_interval_ytm_col interval year(9) to month; --182
l_interval_dts_col interval day(9) to second(2); --183
l_urowid_col urowid; --208
l_timestamp_wltz_col timestamp with local time zone; --231
--l_nchar_col nchar(2000); --96 the same as char
--l_nclob_col nclob; --112 the same as clob
--l_blob_col - not supported
--l_bfile_col - not supported
--l_long_raw_col - not supported
procedure print_rec(rec in dbms_sql.desc_rec) is
begin
l_clob_all := l_clob_all||NL||
'col_type = ' || rec.col_type||NL||
'col_maxlen = ' || rec.col_max_len||NL||
'col_name = ' || rec.col_name||NL||
'col_name_len = ' || rec.col_name_len||NL||
'col_schema_name = ' || rec.col_schema_name||NL||
'col_schema_name_len = ' || rec.col_schema_name_len||NL||
'col_precision = ' || rec.col_precision||NL||
'col_scale = ' || rec.col_scale||NL||
'col_null_ok = ';
if (rec.col_null_ok) then
l_clob_all := l_clob_all||'true'||NL;
else
l_clob_all := l_clob_all||'false'||NL;
end if;
end;
begin
---------------------------------------
-- INSERT - header generation
---------------------------------------
l_clob_all :=
'set define off'||NL||
'declare'||NL||
' type t_clob is table of clob index by binary_integer;'||NL||
' l_clob t_clob;'||NL||
' type t_varchar2 is table of varchar2(64) index by binary_integer;'||NL||
' l_varchar2 t_varchar2;'||NL||
'begin'||NL;
---------------------------------------
-- Introduction
---------------------------------------
-- l_clob_all := l_clob_all||l_line||NL||'Parsing query:'||NL||l_sql||NL;
---------------------------------------
-- Open parse cursor
---------------------------------------
l_cur := dbms_sql.open_cursor;
dbms_sql.parse(l_cur, l_sql, dbms_sql.native);
---------------------------------------
-- Describe columns
---------------------------------------
dbms_sql.describe_columns(l_cur, l_col_cnt, l_rec_tab);
/*
l_clob_all := l_clob_all||l_line||NL||'Describe columns:'||NL;
for i in 1..l_rec_tab.count
loop
print_rec(l_rec_tab(i));
end loop;
*/
l_clob_all := l_clob_all||NL||
' null;'||NL||
' -- start generation of records'||NL||
' '||l_line||NL;
---------------------------------------
-- Define columns
---------------------------------------
for i in 1..l_rec_tab.count
loop
if l_rec_tab(i).col_type = cons_varchar2_code then --varchar2
dbms_sql.define_column(l_cur, i, l_varchar2_col, l_rec_tab(i).col_max_len);
elsif l_rec_tab(i).col_type = cons_number_code then --number
dbms_sql.define_column(l_cur, i, l_number_col);
--elsif l_rec_tab(i).col_type = cons_long_code then --long
-- dbms_sql.define_column_long(l_cur, i);
elsif l_rec_tab(i).col_type = cons_date_code then --date
dbms_sql.define_column(l_cur, i, l_date_col);
elsif l_rec_tab(i).col_type = cons_binary_float_code then --binary_float
dbms_sql.define_column(l_cur, i, l_binary_float_col);
elsif l_rec_tab(i).col_type = cons_binary_double_code then --binary_double
dbms_sql.define_column(l_cur, i, l_binary_double_col);
-- elsif l_rec_tab(i).col_type = cons_raw_code then --raw
-- dbms_sql.define_column_raw(l_cur, i, l_raw_col, l_rec_tab(i).col_max_len);
elsif l_rec_tab(i).col_type = cons_rowid_code then --rowid
dbms_sql.define_column_rowid(l_cur, i, l_rowid_col);
elsif l_rec_tab(i).col_type = cons_char_code then --char
dbms_sql.define_column_char(l_cur, i, l_char_col, l_rec_tab(i).col_max_len);
elsif l_rec_tab(i).col_type = cons_clob_code then --clob
dbms_sql.define_column(l_cur, i, l_clob_col);
elsif l_rec_tab(i).col_type = cons_timestamp_code then --timestamp
dbms_sql.define_column(l_cur, i, l_timestamp_col);
elsif l_rec_tab(i).col_type = cons_timestamp_wtz_code then --timestamp with time zone
dbms_sql.define_column(l_cur, i, l_timestamp_wtz_col);
elsif l_rec_tab(i).col_type = cons_rowid_code then --urowid
dbms_sql.define_column(l_cur, i, l_urowid_col);
elsif l_rec_tab(i).col_type = cons_timestamp_lwtz_code then --timestamp with local time zone
dbms_sql.define_column(l_cur, i, l_timestamp_wltz_col);
elsif l_rec_tab(i).col_type = cons_interval_ytm_code then --interval year to month
dbms_sql.define_column(l_cur, i, l_interval_ytm_col);
elsif l_rec_tab(i).col_type = cons_interval_dts_code then --interval day to second
dbms_sql.define_column(l_cur, i, l_interval_dts_col);
elsif l_rec_tab(i).col_type = cons_urowid_code then --urowid
dbms_sql.define_column(l_cur, i, l_urowid_col);
else
raise_application_error(-20001, 'Column: '||l_rec_tab(i).col_name||NL||
'Type not supported: '||l_rec_tab(i).col_type);
--not supported
end if;
end loop;
---------------------------------------
-- Execute cursor
---------------------------------------
l_ret := dbms_sql.execute(l_cur);
---------------------------------------
-- Fetch rows
---------------------------------------
loop
l_ret := dbms_sql.fetch_rows(l_cur);
exit when l_ret = 0;
---------------------------------------
-- Building INSERT - build column declarations
---------------------------------------
l_clob_line := '';
for i in 1..l_rec_tab.count
loop
if l_rec_tab(i).col_type = cons_varchar2_code then --varchar2
dbms_sql.column_value(l_cur, i, l_varchar2_col);
l_clob := l_varchar2_col;
elsif l_rec_tab(i).col_type = cons_number_code then --number
dbms_sql.column_value(l_cur, i, l_number_col);
l_clob := to_char(l_number_col);
-- elsif l_rec_tab(i).col_type = cons_long_code then --long
-- dbms_sql.column_value(l_cur, i, l_long_col);
-- l_clob := l_long_col;
elsif l_rec_tab(i).col_type = cons_date_code then --date
dbms_sql.column_value(l_cur, i, l_date_col);
l_clob := to_char(l_date_col, cons_date_frm);
elsif l_rec_tab(i).col_type = cons_binary_float_code then --binary_float
dbms_sql.column_value(l_cur, i, l_binary_float_col);
l_clob := to_char(l_binary_float_col);
elsif l_rec_tab(i).col_type = cons_binary_double_code then --binary_double
dbms_sql.column_value(l_cur, i, l_binary_double_col);
l_clob := to_char(l_binary_double_col);
-- elsif l_rec_tab(i).col_type = cons_raw_code then --raw
-- dbms_sql.column_value(l_cur, i, l_raw_col);
-- l_clob := to_char(l_raw_col);
elsif l_rec_tab(i).col_type = cons_rowid_code then --rowid
dbms_sql.column_value(l_cur, i, l_rowid_col);
l_clob := to_char(l_rowid_col);
elsif l_rec_tab(i).col_type = cons_char_code then --char
dbms_sql.column_value_char(l_cur, i, l_char_col);
l_clob := substr(l_char_col, 1, l_rec_tab(i).col_max_len - 1);
elsif l_rec_tab(i).col_type = cons_clob_code then --clob
dbms_sql.column_value(l_cur, i, l_clob_col);
l_clob := l_clob_col;
elsif l_rec_tab(i).col_type = cons_timestamp_code then --timestamp
dbms_sql.column_value(l_cur, i, l_timestamp_col);
l_clob := to_char(l_timestamp_col, cons_timestamp_frm);
elsif l_rec_tab(i).col_type = cons_timestamp_wtz_code then --timestamp with time zone
dbms_sql.column_value(l_cur, i, l_timestamp_wtz_col);
l_clob := to_char(l_timestamp_wtz_col, cons_timestamp_wtz_frm);
elsif l_rec_tab(i).col_type = cons_interval_ytm_code then --interval year to month
dbms_sql.column_value(l_cur, i, l_interval_ytm_col);
l_clob := to_char(l_interval_ytm_col);
elsif l_rec_tab(i).col_type = cons_interval_dts_code then --interval day to second
dbms_sql.column_value(l_cur, i, l_interval_dts_col);
l_clob := to_char(l_interval_dts_col);
elsif l_rec_tab(i).col_type = cons_urowid_code then --urowid
dbms_sql.column_value(l_cur, i, l_urowid_col);
l_clob := to_char(l_urowid_col);
elsif l_rec_tab(i).col_type = cons_timestamp_lwtz_code then --timestamp with local time zone
dbms_sql.column_value(l_cur, i, l_timestamp_wltz_col);
l_clob := to_char(l_timestamp_wltz_col, cons_timestamp_wtz_frm);
end if;
if l_rec_tab(i).col_type in (cons_clob_code, cons_char_code, cons_varchar2_code) then
l_clob_line := l_clob_line||' l_clob('||to_char(i)||') :=q'''||l_separator||l_clob||l_separator||''';'||NL;
else
l_clob_line := l_clob_line||' l_varchar2('||to_char(i)||') :=q'''||l_separator||l_clob||l_separator||''';'||NL;
end if;
end loop;
l_clob_all := l_clob_all||NL||l_clob_line;
---------------------------------------
-- Building INSERT - build column list
---------------------------------------
if p_new_owner_name is null then
l_clob_all := l_clob_all||chr(13)||NL||
' insert into '||p_new_table_name||NL||
' ('||NL;
else
l_clob_all := l_clob_all||chr(13)||NL||
' insert into '||p_new_owner_name||'.'||p_new_table_name||NL||
' ('||NL;
end if;
for i in 1..l_rec_tab.count
loop
if i = 1 then
l_clob_all := l_clob_all||' '||'"'||l_rec_tab(i).col_name||'"'||NL;
else
l_clob_all := l_clob_all||' ,'||'"'||l_rec_tab(i).col_name||'"'||NL;
end if;
end loop;
l_clob_all := l_clob_all||
' )'||NL||
' values'||NL||
' ('||NL;
---------------------------------------
-- Building INSERT - build values
---------------------------------------
for i in 1..l_rec_tab.count
loop
if i!=1 then
l_clob_all := l_clob_all||' ,';
else
l_clob_all := l_clob_all||' ';
end if;
if l_rec_tab(i).col_type = cons_number_code then --number
l_clob_all := l_clob_all||'to_number(l_varchar2('||to_char(i)||'))'||NL;
-- elsif l_rec_tab(i).col_type = cons_long_code then --long
-- l_clob := l_long_col;
elsif l_rec_tab(i).col_type = cons_clob_code then --clob
l_clob_all := l_clob_all||'l_clob('||to_char(i)||')'||NL;
elsif l_rec_tab(i).col_type = cons_char_code then --timestamp with local time zone
l_clob_all := l_clob_all||'to_char(l_clob('||to_char(i)||'))'||NL;
elsif l_rec_tab(i).col_type = cons_varchar2_code then --timestamp with local time zone
l_clob_all := l_clob_all||'to_char(l_clob('||to_char(i)||'))'||NL;
elsif l_rec_tab(i).col_type = cons_date_code then --date
l_clob_all := l_clob_all||'to_date(l_varchar2('||to_char(i)||'),'''||cons_date_frm||''')'||NL;
elsif l_rec_tab(i).col_type = cons_timestamp_code then --timestamp
l_clob_all := l_clob_all||'to_timestamp(l_varchar2('||to_char(i)||'),'''||cons_timestamp_frm||''')'||NL;
elsif l_rec_tab(i).col_type = cons_timestamp_wtz_code then --timestamp with time zone
l_clob_all := l_clob_all||'to_timestamp_tz(l_varchar2('||to_char(i)||'),'''||cons_timestamp_wtz_frm||''')'||NL;
elsif l_rec_tab(i).col_type = cons_interval_ytm_code then --interval year to month
l_clob_all := l_clob_all||'to_yminterval(l_varchar2('||to_char(i)||'))'||NL;
elsif l_rec_tab(i).col_type = cons_interval_dts_code then --interval day to second
l_clob_all := l_clob_all||'to_dsinterval(l_varchar2('||to_char(i)||'))'||NL;
elsif l_rec_tab(i).col_type = cons_timestamp_lwtz_code then --timestamp with local time zone
l_clob_all := l_clob_all||'to_timestamp_tz(l_varchar2('||to_char(i)||'),'''||cons_timestamp_wtz_frm||''')'||NL;
else
l_clob_all := l_clob_all||'l_varchar2('||to_char(i)||')'||NL;
end if;
end loop;
l_clob_all := l_clob_all||' );'||NL;
end loop;
---------------------------------------
-- Building INSERT - end of code
---------------------------------------
l_clob_all := l_clob_all||NL;
l_clob_all := l_clob_all||'end;'||NL;
-- l_clob_all := l_clob_all||'x';
---------------------------------------
-- Close cursor
---------------------------------------
dbms_sql.close_cursor(l_cur);
l_clob_all := l_clob_all||'/'||NL;
return trim(l_clob_all);
end;
/