-
Notifications
You must be signed in to change notification settings - Fork 1
/
Install_DDMGR.sql
657 lines (536 loc) · 32.9 KB
/
Install_DDMGR.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/************************************************************
Author : Ferenc Toth
Remark : Data Dictionary Manager
Date : 2015.07.01
************************************************************/
Prompt *****************************************************************
Prompt ** I N S T A L L I N G D D M G R **
Prompt *****************************************************************
/*============================================================================================*/
CREATE SEQUENCE DD_SEQ_ID
/*============================================================================================*/
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9999999999
START WITH 1000
CYCLE
NOCACHE;
Prompt *****************************************************************
Prompt ** T A B L E S **
Prompt *****************************************************************
/*============================================================================================*/
CREATE TABLE DD_SYSTEMS (
/*============================================================================================*/
ID NUMBER ( 10 ) CONSTRAINT DD_SYSTEMS_NN1 NOT NULL,
NAME VARCHAR2 ( 500 ) CONSTRAINT DD_SYSTEMS_NN2 NOT NULL
);
ALTER TABLE DD_SYSTEMS ADD CONSTRAINT DD_SYSTEMS_PK PRIMARY KEY ( ID );
/*============================================================================================*/
CREATE OR REPLACE TRIGGER TR_DD_SYSTEMS_BIR
/*============================================================================================*/
BEFORE INSERT ON DD_SYSTEMS FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
:NEW.ID := DD_SEQ_ID.NEXTVAL;
END IF;
END;
/
/*============================================================================================*/
CREATE TABLE DD_OBJECTS (
/*============================================================================================*/
ID NUMBER ( 10 ) CONSTRAINT DD_OBJECTS_NN1 NOT NULL,
SYSTEM_ID NUMBER ( 10 ) CONSTRAINT DD_OBJECTS_NN2 NOT NULL,
NAME VARCHAR2 ( 500 ) CONSTRAINT DD_OBJECTS_NN3 NOT NULL
);
ALTER TABLE DD_OBJECTS ADD CONSTRAINT DD_OBJECTS_PK PRIMARY KEY ( ID );
ALTER TABLE DD_OBJECTS ADD CONSTRAINT DD_OBJECTS_FK1 FOREIGN KEY ( SYSTEM_ID ) REFERENCES DD_SYSTEMS ( ID );
CREATE INDEX DD_OBJECTS_IX1 ON DD_OBJECTS ( SYSTEM_ID ) ;
/*============================================================================================*/
CREATE OR REPLACE TRIGGER TR_DD_OBJECTS_BIR
/*============================================================================================*/
BEFORE INSERT ON DD_OBJECTS FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
:NEW.ID := DD_SEQ_ID.NEXTVAL;
END IF;
END;
/
/*============================================================================================*/
CREATE TABLE DD_ATTRIBUTES (
/*============================================================================================*/
ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTES_NN1 NOT NULL,
OBJECT_ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTES_NN2 NOT NULL,
NAME VARCHAR2 ( 500 ) CONSTRAINT DD_ATTRIBUTES_NN3 NOT NULL
);
ALTER TABLE DD_ATTRIBUTES ADD CONSTRAINT DD_ATTRIBUTES_PK PRIMARY KEY ( ID );
ALTER TABLE DD_ATTRIBUTES ADD CONSTRAINT DD_ATTRIBUTES_FK1 FOREIGN KEY ( OBJECT_ID ) REFERENCES DD_OBJECTS ( ID );
CREATE INDEX DD_ATTRIBUTES_IX1 ON DD_ATTRIBUTES ( OBJECT_ID ) ;
/*============================================================================================*/
CREATE OR REPLACE TRIGGER TR_DD_ATTRIBUTES_BIR
/*============================================================================================*/
BEFORE INSERT ON DD_ATTRIBUTES FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
:NEW.ID := DD_SEQ_ID.NEXTVAL;
END IF;
END;
/
/*============================================================================================*/
CREATE TABLE DD_ATTRIBUTE_PAIRS (
/*============================================================================================*/
ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTE_PAIRS_NN1 NOT NULL,
A_ATTRIBUTE_ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTE_PAIRS_NN2 NOT NULL,
B_ATTRIBUTE_ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTE_PAIRS_NN3 NOT NULL
);
ALTER TABLE DD_ATTRIBUTE_PAIRS ADD CONSTRAINT DD_ATTRIBUTE_PAIRS_PK PRIMARY KEY ( ID );
ALTER TABLE DD_ATTRIBUTE_PAIRS ADD CONSTRAINT DD_ATTRIBUTE_PAIRS_FK1 FOREIGN KEY ( A_ATTRIBUTE_ID ) REFERENCES DD_ATTRIBUTES ( ID );
ALTER TABLE DD_ATTRIBUTE_PAIRS ADD CONSTRAINT DD_ATTRIBUTE_PAIRS_FK2 FOREIGN KEY ( B_ATTRIBUTE_ID ) REFERENCES DD_ATTRIBUTES ( ID );
CREATE INDEX DD_ATTRIBUTE_PAIRS_IX1 ON DD_ATTRIBUTE_PAIRS ( A_ATTRIBUTE_ID ) ;
CREATE INDEX DD_ATTRIBUTE_PAIRS_IX2 ON DD_ATTRIBUTE_PAIRS ( B_ATTRIBUTE_ID ) ;
/*============================================================================================*/
CREATE OR REPLACE TRIGGER TR_DD_ATTRIBUTE_PAIRS_BIR
/*============================================================================================*/
BEFORE INSERT ON DD_ATTRIBUTE_PAIRS FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
:NEW.ID := DD_SEQ_ID.NEXTVAL;
END IF;
END;
/
/*============================================================================================*/
CREATE TABLE DD_ATTRIBUTE_VALUE_PAIRS (
/*============================================================================================*/
ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_NN1 NOT NULL,
ATTRIBUTE_PAIR_ID NUMBER ( 10 ) CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_NN2 NOT NULL,
A_ATTRIBUTE_VALUE VARCHAR2( 2000 ) CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_NN3 NOT NULL,
B_ATTRIBUTE_VALUE VARCHAR2( 2000 ) CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_NN4 NOT NULL
);
ALTER TABLE DD_ATTRIBUTE_VALUE_PAIRS ADD CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_PK PRIMARY KEY ( ID );
ALTER TABLE DD_ATTRIBUTE_VALUE_PAIRS ADD CONSTRAINT DD_ATTRIBUTE_VALUE_PAIRS_FK1 FOREIGN KEY ( ATTRIBUTE_PAIR_ID ) REFERENCES DD_ATTRIBUTE_PAIRS ( ID );
CREATE INDEX DD_ATTRIBUTE_VALUE_PAIRS_IX1 ON DD_ATTRIBUTE_VALUE_PAIRS ( ATTRIBUTE_PAIR_ID ) ;
CREATE INDEX DD_ATTRIBUTE_VALUE_PAIRS_IX2 ON DD_ATTRIBUTE_VALUE_PAIRS ( A_ATTRIBUTE_VALUE ) ;
CREATE INDEX DD_ATTRIBUTE_VALUE_PAIRS_IX3 ON DD_ATTRIBUTE_VALUE_PAIRS ( B_ATTRIBUTE_VALUE ) ;
CREATE UNIQUE INDEX DD_ATTRIBUTE_VALUE_PAIRS_IX4 ON DD_ATTRIBUTE_VALUE_PAIRS ( ATTRIBUTE_PAIR_ID, A_ATTRIBUTE_VALUE, B_ATTRIBUTE_VALUE ) ;
CREATE UNIQUE INDEX DD_ATTRIBUTE_VALUE_PAIRS_IX5 ON DD_ATTRIBUTE_VALUE_PAIRS ( ATTRIBUTE_PAIR_ID, B_ATTRIBUTE_VALUE, A_ATTRIBUTE_VALUE ) ;
/*============================================================================================*/
CREATE OR REPLACE TRIGGER TR_DD_ATTRIBUTE_VALUE_PAIR_BIR
/*============================================================================================*/
BEFORE INSERT ON DD_ATTRIBUTE_VALUE_PAIRS FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
:NEW.ID := DD_SEQ_ID.NEXTVAL;
END IF;
END;
/
Prompt *****************************************************************
Prompt ** V I E W S **
Prompt *****************************************************************
CREATE OR REPLACE VIEW DD_SYSTEMS_VW AS
SELECT *
FROM DD_SYSTEMS
;
CREATE OR REPLACE VIEW DD_OBJECTS_VW AS
SELECT DD_OBJECTS.ID AS ID
, DD_SYSTEMS.ID AS SYSTEM_ID
, DD_SYSTEMS.NAME AS SYSTEM_NAME
, DD_OBJECTS.NAME AS NAME
FROM DD_SYSTEMS
, DD_OBJECTS
WHERE DD_OBJECTS.SYSTEM_ID = DD_SYSTEMS.ID
;
CREATE OR REPLACE VIEW DD_ATTRIBUTES_VW AS
SELECT DD_ATTRIBUTES.ID AS ID
, DD_SYSTEMS.ID AS SYSTEM_ID
, DD_SYSTEMS.NAME AS SYSTEM_NAME
, DD_OBJECTS.ID AS OBJECT_ID
, DD_OBJECTS.NAME AS OBJECT_NAME
, DD_ATTRIBUTES.NAME AS NAME
FROM DD_SYSTEMS
, DD_OBJECTS
, DD_ATTRIBUTES
WHERE DD_OBJECTS.SYSTEM_ID = DD_SYSTEMS.ID
AND DD_ATTRIBUTES.OBJECT_ID = DD_OBJECTS.ID
;
CREATE OR REPLACE VIEW DD_ATTRIBUTE_PAIRS_VW AS
SELECT 'A2B' AS WAY
, DD_ATTRIBUTE_PAIRS.ID AS ID
, A_DD_SYSTEMS.ID AS A_SYSTEM_ID
, A_DD_SYSTEMS.NAME AS A_SYSTEM_NAME
, A_DD_OBJECTS.ID AS A_OBJECT_ID
, A_DD_OBJECTS.NAME AS A_OBJECT_NAME
, A_DD_ATTRIBUTES.ID AS A_ATTRIBUTE_ID
, A_DD_ATTRIBUTES.NAME AS A_ATTRIBUTE_NAME
, B_DD_SYSTEMS.ID AS B_SYSTEM_ID
, B_DD_SYSTEMS.NAME AS B_SYSTEM_NAME
, B_DD_OBJECTS.ID AS B_OBJECT_ID
, B_DD_OBJECTS.NAME AS B_OBJECT_NAME
, B_DD_ATTRIBUTES.ID AS B_ATTRIBUTE_ID
, B_DD_ATTRIBUTES.NAME AS B_ATTRIBUTE_NAME
FROM DD_SYSTEMS A_DD_SYSTEMS
, DD_OBJECTS A_DD_OBJECTS
, DD_ATTRIBUTES A_DD_ATTRIBUTES
, DD_SYSTEMS B_DD_SYSTEMS
, DD_OBJECTS B_DD_OBJECTS
, DD_ATTRIBUTES B_DD_ATTRIBUTES
, DD_ATTRIBUTE_PAIRS
WHERE DD_ATTRIBUTE_PAIRS.A_ATTRIBUTE_ID = A_DD_ATTRIBUTES.ID
AND A_DD_ATTRIBUTES.OBJECT_ID = A_DD_OBJECTS.ID
AND A_DD_OBJECTS.SYSTEM_ID = A_DD_SYSTEMS.ID
AND DD_ATTRIBUTE_PAIRS.B_ATTRIBUTE_ID = B_DD_ATTRIBUTES.ID
AND B_DD_ATTRIBUTES.OBJECT_ID = B_DD_OBJECTS.ID
AND B_DD_OBJECTS.SYSTEM_ID = B_DD_SYSTEMS.ID
UNION ALL
SELECT 'B2A' AS WAY
, DD_ATTRIBUTE_PAIRS.ID AS ID
, B_DD_SYSTEMS.ID AS A_SYSTEM_ID
, B_DD_SYSTEMS.NAME AS A_SYSTEM_NAME
, B_DD_OBJECTS.ID AS A_OBJECT_ID
, B_DD_OBJECTS.NAME AS A_OBJECT_NAME
, B_DD_ATTRIBUTES.ID AS A_ATTRIBUTE_ID
, B_DD_ATTRIBUTES.NAME AS A_ATTRIBUTE_NAME
, A_DD_SYSTEMS.ID AS B_SYSTEM_ID
, A_DD_SYSTEMS.NAME AS B_SYSTEM_NAME
, A_DD_OBJECTS.ID AS B_OBJECT_ID
, A_DD_OBJECTS.NAME AS B_OBJECT_NAME
, A_DD_ATTRIBUTES.ID AS B_ATTRIBUTE_ID
, A_DD_ATTRIBUTES.NAME AS B_ATTRIBUTE_NAME
FROM DD_SYSTEMS A_DD_SYSTEMS
, DD_OBJECTS A_DD_OBJECTS
, DD_ATTRIBUTES A_DD_ATTRIBUTES
, DD_SYSTEMS B_DD_SYSTEMS
, DD_OBJECTS B_DD_OBJECTS
, DD_ATTRIBUTES B_DD_ATTRIBUTES
, DD_ATTRIBUTE_PAIRS
WHERE DD_ATTRIBUTE_PAIRS.A_ATTRIBUTE_ID = A_DD_ATTRIBUTES.ID
AND A_DD_ATTRIBUTES.OBJECT_ID = A_DD_OBJECTS.ID
AND A_DD_OBJECTS.SYSTEM_ID = A_DD_SYSTEMS.ID
AND DD_ATTRIBUTE_PAIRS.B_ATTRIBUTE_ID = B_DD_ATTRIBUTES.ID
AND B_DD_ATTRIBUTES.OBJECT_ID = B_DD_OBJECTS.ID
AND B_DD_OBJECTS.SYSTEM_ID = B_DD_SYSTEMS.ID
;
CREATE OR REPLACE VIEW DD_ATTRIBUTE_VALUE_PAIRS_VW AS
SELECT DD_ATTRIBUTE_PAIRS_VW.*
, DD_ATTRIBUTE_VALUE_PAIRS.A_ATTRIBUTE_VALUE
, DD_ATTRIBUTE_VALUE_PAIRS.B_ATTRIBUTE_VALUE
FROM DD_ATTRIBUTE_PAIRS_VW
, DD_ATTRIBUTE_VALUE_PAIRS
WHERE WAY = 'A2B'
AND DD_ATTRIBUTE_PAIRS_VW.ID = DD_ATTRIBUTE_VALUE_PAIRS.ATTRIBUTE_PAIR_ID
UNION ALL
SELECT DD_ATTRIBUTE_PAIRS_VW.*
, DD_ATTRIBUTE_VALUE_PAIRS.B_ATTRIBUTE_VALUE
, DD_ATTRIBUTE_VALUE_PAIRS.A_ATTRIBUTE_VALUE
FROM DD_ATTRIBUTE_PAIRS_VW
, DD_ATTRIBUTE_VALUE_PAIRS
WHERE WAY = 'B2A'
AND DD_ATTRIBUTE_PAIRS_VW.ID = DD_ATTRIBUTE_VALUE_PAIRS.ATTRIBUTE_PAIR_ID
;
Prompt *****************************************************************
Prompt ** P A C K A G E **
Prompt *****************************************************************
CREATE OR REPLACE PACKAGE PKG_DDM IS
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_system ( i_system_name IN VARCHAR2 );
PROCEDURE rename_system ( i_system_name IN VARCHAR2, i_new_name IN VARCHAR2 );
PROCEDURE remove_system ( i_system_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE );
FUNCTION get_system_id ( i_system_name IN VARCHAR2 ) RETURN NUMBER;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2 );
PROCEDURE rename_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_new_name IN VARCHAR2 );
PROCEDURE remove_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE );
FUNCTION get_object_id ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2 ) RETURN NUMBER;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2 );
PROCEDURE rename_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2, i_new_name IN VARCHAR2 );
PROCEDURE remove_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE );
FUNCTION get_attribute_id ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2 ) RETURN NUMBER;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute_pair ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2
);
PROCEDURE remove_attribute_pair ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2,
i_way IN VARCHAR2 := NULL, -- null = both, or 'A2B' or 'B2A'
i_cascade IN BOOLEAN := FALSE
);
FUNCTION get_attribute_pair_id ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2,
i_way IN VARCHAR2 := NULL -- null = both, or 'A2B' or 'B2A'
) RETURN NUMBER;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute_value_pair ( i_attribute_pair_id IN NUMBER,
i_a_value IN VARCHAR2,
i_b_value IN VARCHAR2
);
PROCEDURE remove_attribute_value_pair( i_attribute_pair_id IN NUMBER,
i_a_value IN VARCHAR2,
i_b_value IN VARCHAR2
);
FUNCTION get_attribute_value ( i_attribute_pair_id IN NUMBER,
i_value IN VARCHAR2,
i_way IN VARCHAR2 := NULL, -- null = both, or 'A2B' or 'B2A'
i_exact IN BOOLEAN := TRUE, -- true = use exact match eg "=", false = use "like"
i_case_sensitive IN BOOLEAN := TRUE -- false = use UPPER before comparison
) RETURN VARCHAR2;
--------------------------------------------------------------------------------------------------------------------
END PKG_DDM;
/
CREATE OR REPLACE PACKAGE BODY PKG_DDM IS
/****************************************/
/************** SYSTEM **************/
/****************************************/
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_system ( i_system_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
INSERT INTO DD_SYSTEMS ( id, name ) VALUES ( DD_SEQ_ID.NEXTVAL, UPPER( i_system_name ) );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE rename_system ( i_system_name IN VARCHAR2, i_new_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
UPDATE DD_SYSTEMS SET name = UPPER( i_new_name ) WHERE id = get_system_id( i_system_name );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE remove_system ( i_system_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE ) IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
v_id := get_system_id( i_system_name);
IF i_cascade THEN
FOR l_r IN ( SELECT * FROM DD_OBJECTS WHERE system_id = v_id ) LOOP
remove_object( i_system_name, l_r.name, i_cascade => TRUE );
END LOOP;
END IF;
DELETE DD_SYSTEMS WHERE id = v_id;
END;
--------------------------------------------------------------------------------------------------------------------
FUNCTION get_system_id ( i_system_name IN VARCHAR2 ) RETURN NUMBER IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
SELECT MIN ( id ) INTO v_id FROM DD_SYSTEMS WHERE name = UPPER ( i_system_name );
RETURN v_id;
END;
/****************************************/
/************** OBJECT **************/
/****************************************/
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
INSERT INTO DD_OBJECTS ( id, system_id, name ) VALUES ( DD_SEQ_ID.NEXTVAL, get_system_id( i_system_name ), UPPER( i_object_name ) );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE rename_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_new_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
UPDATE DD_OBJECTS SET name = UPPER( i_new_name ) WHERE id = get_object_id( i_system_name, i_object_name );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE remove_object ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE ) IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
v_id := get_object_id( i_system_name, i_object_name );
IF i_cascade THEN
FOR l_r IN ( SELECT * FROM DD_ATTRIBUTES WHERE object_id = v_id ) LOOP
remove_attribute( i_system_name, i_object_name, l_r.name, i_cascade => TRUE );
END LOOP;
END IF;
DELETE DD_OBJECTS WHERE id = v_id;
END;
--------------------------------------------------------------------------------------------------------------------
FUNCTION get_object_id ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2 ) RETURN NUMBER IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
SELECT MIN ( id ) INTO v_id FROM DD_OBJECTS_VW WHERE system_name = UPPER( i_system_name ) AND name = UPPER ( i_object_name );
RETURN v_id;
END;
/*******************************************/
/************** ATTRIBUTE **************/
/*******************************************/
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
INSERT INTO DD_ATTRIBUTES ( id, object_id, name ) VALUES ( DD_SEQ_ID.NEXTVAL, get_object_id( i_system_name, i_object_name ), UPPER( i_attribute_name ) ) ;
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE rename_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2, i_new_name IN VARCHAR2 ) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
UPDATE DD_ATTRIBUTES SET name = UPPER( i_new_name ) WHERE id = get_attribute_id( i_system_name, i_object_name, i_attribute_name );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE remove_attribute ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2, i_cascade IN BOOLEAN := FALSE ) IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
v_id := get_attribute_id( i_system_name, i_object_name, i_attribute_name );
IF i_cascade THEN
remove_attribute_pair( i_system_name, i_object_name, i_attribute_name, NULL, NULL, NULL, i_cascade => TRUE );
remove_attribute_pair( NULL, NULL, NULL, i_system_name, i_object_name, i_attribute_name, i_cascade => TRUE );
END IF;
DELETE DD_ATTRIBUTES WHERE id = v_id;
END;
--------------------------------------------------------------------------------------------------------------------
FUNCTION get_attribute_id ( i_system_name IN VARCHAR2, i_object_name IN VARCHAR2, i_attribute_name IN VARCHAR2 ) RETURN NUMBER IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
SELECT MIN ( id ) INTO v_id FROM DD_ATTRIBUTES_VW WHERE system_name = UPPER( i_system_name ) AND object_name = UPPER( i_object_name ) AND name = UPPER ( i_attribute_name );
RETURN v_id;
END;
/***********************************************/
/************** ATTRIBUTE PAIR **************/
/***********************************************/
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute_pair ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2
) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
INSERT INTO DD_ATTRIBUTE_PAIRS ( id, a_attribute_id, b_attribute_id ) VALUES
( DD_SEQ_ID.NEXTVAL, get_attribute_id( i_a_system_name, i_a_object_name, i_a_attribute_name ),
get_attribute_id( i_b_system_name, i_b_object_name, i_b_attribute_name ) );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE remove_attribute_pair ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2,
i_way IN VARCHAR2 := NULL, -- null = both, or 'A2B' or 'B2A'
i_cascade IN BOOLEAN := FALSE
) IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
v_id := get_attribute_pair_id( i_a_system_name, i_a_object_name, i_a_attribute_name, i_b_system_name, i_b_object_name, i_b_attribute_name, i_way );
IF i_cascade THEN
remove_attribute_value_pair( v_id, NULL, NULL );
END IF;
DELETE DD_ATTRIBUTE_PAIRS WHERE id = v_id;
END;
--------------------------------------------------------------------------------------------------------------------
FUNCTION get_attribute_pair_id ( i_a_system_name IN VARCHAR2,
i_a_object_name IN VARCHAR2,
i_a_attribute_name IN VARCHAR2,
i_b_system_name IN VARCHAR2,
i_b_object_name IN VARCHAR2,
i_b_attribute_name IN VARCHAR2,
i_way IN VARCHAR2 := NULL -- null = both, or 'A2B' or 'B2A'
) RETURN NUMBER IS
--------------------------------------------------------------------------------------------------------------------
v_id NUMBER;
BEGIN
SELECT MIN ( id )
INTO v_id
FROM DD_ATTRIBUTE_PAIRS_VW
WHERE a_attribute_id = NVL( get_attribute_id( i_a_system_name, i_a_object_name, i_a_attribute_name ), a_attribute_id )
AND b_attribute_id = NVL( get_attribute_id( i_b_system_name, i_b_object_name, i_b_attribute_name ), b_attribute_id )
AND way = NVL( UPPER ( i_way ), way );
RETURN v_id;
END;
/*****************************************************/
/************** ATTRIBUTE VALUE PAIR **************/
/*****************************************************/
--------------------------------------------------------------------------------------------------------------------
PROCEDURE add_attribute_value_pair ( i_attribute_pair_id IN NUMBER,
i_a_value IN VARCHAR2,
i_b_value IN VARCHAR2
) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
INSERT INTO DD_ATTRIBUTE_VALUE_PAIRS ( id, attribute_pair_id, a_attribute_value, b_attribute_value ) VALUES
( DD_SEQ_ID.NEXTVAL, i_attribute_pair_id, i_a_value, i_b_value );
END;
--------------------------------------------------------------------------------------------------------------------
PROCEDURE remove_attribute_value_pair( i_attribute_pair_id IN NUMBER,
i_a_value IN VARCHAR2,
i_b_value IN VARCHAR2
) IS
--------------------------------------------------------------------------------------------------------------------
BEGIN
DELETE DD_ATTRIBUTE_VALUE_PAIRS
WHERE attribute_pair_id = i_attribute_pair_id
AND a_attribute_value = NVL( i_a_value, a_attribute_value)
AND b_attribute_value = NVL( i_b_value, b_attribute_value);
END;
--------------------------------------------------------------------------------------------------------------------
FUNCTION get_attribute_value ( i_attribute_pair_id IN NUMBER,
i_value IN VARCHAR2,
i_way IN VARCHAR2 := NULL, -- null = both, or 'A2B' or 'B2A'
i_exact IN BOOLEAN := TRUE, -- true = use exact match eg "=", false = use "like"
i_case_sensitive IN BOOLEAN := TRUE -- false = use UPPER before comparison
) RETURN VARCHAR2 IS
--------------------------------------------------------------------------------------------------------------------
l_sql VARCHAR2 ( 4000 );
l_value VARCHAR2 ( 2000 );
PROCEDURE set_sql ( i_from IN VARCHAR2, i_to IN VARCHAR2 ) IS
BEGIN
l_sql := 'SELECT '||i_to||'_ATTRIBUTE_VALUE FROM DD_ATTRIBUTE_VALUE_PAIRS WHERE ATTRIBUTE_PAIR_ID = '||i_attribute_pair_id||' AND ';
IF i_case_sensitive THEN
l_sql := l_sql || ' '||i_from||'_ATTRIBUTE_VALUE ';
ELSE
l_sql := l_sql || ' UPPER( '||i_from||'_ATTRIBUTE_VALUE )';
END IF;
IF i_exact THEN
l_sql := l_sql || ' = ';
ELSE
l_sql := l_sql || ' LIKE ';
END IF;
IF i_case_sensitive THEN
l_sql := l_sql || '''' || i_value || ''' ';
ELSE
l_sql := l_sql || ' UPPER( '|| '''' || i_value || ''') ';
END IF;
-- dbms_output.put_line(l_sql);
END;
BEGIN
IF i_way IS NULL OR UPPER( i_way ) = 'A2B' THEN
set_sql( 'A', 'B' );
BEGIN
EXECUTE IMMEDIATE l_sql INTO l_value;
IF l_value IS NOT NULL THEN
RETURN l_value;
END IF;
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END IF;
IF i_way IS NULL OR UPPER( i_way ) = 'B2A' THEN
set_sql( 'B', 'A' );
BEGIN
EXECUTE IMMEDIATE l_sql INTO l_value;
IF l_value IS NOT NULL THEN
RETURN l_value;
END IF;
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END IF;
RETURN NULL;
END;
--------------------------------------------------------------------------------------------------------------------
END PKG_DDM;
/