-
Notifications
You must be signed in to change notification settings - Fork 2
/
evolve.sql
607 lines (557 loc) · 23.5 KB
/
evolve.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
-------------------------------------------------------------------------------
-- SCHEMA EVOLUTION UTILS
-------------------------------------------------------------------------------
-- Copyright (c) 2005-2013 Dave Hughes <[email protected]>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to
-- deal in the Software without restriction, including without limitation the
-- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-- sell copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-- IN THE SOFTWARE.
-------------------------------------------------------------------------------
-- The following code provides a set of utilities for evolving schemas,
-- particularly views and triggers. Routines are provided to recreate
-- inoperative views and triggers, and save and restore view definitions to
-- ensure preservation of authorizations. This functionality is more or less
-- redundant as of DB2 9.7 with its vastly improved schema evolution
-- capabilities, but may still prove useful in prior versions.
-------------------------------------------------------------------------------
-- ROLES
-------------------------------------------------------------------------------
-- The following roles grant usage and administrative rights to the objects
-- created by this module.
-------------------------------------------------------------------------------
CREATE ROLE UTILS_EVOLVE_USER!
CREATE ROLE UTILS_EVOLVE_ADMIN!
GRANT ROLE UTILS_EVOLVE_USER TO ROLE UTILS_USER!
GRANT ROLE UTILS_EVOLVE_USER TO ROLE UTILS_EVOLVE_ADMIN WITH ADMIN OPTION!
GRANT ROLE UTILS_EVOLVE_ADMIN TO ROLE UTILS_ADMIN WITH ADMIN OPTION!
-- RECREATE_VIEW(ASCHEMA, AVIEW)
-- RECREATE_VIEW(AVIEW)
-------------------------------------------------------------------------------
-- RECREATE_VIEW is a utility procedure which recreates the specified view
-- using the SQL found in the system catalog tables. It is useful for quickly
-- recreating views which have been marked inoperative after a change to one or
-- more of the view's dependencies. If ASCHEMA is omitted it defaults to the
-- current schema.
--
-- NOTE: The procedure does NOT drop the view before recreating it. This guards
-- against attempting to recreate an operative view (an inoperative view can be
-- recreated without dropping it first). That said, it will not return an error
-- in the case of attempting to recreate an operative view; the procedure will
-- simply do nothing.
--
-- NOTE: See the SAVE_AUTH procedure's description for warnings regarding the
-- loss of authorization information with inoperative views.
-------------------------------------------------------------------------------
CREATE PROCEDURE RECREATE_VIEW(ASCHEMA VARCHAR(128), AVIEW VARCHAR(128))
SPECIFIC RECREATE_VIEW1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE SAVE_PATH VARCHAR(254);
DECLARE SAVE_SCHEMA VARCHAR(128);
SET SAVE_PATH = CURRENT PATH;
SET SAVE_SCHEMA = CURRENT SCHEMA;
FOR D AS
SELECT
'SET SCHEMA ' || QUOTE_IDENTIFIER(QUALIFIER) AS SET_QUALIFIER,
'SET PATH ' || FUNC_PATH AS SET_PATH,
TEXT AS TEXT,
'SET SCHEMA ' || QUOTE_IDENTIFIER(SAVE_SCHEMA) AS RESTORE_QUALIFIER,
'SET PATH ' || SAVE_PATH AS RESTORE_PATH
FROM
SYSCAT.VIEWS
WHERE
VIEWSCHEMA = ASCHEMA
AND VIEWNAME = AVIEW
AND VALID = 'X'
DO
EXECUTE IMMEDIATE D.SET_QUALIFIER;
EXECUTE IMMEDIATE D.SET_PATH;
EXECUTE IMMEDIATE D.TEXT;
EXECUTE IMMEDIATE D.RESTORE_QUALIFIER;
EXECUTE IMMEDIATE D.RESTORE_PATH;
END FOR;
END!
CREATE PROCEDURE RECREATE_VIEW(AVIEW VARCHAR(128))
SPECIFIC RECREATE_VIEW2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RECREATE_VIEW(CURRENT SCHEMA, AVIEW);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEW1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEW2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEW1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEW2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RECREATE_VIEW1
IS 'Recreates the specified inoperative view from its definition in the system catalogue'!
COMMENT ON SPECIFIC PROCEDURE RECREATE_VIEW2
IS 'Recreates the specified inoperative view from its definition in the system catalogue'!
-- RECREATE_VIEWS(ASCHEMA)
-- RECREATE_VIEWS()
-------------------------------------------------------------------------------
-- RECREATE_VIEWS is a utility procedure which recreates all inoperative
-- views in the optionally specified schema. If ASCHEMA is omitted it defaults
-- to the CURRENT SCHEMA.
-------------------------------------------------------------------------------
CREATE PROCEDURE RECREATE_VIEWS(ASCHEMA VARCHAR(128))
SPECIFIC RECREATE_VIEWS1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE SAVE_PATH VARCHAR(254);
DECLARE SAVE_SCHEMA VARCHAR(128);
SET SAVE_PATH = CURRENT PATH;
SET SAVE_SCHEMA = CURRENT SCHEMA;
FOR D AS
SELECT
'SET SCHEMA ' || QUOTE_IDENTIFIER(V.QUALIFIER) AS SET_QUALIFIER,
'SET PATH ' || V.FUNC_PATH AS SET_PATH,
V.TEXT AS TEXT,
'SET SCHEMA ' || QUOTE_IDENTIFIER(SAVE_SCHEMA) AS RESTORE_QUALIFIER,
'SET PATH ' || SAVE_PATH AS RESTORE_PATH
FROM
SYSCAT.VIEWS V
INNER JOIN SYSCAT.TABLES T
ON V.VIEWSCHEMA = T.TABSCHEMA
AND V.VIEWNAME = T.TABNAME
WHERE
V.VIEWSCHEMA = ASCHEMA
AND V.VALID = 'X'
ORDER BY
T.CREATE_TIME
DO
EXECUTE IMMEDIATE D.SET_PATH;
EXECUTE IMMEDIATE D.SET_QUALIFIER;
EXECUTE IMMEDIATE D.TEXT;
EXECUTE IMMEDIATE D.RESTORE_QUALIFIER;
EXECUTE IMMEDIATE D.RESTORE_PATH;
END FOR;
END!
CREATE PROCEDURE RECREATE_VIEWS()
SPECIFIC RECREATE_VIEWS2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RECREATE_VIEWS(CURRENT SCHEMA);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEWS1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEWS2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEWS1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_VIEWS2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RECREATE_VIEWS1
IS 'Recreates all inoperative views in the specified schema from their system catalogue definitions'!
COMMENT ON SPECIFIC PROCEDURE RECREATE_VIEWS2
IS 'Recreates all inoperative views in the specified schema from their system catalogue definitions'!
-- RECREATE_TRIGGER(ASCHEMA, ATRIGGER)
-- RECREATE_TRIGGER(ATRIGGER)
-------------------------------------------------------------------------------
-- RECREATE_TRIGGER is a utility procedure which recreates the specified
-- trigger using the SQL found in the system catalog tables. It is useful for
-- quickly recreating triggers which have been marked inoperative after a
-- change to one or more of the trigger's dependencies. If ASCHEMA is omitted
-- it defaults to the current schema.
--
-- NOTE: The procedure does NOT drop the trigger before recreating it. This
-- guards against attempting to recreate an operative trigger (an inoperative
-- trigger can be recreated without dropping it first). That said, it will not
-- return an error in the case of attempting to recreate an operative trigger;
-- the procedure will simply do nothing.
-------------------------------------------------------------------------------
CREATE PROCEDURE RECREATE_TRIGGER(ASCHEMA VARCHAR(128), ATRIGGER VARCHAR(128))
SPECIFIC RECREATE_TRIGGER1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE SAVE_PATH VARCHAR(254);
DECLARE SAVE_SCHEMA VARCHAR(128);
SET SAVE_PATH = CURRENT PATH;
SET SAVE_SCHEMA = CURRENT SCHEMA;
FOR D AS
SELECT
'SET SCHEMA ' || QUOTE_IDENTIFIER(QUALIFIER) AS SET_QUALIFIER,
'SET PATH ' || FUNC_PATH AS SET_PATH,
TEXT AS TEXT,
'SET SCHEMA ' || QUOTE_IDENTIFIER(SAVE_SCHEMA) AS RESTORE_QUALIFIER,
'SET PATH ' || SAVE_PATH AS RESTORE_PATH
FROM
SYSCAT.TRIGGERS
WHERE
TRIGSCHEMA = ASCHEMA
AND TRIGNAME = ATRIGGER
AND VALID = 'X'
DO
EXECUTE IMMEDIATE D.SET_QUALIFIER;
EXECUTE IMMEDIATE D.SET_PATH;
EXECUTE IMMEDIATE D.TEXT;
EXECUTE IMMEDIATE D.RESTORE_QUALIFIER;
EXECUTE IMMEDIATE D.RESTORE_PATH;
END FOR;
END!
CREATE PROCEDURE RECREATE_TRIGGER(ATRIGGER VARCHAR(128))
SPECIFIC RECREATE_TRIGGER2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RECREATE_TRIGGER(CURRENT SCHEMA, ATRIGGER);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGER1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGER2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGER1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGER2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RECREATE_TRIGGER1
IS 'Recreates the specified inoperative trigger from its definition in the system catalogue'!
COMMENT ON SPECIFIC PROCEDURE RECREATE_TRIGGER2
IS 'Recreates the specified inoperative trigger from its definition in the system catalogue'!
-- RECREATE_TRIGGERS(ASCHEMA, ATABLE)
-- RECREATE_TRIGGERS(ATABLE)
-------------------------------------------------------------------------------
-- RECREATE_TRIGGERS is a utility procedure which recreates all inoperative
-- triggers associated with the specified table.
-------------------------------------------------------------------------------
CREATE PROCEDURE RECREATE_TRIGGERS(ASCHEMA VARCHAR(128), ATABLE VARCHAR(128))
SPECIFIC RECREATE_TRIGGERS1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE SAVE_PATH VARCHAR(254);
DECLARE SAVE_SCHEMA VARCHAR(128);
SET SAVE_PATH = CURRENT PATH;
SET SAVE_SCHEMA = CURRENT SCHEMA;
FOR D AS
SELECT
'SET SCHEMA ' || QUOTE_IDENTIFIER(QUALIFIER) AS SET_QUALIFIER,
'SET PATH ' || FUNC_PATH AS SET_PATH,
TEXT AS TEXT,
'SET SCHEMA ' || QUOTE_IDENTIFIER(SAVE_SCHEMA) AS RESTORE_QUALIFIER,
'SET PATH ' || SAVE_PATH AS RESTORE_PATH
FROM
SYSCAT.TRIGGERS
WHERE
TABSCHEMA = ASCHEMA
AND TABNAME = ATABLE
AND VALID = 'X'
ORDER BY
CREATE_TIME
DO
EXECUTE IMMEDIATE D.SET_QUALIFIER;
EXECUTE IMMEDIATE D.SET_PATH;
EXECUTE IMMEDIATE D.TEXT;
EXECUTE IMMEDIATE D.RESTORE_QUALIFIER;
EXECUTE IMMEDIATE D.RESTORE_PATH;
END FOR;
END!
CREATE PROCEDURE RECREATE_TRIGGERS(ATABLE VARCHAR(128))
SPECIFIC RECREATE_TRIGGERS2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RECREATE_TRIGGERS(CURRENT SCHEMA, ATABLE);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGERS1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGERS2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGERS1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RECREATE_TRIGGERS2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RECREATE_TRIGGERS1
IS 'Recreates all the inoperative triggers associated with the specified table from their definitions in the system catalogue'!
COMMENT ON SPECIFIC PROCEDURE RECREATE_TRIGGERS2
IS 'Recreates all the inoperative triggers associated with the specified table from their definitions in the system catalogue'!
-- SAVED_VIEWS
-------------------------------------------------------------------------------
-- A simple table which replicates a portion of the SYSCAT.VIEWS view for use
-- by the SAVE_VIEW and RESTORE_VIEW procedures below.
-------------------------------------------------------------------------------
CREATE TABLE SAVED_VIEWS AS (
SELECT
VIEWSCHEMA,
VIEWNAME,
QUALIFIER,
FUNC_PATH,
TEXT
FROM SYSCAT.VIEWS
)
WITH NO DATA!
CREATE UNIQUE INDEX SAVED_VIEWS_PK
ON SAVED_VIEWS(VIEWSCHEMA, VIEWNAME)!
ALTER TABLE SAVED_VIEWS
ADD CONSTRAINT PK PRIMARY KEY (VIEWSCHEMA, VIEWNAME)!
GRANT CONTROL ON TABLE SAVED_VIEWS TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
-- SAVE_VIEW(ASCHEMA, AVIEW)
-- SAVE_VIEW(AVIEW)
-------------------------------------------------------------------------------
-- SAVE_VIEW is a utility procedure which saves the definition of the specified
-- view to the SAVED_VIEWS table above. This saved definition can then be
-- restored with the RESTORE_VIEW procedure declared below. SAVE_VIEW and
-- RESTORE_VIEW also implicitly call SAVE_AUTH and RESTORE_AUTH to preserve the
-- authorizations of the view. This is in contrast to inoperative views
-- recreated with RECREATE_VIEW which lose authorization information.
-------------------------------------------------------------------------------
CREATE PROCEDURE SAVE_VIEW(ASCHEMA VARCHAR(128), AVIEW VARCHAR(128))
SPECIFIC SAVE_VIEW1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL SAVE_AUTH(ASCHEMA, AVIEW);
MERGE INTO SAVED_VIEWS AS DEST
USING (
SELECT VIEWSCHEMA, VIEWNAME, QUALIFIER, FUNC_PATH, TEXT
FROM SYSCAT.VIEWS
WHERE VIEWSCHEMA = ASCHEMA
AND VIEWNAME = AVIEW
) AS SRC
ON SRC.VIEWSCHEMA = DEST.VIEWSCHEMA
AND SRC.VIEWNAME = DEST.VIEWNAME
WHEN MATCHED THEN
UPDATE SET (
QUALIFIER,
FUNC_PATH,
TEXT
) = (
SRC.QUALIFIER,
SRC.FUNC_PATH,
SRC.TEXT
)
WHEN NOT MATCHED THEN
INSERT (
VIEWSCHEMA,
VIEWNAME,
QUALIFIER,
FUNC_PATH,
TEXT
)
VALUES (
SRC.VIEWSCHEMA,
SRC.VIEWNAME,
SRC.QUALIFIER,
SRC.FUNC_PATH,
SRC.TEXT
);
END!
CREATE PROCEDURE SAVE_VIEW(AVIEW VARCHAR(128))
SPECIFIC SAVE_VIEW2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL SAVE_VIEW(CURRENT SCHEMA, AVIEW);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEW1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEW2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEW1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEW2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE SAVE_VIEW1
IS 'Saves the authorizations and definition of the specified view for later restoration with RESTORE_VIEW'!
COMMENT ON SPECIFIC PROCEDURE SAVE_VIEW2
IS 'Saves the authorizations and definition of the specified view for later restoration with RESTORE_VIEW'!
-- SAVE_VIEWS(ASCHEMA)
-- SAVE_VIEWS()
-------------------------------------------------------------------------------
-- SAVE_VIEWS is a utility procedure which saves the definitions of all views
-- in the optionally specified schema to the SAVED_VIEWS table above. If
-- ASCHEMA is omitted it defaults to the CURRENT SCHEMA.
-------------------------------------------------------------------------------
CREATE PROCEDURE SAVE_VIEWS(ASCHEMA VARCHAR(128))
SPECIFIC SAVE_VIEWS1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
MERGE INTO SAVED_VIEWS AS DEST
USING (
SELECT VIEWSCHEMA, VIEWNAME, QUALIFIER, FUNC_PATH, TEXT
FROM SYSCAT.VIEWS
WHERE VIEWSCHEMA = ASCHEMA
) AS SRC
ON SRC.VIEWSCHEMA = DEST.VIEWSCHEMA
AND SRC.VIEWNAME = DEST.VIEWNAME
WHEN MATCHED THEN
UPDATE SET (
QUALIFIER,
FUNC_PATH,
TEXT
) = (
SRC.QUALIFIER,
SRC.FUNC_PATH,
SRC.TEXT
)
WHEN NOT MATCHED THEN
INSERT (
VIEWSCHEMA,
VIEWNAME,
QUALIFIER,
FUNC_PATH,
TEXT
)
VALUES (
SRC.VIEWSCHEMA,
SRC.VIEWNAME,
SRC.QUALIFIER,
SRC.FUNC_PATH,
SRC.TEXT
);
-- Can't directly use SAVE_AUTHS as that'll also save table authorizations
-- which we don't want. Instead we call SAVE_AUTH for each view definition
-- that we save...
FOR D AS
SELECT VIEWSCHEMA, VIEWNAME
FROM SYSCAT.VIEWS
WHERE VIEWSCHEMA = ASCHEMA
DO
CALL SAVE_AUTH(D.VIEWSCHEMA, D.VIEWNAME);
END FOR;
END!
CREATE PROCEDURE SAVE_VIEWS()
SPECIFIC SAVE_VIEWS2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL SAVE_VIEWS(CURRENT SCHEMA);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEWS1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEWS2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEWS1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE SAVE_VIEWS2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE SAVE_VIEWS1
IS 'Saves the authorizations and definitions of all views in the specified schema for later restoration with RESTORE_VIEWS'!
COMMENT ON SPECIFIC PROCEDURE SAVE_VIEWS2
IS 'Saves the authorizations and definitions of all views in the specified schema for later restoration with RESTORE_VIEWS'!
-- RESTORE_VIEW(ASCHEMA, AVIEW)
-- RESTORE_VIEW(AVIEW)
-------------------------------------------------------------------------------
-- RESTORE_VIEW is a utility procedure which restores the specified view using
-- the SQL found in the SAVED_VIEWS table, which is populated initially by a
-- call to SAVE_VIEW or SAVE_VIEWS. It also implicitly calls RESTORE_AUTH just
-- as SAVE_VIEW calls SAVE_AUTH to ensure that authorizations are not lost.
-- This is the primary difference between using SAVE_VIEW / RESTORE_VIEW and
-- using DB2's inoperative view mechanism with the RECREATE_VIEW procedure.
-- Another use of these procedures is in recreating views which need to be
-- dropped surrounding the update of a UDF.
-------------------------------------------------------------------------------
CREATE PROCEDURE RESTORE_VIEW(ASCHEMA VARCHAR(128), AVIEW VARCHAR(128))
SPECIFIC RESTORE_VIEW1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE SAVE_PATH VARCHAR(254);
DECLARE SAVE_SCHEMA VARCHAR(128);
SET SAVE_PATH = CURRENT PATH;
SET SAVE_SCHEMA = CURRENT SCHEMA;
FOR D AS
SELECT
'SET SCHEMA ' || QUOTE_IDENTIFIER(QUALIFIER) AS SET_QUALIFIER,
'SET PATH ' || FUNC_PATH AS SET_PATH,
TEXT AS TEXT,
'SET SCHEMA ' || QUOTE_IDENTIFIER(SAVE_SCHEMA) AS RESTORE_QUALIFIER,
'SET PATH ' || SAVE_PATH AS RESTORE_PATH
FROM
SAVED_VIEWS
WHERE
VIEWSCHEMA = ASCHEMA
AND VIEWNAME = AVIEW
DO
EXECUTE IMMEDIATE D.SET_QUALIFIER;
EXECUTE IMMEDIATE D.SET_PATH;
EXECUTE IMMEDIATE D.TEXT;
EXECUTE IMMEDIATE D.RESTORE_QUALIFIER;
EXECUTE IMMEDIATE D.RESTORE_PATH;
END FOR;
CALL RESTORE_AUTH(ASCHEMA, AVIEW);
DELETE FROM SAVED_VIEWS
WHERE VIEWSCHEMA = ASCHEMA
AND VIEWNAME = AVIEW;
END!
CREATE PROCEDURE RESTORE_VIEW(AVIEW VARCHAR(128))
SPECIFIC RESTORE_VIEW2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RESTORE_VIEW(CURRENT SCHEMA, AVIEW);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEW1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEW2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEW1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEW2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RESTORE_VIEW1
IS 'Restores the specified view which was previously saved with SAVE_VIEW'!
COMMENT ON SPECIFIC PROCEDURE RESTORE_VIEW2
IS 'Restores the specified view which was previously saved with SAVE_VIEW'!
-- RESTORE_VIEWS(ASCHEMA)
-- RESTORE_VIEWS()
-------------------------------------------------------------------------------
-- RESTORE_VIEWS is a utility procedure which restores all the views in the
-- optionally specified schema from the SAVED_VIEWS table above. If ASCHEMA is
-- omitted it defaults to the CURRENT SCHEMA.
-------------------------------------------------------------------------------
CREATE PROCEDURE RESTORE_VIEWS(ASCHEMA VARCHAR(128))
SPECIFIC RESTORE_VIEWS1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
FOR D AS
SELECT VIEWNAME
FROM SAVED_VIEWS
WHERE VIEWSCHEMA = ASCHEMA
DO
CALL RESTORE_VIEW(ASCHEMA, D.VIEWNAME);
END FOR;
END!
CREATE PROCEDURE RESTORE_VIEWS()
SPECIFIC RESTORE_VIEWS2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL RESTORE_VIEWS(CURRENT SCHEMA);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEWS1 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEWS2 TO ROLE UTILS_EVOLVE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEWS1 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE RESTORE_VIEWS2 TO ROLE UTILS_EVOLVE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE RESTORE_VIEWS1
IS 'Restores all views in the specified schema which were previously saved with SAVE_VIEWS'!
COMMENT ON SPECIFIC PROCEDURE RESTORE_VIEWS2
IS 'Restores all views in the specified schema which were previously saved with SAVE_VIEWS'!
-- vim: set et sw=4 sts=4: