-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathddl_generator.sql
603 lines (502 loc) · 20.1 KB
/
ddl_generator.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
declare
a clob;
b DDL_GENERATOR.object_collection;
l_idx number;
begin
dbms_output.enable(200000);
DDL_GENERATOR.CLEAN;
--DDL_GENERATOR.GEN_SEQUENCE();
--DDL_GENERATOR.GEN_TABLE('AGE_BOX_EVENTI');
--DDL_GENERATOR.GEN_INDEX();
--DDL_GENERATOR.GEN_TRIGGER();
--DDL_GENERATOR.GEN_FOREIGN_KEY();
--DDL_GENERATOR.GEN_FUNCTION();
DDL_GENERATOR.GEN_PROCEDURE('UNIXX_GOL_INSERT');
--DDL_GENERATOR.GEN_VIEW();
--DDL_GENERATOR.GEN_PACKAGE();
b := DDL_GENERATOR.FETCH_RESULTS;
l_idx := b.FIRST;
WHILE l_idx IS NOT NULL LOOP
dbms_output.put_line (to_char(l_idx));
--DBMS_OUTPUT.PUT_LINE(b(l_idx).object_name);
--DBMS_OUTPUT.PUT_LINE(b(l_idx).source_code);
DDL_GENERATOR.PRINT_CLOB(b(l_idx).source_code);
l_idx := b.NEXT(l_idx);
END LOOP;
end;
/
CREATE OR REPLACE PACKAGE DDL_GENERATOR
IS
TYPE object_attribute IS RECORD (
source_code CLOB,
object_name VARCHAR2(30)
);
--TYPE object_collection IS TABLE OF CLOB INDEX BY BINARY_INTEGER;
TYPE object_collection IS TABLE OF object_attribute INDEX BY BINARY_INTEGER;
oc object_collection;
indice number(30);
--procedure help;
--function get_tables (p_TABLE_NAME IN VARCHAR2) RETURN CLOB;
PROCEDURE gen_sequence (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_table (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_index (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_trigger (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_foreign_key (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_function (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_procedure (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_package (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE gen_view (p_object_name VARCHAR2 DEFAULT NULL);
PROCEDURE print_clob(p_clob IN CLOB);
PROCEDURE clean;
FUNCTION fetch_results RETURN object_collection;
END DDL_GENERATOR;
/
CREATE OR REPLACE PACKAGE BODY DDL_GENERATOR
IS
-- http://www.oraclebytes.com/reference/packages/view/DBMS_METADATA/open-%28f%29
/*
procedure help
is
l_help varchar2(1000);
begin
l_help := '
Procedura per l''invio di Notifiche push - APEX-net srl
rel.1.0 - S. Teodorani
-------------------------------------------------------------
';
dbms_output.put_line(l_help);
end;
*/
procedure print_clob( p_clob in clob ) is
v_offset number default 1;
v_chunk_size number := 10000;
begin
loop
exit when v_offset > dbms_lob.getlength(p_clob);
dbms_output.put( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
--dbms_output.put_line('XX');
v_offset := v_offset + v_chunk_size;
end loop;
end;
PROCEDURE clean
IS
BEGIN
indice := 1;
oc.delete;
END;
FUNCTION fetch_results
RETURN object_collection
IS
BEGIN
RETURN oc;
END;
FUNCTION get_ddl_sequence (object_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('SEQUENCE', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', object_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
--dbms_metadata.set_transform_param(v_transhandle,'SEGMENT_ATTRIBUTES',false);
--dbms_metadata.set_transform_param(v_transhandle,'PRETTY',true);
--dbms_metadata.set_transform_param(v_transhandle,'REF_CONSTRAINTS',false);
--dbms_metadata.set_transform_param(v_transhandle,'TABLESPACE',false);
--dbms_metadata.set_transform_param(v_transhandle,'CONSTRAINTS',false);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_foreign_keys (object_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('REF_CONSTRAINT', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', object_name);
/*
DBMS_METADATA.SET_FILTER(v_handle, 'NAME_EXPR',
'IN (SELECT constraint_name ' ||
' FROM user_constraints ' ||
' WHERE constraint_type = ''R'' ' ||
' AND table_name = ''' || object_name || ''')');
*/
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
--dbms_metadata.set_transform_param(v_transhandle,'SEGMENT_ATTRIBUTES',false);
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
DBMS_METADATA.set_transform_param (v_transhandle,
'REF_CONSTRAINTS',
TRUE);
-- dbms_metadata.set_transform_param(v_transhandle,'TABLESPACE',false);
--dbms_metadata.set_transform_param(v_transhandle,'CONSTRAINTS',false);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_triggers (object_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('TRIGGER', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', object_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
--dbms_metadata.set_transform_param(v_transhandle,'SEGMENT_ATTRIBUTES',false);
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
--dbms_metadata.set_transform_param(v_transhandle,'REF_CONSTRAINTS',false);
-- dbms_metadata.set_transform_param(v_transhandle,'TABLESPACE',false);
--dbms_metadata.set_transform_param(v_transhandle,'CONSTRAINTS',false);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_indexes (object_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('INDEX', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', object_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
DBMS_METADATA.set_transform_param (v_transhandle,
'SEGMENT_ATTRIBUTES',
FALSE);
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
--dbms_metadata.set_transform_param(v_transhandle,'REF_CONSTRAINTS',false);
DBMS_METADATA.set_transform_param (v_transhandle, 'TABLESPACE', FALSE);
--dbms_metadata.set_transform_param(v_transhandle,'CONSTRAINTS',false);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_table (table_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('TABLE', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', table_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
DBMS_METADATA.set_transform_param (v_transhandle,
'SEGMENT_ATTRIBUTES',
FALSE);
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
DBMS_METADATA.set_transform_param (v_transhandle,
'REF_CONSTRAINTS',
FALSE);
DBMS_METADATA.set_transform_param (v_transhandle, 'TABLESPACE', FALSE);
--dbms_metadata.set_transform_param(v_transhandle,'CONSTRAINTS',false);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_code_object (obj_type varchar2,
object_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open (obj_type, version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'NAME', object_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle, 'REMAP_SCHEMA', schema, new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
DBMS_METADATA.set_transform_param (v_transhandle, 'SQLTERMINATOR', TRUE);
/*
v_ddl := '--------------------------------------------------------';
v_ddl := v_ddl || '-- DDL for '|| obj_type || ' ' || object_name;
v_ddl := v_ddl || '--------------------------------------------------------';
*/
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_table_comment (table_name VARCHAR2,
schema VARCHAR2 DEFAULT USER,
new_owner VARCHAR2 DEFAULT NULL)
RETURN CLOB
IS
v_handle NUMBER;
v_transhandle NUMBER;
v_ddl CLOB;
BEGIN
v_handle := DBMS_METADATA.open ('COMMENT', version => '9.0.1');
DBMS_METADATA.set_filter (v_handle, 'BASE_OBJECT_SCHEMA', schema);
DBMS_METADATA.set_filter (v_handle, 'BASE_OBJECT_NAME', table_name);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'MODIFY');
DBMS_METADATA.set_remap_param (v_transhandle,
'REMAP_SCHEMA',
schema,
new_owner);
v_transhandle := DBMS_METADATA.add_transform (v_handle, 'DDL');
DBMS_METADATA.set_transform_param (v_transhandle, 'PRETTY', TRUE);
DBMS_METADATA.set_transform_param (v_transhandle,
'SQLTERMINATOR',
TRUE);
v_ddl := DBMS_METADATA.fetch_clob (v_handle);
-- resolve bug oracle
v_ddl := replace(v_ddl, 'COMMENT ON COLUMN .', 'COMMENT ON COLUMN ');
--v_ddl := TRIM (v_ddl) || CHR (13) || CHR (10);
DBMS_METADATA.close (v_handle);
RETURN v_ddl;
END;
FUNCTION get_ddl_table_comment_old (p_table_name VARCHAR2)
RETURN CLOB
IS
v_ddl CLOB := NULL;
BEGIN
BEGIN
DBMS_METADATA.SET_TRANSFORM_PARAM (DBMS_METADATA.SESSION_TRANSFORM,
'PRETTY',
TRUE);
DBMS_METADATA.SET_TRANSFORM_PARAM (DBMS_METADATA.SESSION_TRANSFORM,
'SQLTERMINATOR',
TRUE);
FOR comm
IN (SELECT DBMS_METADATA.get_dependent_ddl ('COMMENT',
p_table_name,
USER)
AS ddl_statement
FROM DUAL)
LOOP
v_ddl := v_ddl || comm.ddl_statement;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
NULL;
v_ddl := TO_CLOB (NULL);
END;
RETURN v_ddl;
END;
PROCEDURE gen_sequence (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT sequence_name
FROM user_sequences
WHERE (sequence_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_sequence (a.sequence_name);
oc (indice).object_name := a.sequence_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_table (p_object_name VARCHAR2)
IS
last_ddl CLOB;
last_comment CLOB;
BEGIN
FOR a IN (SELECT table_name
FROM user_tables
WHERE (table_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_table (a.table_name);
oc (indice).object_name := a.table_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
last_comment := get_ddl_table_comment (a.table_name);
oc (indice).object_name := a.table_name;
oc (indice).source_code := last_comment;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_foreign_key (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT constraint_name AS object_name
FROM user_constraints
WHERE constraint_type = 'R'
AND ( constraint_name = p_object_name
OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_foreign_keys (a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_index (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT index_name AS object_name
FROM USER_INDEXES
WHERE index_type != 'LOB'
AND (index_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_indexes (a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_trigger (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT trigger_name AS object_name
FROM USER_TRIGGERS
WHERE (trigger_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_triggers (a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_function (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT object_name AS object_name
FROM USER_OBJECTS
WHERE object_type = 'FUNCTION' and (object_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_code_object ('FUNCTION', a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_procedure (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT object_name AS object_name
FROM USER_OBJECTS
WHERE object_type = 'PROCEDURE' and (object_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_code_object ('PROCEDURE', a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_view (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT object_name AS object_name
FROM USER_OBJECTS
WHERE object_type = 'VIEW' and (object_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_code_object ('VIEW', a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
PROCEDURE gen_package (p_object_name VARCHAR2)
IS
last_ddl CLOB;
BEGIN
FOR a
IN (SELECT object_name AS object_name
FROM USER_OBJECTS
WHERE object_type = 'PACKAGE' and (object_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_code_object ('PACKAGE_SPEC', a.object_name);
oc (indice).object_name := a.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
FOR b
IN (SELECT object_name AS object_name
FROM USER_OBJECTS
WHERE object_type = 'PACKAGE BODY' and (object_name = p_object_name OR p_object_name IS NULL))
LOOP
last_ddl := get_ddl_code_object ('PACKAGE_BODY', b.object_name);
oc (indice).object_name := b.object_name;
oc (indice).source_code := last_ddl;
indice := indice + 1;
END LOOP;
END;
END DDL_GENERATOR;
/