]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/rules.sql
Remove non-portable queries by replacing getpgusername() with a constant
[postgresql] / src / test / regress / sql / rules.sql
1 --
2 -- rules.sql
3 -- From Jan's original setup_ruletest.sql and run_ruletest.sql
4 -- - thomas 1998-09-13
5 --
6
7 --
8 -- Tables and rules for the view test
9 --
10 create table rtest_t1 (a int4, b int4);
11 create table rtest_t2 (a int4, b int4);
12 create table rtest_t3 (a int4, b int4);
13
14 create view rtest_v1 as select * from rtest_t1;
15 create rule rtest_v1_ins as on insert to rtest_v1 do instead
16         insert into rtest_t1 values (new.a, new.b);
17 create rule rtest_v1_upd as on update to rtest_v1 do instead
18         update rtest_t1 set a = new.a, b = new.b
19         where a = old.a;
20 create rule rtest_v1_del as on delete to rtest_v1 do instead
21         delete from rtest_t1 where a = old.a;
22
23 --
24 -- Tables and rules for the constraint update/delete test
25 --
26 -- Note:
27 --      Now that we have multiple action rule support, we check
28 --      both possible syntaxes to define them (The last action
29 --  can but must not have a semicolon at the end).
30 --
31 create table rtest_system (sysname text, sysdesc text);
32 create table rtest_interface (sysname text, ifname text);
33 create table rtest_person (pname text, pdesc text);
34 create table rtest_admin (pname text, sysname text);
35
36 create rule rtest_sys_upd as on update to rtest_system do (
37         update rtest_interface set sysname = new.sysname 
38                 where sysname = old.sysname;
39         update rtest_admin set sysname = new.sysname 
40                 where sysname = old.sysname
41         );
42
43 create rule rtest_sys_del as on delete to rtest_system do (
44         delete from rtest_interface where sysname = old.sysname;
45         delete from rtest_admin where sysname = old.sysname;
46         );
47
48 create rule rtest_pers_upd as on update to rtest_person do 
49         update rtest_admin set pname = new.pname where pname = old.pname;
50
51 create rule rtest_pers_del as on delete to rtest_person do 
52         delete from rtest_admin where pname = old.pname;
53
54 --
55 -- Tables and rules for the logging test
56 --
57 create table rtest_emp (ename char(20), salary money);
58 create table rtest_emplog (ename char(20), who name, action char(10), newsal money, oldsal money);
59 create table rtest_empmass (ename char(20), salary money);
60
61 create rule rtest_emp_ins as on insert to rtest_emp do
62         insert into rtest_emplog values (new.ename, current_user,
63                         'hired', new.salary, '0.00');
64
65 create rule rtest_emp_upd as on update to rtest_emp where new.salary != old.salary do
66         insert into rtest_emplog values (new.ename, current_user,
67                         'honored', new.salary, old.salary);
68
69 create rule rtest_emp_del as on delete to rtest_emp do
70         insert into rtest_emplog values (old.ename, current_user,
71                         'fired', '0.00', old.salary);
72
73 --
74 -- Tables and rules for the multiple cascaded qualified instead
75 -- rule test 
76 --
77 create table rtest_t4 (a int4, b text);
78 create table rtest_t5 (a int4, b text);
79 create table rtest_t6 (a int4, b text);
80 create table rtest_t7 (a int4, b text);
81 create table rtest_t8 (a int4, b text);
82 create table rtest_t9 (a int4, b text);
83
84 create rule rtest_t4_ins1 as on insert to rtest_t4
85                 where new.a >= 10 and new.a < 20 do instead
86         insert into rtest_t5 values (new.a, new.b);
87
88 create rule rtest_t4_ins2 as on insert to rtest_t4
89                 where new.a >= 20 and new.a < 30 do
90         insert into rtest_t6 values (new.a, new.b);
91
92 create rule rtest_t5_ins as on insert to rtest_t5
93                 where new.a > 15 do
94         insert into rtest_t7 values (new.a, new.b);
95
96 create rule rtest_t6_ins as on insert to rtest_t6
97                 where new.a > 25 do instead
98         insert into rtest_t8 values (new.a, new.b);
99
100 --
101 -- Tables and rules for the rule fire order test
102 --
103 create table rtest_order1 (a int4);
104 create table rtest_order2 (a int4, b int4, c text);
105
106 create sequence rtest_seq;
107
108 create rule rtest_order_r3 as on insert to rtest_order1 do instead
109         insert into rtest_order2 values (new.a, nextval('rtest_seq'),
110                 'rule 3 - this should run 3rd or 4th');
111
112 create rule rtest_order_r4 as on insert to rtest_order1
113                 where a < 100 do instead
114         insert into rtest_order2 values (new.a, nextval('rtest_seq'),
115                 'rule 4 - this should run 2nd');
116
117 create rule rtest_order_r2 as on insert to rtest_order1 do
118         insert into rtest_order2 values (new.a, nextval('rtest_seq'),
119                 'rule 2 - this should run 1st');
120
121 create rule rtest_order_r1 as on insert to rtest_order1 do instead
122         insert into rtest_order2 values (new.a, nextval('rtest_seq'),
123                 'rule 1 - this should run 3rd or 4th');
124
125 --
126 -- Tables and rules for the instead nothing test
127 --
128 create table rtest_nothn1 (a int4, b text);
129 create table rtest_nothn2 (a int4, b text);
130 create table rtest_nothn3 (a int4, b text);
131 create table rtest_nothn4 (a int4, b text);
132
133 create rule rtest_nothn_r1 as on insert to rtest_nothn1
134         where new.a >= 10 and new.a < 20 do instead (select 1);
135
136 create rule rtest_nothn_r2 as on insert to rtest_nothn1
137         where new.a >= 30 and new.a < 40 do instead nothing;
138
139 create rule rtest_nothn_r3 as on insert to rtest_nothn2
140         where new.a >= 100 do instead
141         insert into rtest_nothn3 values (new.a, new.b);
142
143 create rule rtest_nothn_r4 as on insert to rtest_nothn2
144         do instead nothing;
145
146 --
147 -- Tests on a view that is select * of a table
148 -- and has insert/update/delete instead rules to
149 -- behave close like the real table.
150 --
151
152 --
153 -- We need test date later
154 --
155 insert into rtest_t2 values (1, 21);
156 insert into rtest_t2 values (2, 22);
157 insert into rtest_t2 values (3, 23);
158
159 insert into rtest_t3 values (1, 31);
160 insert into rtest_t3 values (2, 32);
161 insert into rtest_t3 values (3, 33);
162 insert into rtest_t3 values (4, 34);
163 insert into rtest_t3 values (5, 35);
164
165 -- insert values
166 insert into rtest_v1 values (1, 11);
167 insert into rtest_v1 values (2, 12);
168 select * from rtest_v1;
169
170 -- delete with constant expression
171 delete from rtest_v1 where a = 1;
172 select * from rtest_v1;
173 insert into rtest_v1 values (1, 11);
174 delete from rtest_v1 where b = 12;
175 select * from rtest_v1;
176 insert into rtest_v1 values (2, 12);
177 insert into rtest_v1 values (2, 13);
178 select * from rtest_v1;
179 ** Remember the delete rule on rtest_v1: It says
180 ** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
181 ** So this time both rows with a = 2 must get deleted
182 \p
183 \r
184 delete from rtest_v1 where b = 12;
185 select * from rtest_v1;
186 delete from rtest_v1;
187
188 -- insert select
189 insert into rtest_v1 select * from rtest_t2;
190 select * from rtest_v1;
191 delete from rtest_v1;
192
193 -- same with swapped targetlist
194 insert into rtest_v1 (b, a) select b, a from rtest_t2;
195 select * from rtest_v1;
196
197 -- now with only one target attribute
198 insert into rtest_v1 (a) select a from rtest_t3;
199 select * from rtest_v1;
200 select * from rtest_v1 where b isnull;
201
202 -- let attribute a differ (must be done on rtest_t1 - see above)
203 update rtest_t1 set a = a + 10 where b isnull;
204 delete from rtest_v1 where b isnull;
205 select * from rtest_v1;
206
207 -- now updates with constant expression
208 update rtest_v1 set b = 42 where a = 2;
209 select * from rtest_v1;
210 update rtest_v1 set b = 99 where b = 42;
211 select * from rtest_v1;
212 update rtest_v1 set b = 88 where b < 50;
213 select * from rtest_v1;
214 delete from rtest_v1;
215 insert into rtest_v1 select rtest_t2.a, rtest_t3.b where rtest_t2.a = rtest_t3.a;
216 select * from rtest_v1;
217
218 -- updates in a mergejoin
219 update rtest_v1 set b = rtest_t2.b where a = rtest_t2.a;
220 select * from rtest_v1;
221 insert into rtest_v1 select * from rtest_t3;
222 select * from rtest_v1;
223 update rtest_t1 set a = a + 10 where b > 30;
224 select * from rtest_v1;
225 update rtest_v1 set a = rtest_t3.a + 20 where b = rtest_t3.b;
226 select * from rtest_v1;
227
228 --
229 -- Test for constraint updates/deletes
230 --
231 insert into rtest_system values ('orion', 'Linux Jan Wieck');
232 insert into rtest_system values ('notjw', 'WinNT Jan Wieck (notebook)');
233 insert into rtest_system values ('neptun', 'Fileserver');
234
235 insert into rtest_interface values ('orion', 'eth0');
236 insert into rtest_interface values ('orion', 'eth1');
237 insert into rtest_interface values ('notjw', 'eth0');
238 insert into rtest_interface values ('neptun', 'eth0');
239
240 insert into rtest_person values ('jw', 'Jan Wieck');
241 insert into rtest_person values ('bm', 'Bruce Momjian');
242
243 insert into rtest_admin values ('jw', 'orion');
244 insert into rtest_admin values ('jw', 'notjw');
245 insert into rtest_admin values ('bm', 'neptun');
246
247 update rtest_system set sysname = 'pluto' where sysname = 'neptun';
248
249 select * from rtest_interface;
250 select * from rtest_admin;
251
252 update rtest_person set pname = 'jwieck' where pdesc = 'Jan Wieck';
253
254 -- Note: use ORDER BY here to ensure consistent output across all systems.
255 -- The above UPDATE affects two rows with equal keys, so they could be
256 -- updated in either order depending on the whim of the local qsort().
257
258 select * from rtest_admin order by pname, sysname;
259
260 delete from rtest_system where sysname = 'orion';
261
262 select * from rtest_interface;
263 select * from rtest_admin;
264
265 --
266 -- Rule qualification test
267 --
268 insert into rtest_emp values ('wiech', '5000.00');
269 insert into rtest_emp values ('gates', '80000.00');
270 update rtest_emp set ename = 'wiecx' where ename = 'wiech';
271 update rtest_emp set ename = 'wieck', salary = '6000.00' where ename = 'wiecx';
272 update rtest_emp set salary = '7000.00' where ename = 'wieck';
273 delete from rtest_emp where ename = 'gates';
274
275 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog;
276 insert into rtest_empmass values ('meyer', '4000.00');
277 insert into rtest_empmass values ('maier', '5000.00');
278 insert into rtest_empmass values ('mayr', '6000.00');
279 insert into rtest_emp select * from rtest_empmass;
280 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog;
281 update rtest_empmass set salary = salary + '1000.00';
282 update rtest_emp set salary = rtest_empmass.salary where ename = rtest_empmass.ename;
283 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog;
284 delete from rtest_emp where ename = rtest_empmass.ename;
285 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog;
286
287 --
288 -- Multiple cascaded qualified instead rule test
289 --
290 insert into rtest_t4 values (1, 'Record should go to rtest_t4');
291 insert into rtest_t4 values (2, 'Record should go to rtest_t4');
292 insert into rtest_t4 values (10, 'Record should go to rtest_t5');
293 insert into rtest_t4 values (15, 'Record should go to rtest_t5');
294 insert into rtest_t4 values (19, 'Record should go to rtest_t5 and t7');
295 insert into rtest_t4 values (20, 'Record should go to rtest_t4 and t6');
296 insert into rtest_t4 values (26, 'Record should go to rtest_t4 and t8');
297 insert into rtest_t4 values (28, 'Record should go to rtest_t4 and t8');
298 insert into rtest_t4 values (30, 'Record should go to rtest_t4');
299 insert into rtest_t4 values (40, 'Record should go to rtest_t4');
300
301 select * from rtest_t4;
302 select * from rtest_t5;
303 select * from rtest_t6;
304 select * from rtest_t7;
305 select * from rtest_t8;
306
307 delete from rtest_t4;
308 delete from rtest_t5;
309 delete from rtest_t6;
310 delete from rtest_t7;
311 delete from rtest_t8;
312
313 insert into rtest_t9 values (1, 'Record should go to rtest_t4');
314 insert into rtest_t9 values (2, 'Record should go to rtest_t4');
315 insert into rtest_t9 values (10, 'Record should go to rtest_t5');
316 insert into rtest_t9 values (15, 'Record should go to rtest_t5');
317 insert into rtest_t9 values (19, 'Record should go to rtest_t5 and t7');
318 insert into rtest_t9 values (20, 'Record should go to rtest_t4 and t6');
319 insert into rtest_t9 values (26, 'Record should go to rtest_t4 and t8');
320 insert into rtest_t9 values (28, 'Record should go to rtest_t4 and t8');
321 insert into rtest_t9 values (30, 'Record should go to rtest_t4');
322 insert into rtest_t9 values (40, 'Record should go to rtest_t4');
323
324 insert into rtest_t4 select * from rtest_t9 where a < 20;
325
326 select * from rtest_t4;
327 select * from rtest_t5;
328 select * from rtest_t6;
329 select * from rtest_t7;
330 select * from rtest_t8;
331
332 insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
333
334 select * from rtest_t4;
335 select * from rtest_t5;
336 select * from rtest_t6;
337 select * from rtest_t7;
338 select * from rtest_t8;
339
340 insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
341
342 select * from rtest_t4;
343 select * from rtest_t5;
344 select * from rtest_t6;
345 select * from rtest_t7;
346 select * from rtest_t8;
347
348 --
349 -- Check that the ordering of rules fired is correct
350 --
351 insert into rtest_order1 values (1);
352 select * from rtest_order2;
353
354 --
355 -- Check if instead nothing w/without qualification works
356 --
357 insert into rtest_nothn1 values (1, 'want this');
358 insert into rtest_nothn1 values (2, 'want this');
359 insert into rtest_nothn1 values (10, 'don''t want this');
360 insert into rtest_nothn1 values (19, 'don''t want this');
361 insert into rtest_nothn1 values (20, 'want this');
362 insert into rtest_nothn1 values (29, 'want this');
363 insert into rtest_nothn1 values (30, 'don''t want this');
364 insert into rtest_nothn1 values (39, 'don''t want this');
365 insert into rtest_nothn1 values (40, 'want this');
366 insert into rtest_nothn1 values (50, 'want this');
367 insert into rtest_nothn1 values (60, 'want this');
368
369 select * from rtest_nothn1;
370
371 insert into rtest_nothn2 values (10, 'too small');
372 insert into rtest_nothn2 values (50, 'too small');
373 insert into rtest_nothn2 values (100, 'OK');
374 insert into rtest_nothn2 values (200, 'OK');
375
376 select * from rtest_nothn2;
377 select * from rtest_nothn3;
378
379 delete from rtest_nothn1;
380 delete from rtest_nothn2;
381 delete from rtest_nothn3;
382
383 insert into rtest_nothn4 values (1, 'want this');
384 insert into rtest_nothn4 values (2, 'want this');
385 insert into rtest_nothn4 values (10, 'don''t want this');
386 insert into rtest_nothn4 values (19, 'don''t want this');
387 insert into rtest_nothn4 values (20, 'want this');
388 insert into rtest_nothn4 values (29, 'want this');
389 insert into rtest_nothn4 values (30, 'don''t want this');
390 insert into rtest_nothn4 values (39, 'don''t want this');
391 insert into rtest_nothn4 values (40, 'want this');
392 insert into rtest_nothn4 values (50, 'want this');
393 insert into rtest_nothn4 values (60, 'want this');
394
395 insert into rtest_nothn1 select * from rtest_nothn4;
396
397 select * from rtest_nothn1;
398
399 delete from rtest_nothn4;
400
401 insert into rtest_nothn4 values (10, 'too small');
402 insert into rtest_nothn4 values (50, 'too small');
403 insert into rtest_nothn4 values (100, 'OK');
404 insert into rtest_nothn4 values (200, 'OK');
405
406 insert into rtest_nothn2 select * from rtest_nothn4;
407
408 select * from rtest_nothn2;
409 select * from rtest_nothn3;
410
411 create table rtest_view1 (a int4, b text, v bool);
412 create table rtest_view2 (a int4);
413 create table rtest_view3 (a int4, b text);
414 create table rtest_view4 (a int4, b text, c int4);
415 create view rtest_vview1 as select a, b from rtest_view1 X 
416         where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
417 create view rtest_vview2 as select a, b from rtest_view1 where v;
418 create view rtest_vview3 as select a, b from rtest_vview2 X
419         where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
420 create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount
421         from rtest_view1 X, rtest_view2 Y
422         where X.a = Y.a
423         group by X.a, X.b;
424 create function rtest_viewfunc1(int4) returns int4 as
425         'select count(*) from rtest_view2 where a = $1'
426         language 'sql';
427 create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
428         from rtest_view1;
429
430 insert into rtest_view1 values (1, 'item 1', 't');
431 insert into rtest_view1 values (2, 'item 2', 't');
432 insert into rtest_view1 values (3, 'item 3', 't');
433 insert into rtest_view1 values (4, 'item 4', 'f');
434 insert into rtest_view1 values (5, 'item 5', 't');
435 insert into rtest_view1 values (6, 'item 6', 'f');
436 insert into rtest_view1 values (7, 'item 7', 't');
437 insert into rtest_view1 values (8, 'item 8', 't');
438
439 insert into rtest_view2 values (2);
440 insert into rtest_view2 values (2);
441 insert into rtest_view2 values (4);
442 insert into rtest_view2 values (5);
443 insert into rtest_view2 values (7);
444 insert into rtest_view2 values (7);
445 insert into rtest_view2 values (7);
446 insert into rtest_view2 values (7);
447
448 select * from rtest_vview1;
449 select * from rtest_vview2;
450 select * from rtest_vview3;
451 select * from rtest_vview4;
452 select * from rtest_vview5;
453
454 insert into rtest_view3 select * from rtest_vview1 where a < 7;
455 select * from rtest_view3;
456 delete from rtest_view3;
457
458 insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';
459 select * from rtest_view3;
460 delete from rtest_view3;
461
462 insert into rtest_view3 select * from rtest_vview3;
463 select * from rtest_view3;
464 delete from rtest_view3;
465
466 insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
467 select * from rtest_view4;
468 delete from rtest_view4;
469
470 insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;
471 select * from rtest_view4;
472 delete from rtest_view4;
473 --
474 -- Test for computations in views
475 --
476 create table rtest_comp (
477         part    text,
478         unit    char(4),
479         size    float
480 );
481
482
483 create table rtest_unitfact (
484         unit    char(4),
485         factor  float
486 );
487
488 create view rtest_vcomp as 
489         select X.part, (X.size * Y.factor) as size_in_cm
490                         from rtest_comp X, rtest_unitfact Y
491                         where X.unit = Y.unit;
492
493
494 insert into rtest_unitfact values ('m', 100.0);
495 insert into rtest_unitfact values ('cm', 1.0);
496 insert into rtest_unitfact values ('inch', 2.54);
497
498 insert into rtest_comp values ('p1', 'm', 5.0);
499 insert into rtest_comp values ('p2', 'm', 3.0);
500 insert into rtest_comp values ('p3', 'cm', 5.0);
501 insert into rtest_comp values ('p4', 'cm', 15.0);
502 insert into rtest_comp values ('p5', 'inch', 7.0);
503 insert into rtest_comp values ('p6', 'inch', 4.4);
504
505 select * from rtest_vcomp order by part;
506
507 select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
508
509 --
510 -- In addition run the (slightly modified) queries from the
511 -- programmers manual section on the rule system.
512 --
513 CREATE TABLE shoe_data (
514         shoename   char(10),      -- primary key
515         sh_avail   integer,       -- available # of pairs
516         slcolor    char(10),      -- preferred shoelace color
517         slminlen   float,         -- miminum shoelace length
518         slmaxlen   float,         -- maximum shoelace length
519         slunit     char(8)        -- length unit
520 );
521
522 CREATE TABLE shoelace_data (
523         sl_name    char(10),      -- primary key
524         sl_avail   integer,       -- available # of pairs
525         sl_color   char(10),      -- shoelace color
526         sl_len     float,         -- shoelace length
527         sl_unit    char(8)        -- length unit
528 );
529
530 CREATE TABLE unit (
531         un_name    char(8),       -- the primary key
532         un_fact    float          -- factor to transform to cm
533 );
534
535 CREATE VIEW shoe AS
536         SELECT sh.shoename,
537                    sh.sh_avail,
538                    sh.slcolor,
539                    sh.slminlen,
540                    sh.slminlen * un.un_fact AS slminlen_cm,
541                    sh.slmaxlen,
542                    sh.slmaxlen * un.un_fact AS slmaxlen_cm,
543                    sh.slunit
544           FROM shoe_data sh, unit un
545          WHERE sh.slunit = un.un_name;
546
547 CREATE VIEW shoelace AS
548         SELECT s.sl_name,
549                    s.sl_avail,
550                    s.sl_color,
551                    s.sl_len,
552                    s.sl_unit,
553                    s.sl_len * u.un_fact AS sl_len_cm
554           FROM shoelace_data s, unit u
555          WHERE s.sl_unit = u.un_name;
556
557 CREATE VIEW shoe_ready AS
558         SELECT rsh.shoename,
559                    rsh.sh_avail,
560                    rsl.sl_name,
561                    rsl.sl_avail,
562                    int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail
563           FROM shoe rsh, shoelace rsl
564          WHERE rsl.sl_color = rsh.slcolor
565            AND rsl.sl_len_cm >= rsh.slminlen_cm
566            AND rsl.sl_len_cm <= rsh.slmaxlen_cm;
567
568 INSERT INTO unit VALUES ('cm', 1.0);
569 INSERT INTO unit VALUES ('m', 100.0);
570 INSERT INTO unit VALUES ('inch', 2.54);
571
572 INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');
573 INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');
574 INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');
575 INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');
576
577 INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');
578 INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');
579 INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');
580 INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');
581 INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');
582 INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');
583 INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');
584 INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');
585
586 -- SELECTs in doc
587 SELECT * FROM shoelace;
588 SELECT * FROM shoe_ready WHERE total_avail >= 2;
589
590     CREATE TABLE shoelace_log (
591         sl_name    char(10),      -- shoelace changed
592         sl_avail   integer,       -- new available value
593         log_who    name,          -- who did it
594         log_when   datetime       -- when
595     );
596
597 -- Want "log_who" to be CURRENT_USER,
598 -- but that is non-portable for the regression test
599 -- - thomas 1999-02-21
600
601     CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
602         WHERE NEW.sl_avail != OLD.sl_avail
603         DO INSERT INTO shoelace_log VALUES (
604                                         NEW.sl_name,
605                                         NEW.sl_avail,
606                                         'Al Bundy',
607                                         'epoch'::text
608                                     );
609
610 UPDATE shoelace_data SET sl_avail = 6 WHERE  sl_name = 'sl7';
611
612 SELECT * FROM shoelace_log;
613
614     CREATE RULE shoelace_ins AS ON INSERT TO shoelace
615         DO INSTEAD
616         INSERT INTO shoelace_data VALUES (
617                NEW.sl_name,
618                NEW.sl_avail,
619                NEW.sl_color,
620                NEW.sl_len,
621                NEW.sl_unit);
622
623     CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
624         DO INSTEAD
625         UPDATE shoelace_data SET
626                sl_name = NEW.sl_name,
627                sl_avail = NEW.sl_avail,
628                sl_color = NEW.sl_color,
629                sl_len = NEW.sl_len,
630                sl_unit = NEW.sl_unit
631          WHERE sl_name = OLD.sl_name;
632
633     CREATE RULE shoelace_del AS ON DELETE TO shoelace
634         DO INSTEAD
635         DELETE FROM shoelace_data
636          WHERE sl_name = OLD.sl_name;
637
638     CREATE TABLE shoelace_arrive (
639         arr_name    char(10),
640         arr_quant   integer
641     );
642
643     CREATE TABLE shoelace_ok (
644         ok_name     char(10),
645         ok_quant    integer
646     );
647
648     CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
649         DO INSTEAD
650         UPDATE shoelace SET
651                sl_avail = sl_avail + NEW.ok_quant
652          WHERE sl_name = NEW.ok_name;
653
654 INSERT INTO shoelace_arrive VALUES ('sl3', 10);
655 INSERT INTO shoelace_arrive VALUES ('sl6', 20);
656 INSERT INTO shoelace_arrive VALUES ('sl8', 20);
657
658 SELECT * FROM shoelace;
659
660 insert into shoelace_ok select * from shoelace_arrive;
661
662 SELECT * FROM shoelace;
663
664 SELECT * FROM shoelace_log;
665
666     CREATE VIEW shoelace_obsolete AS
667         SELECT * FROM shoelace WHERE NOT EXISTS
668             (SELECT shoename FROM shoe WHERE slcolor = sl_color);
669
670     CREATE VIEW shoelace_candelete AS
671         SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;
672
673 insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);
674 insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);
675
676 SELECT * FROM shoelace_obsolete;
677 SELECT * FROM shoelace_candelete;
678
679 DELETE FROM shoelace WHERE EXISTS
680     (SELECT * FROM shoelace_candelete
681              WHERE sl_name = shoelace.sl_name);
682
683 SELECT * FROM shoelace;