@@ -1200,6 +1200,67 @@ drop rule foorule;
1200
1200
drop table foo;
1201
1201
drop table foo2;
1202
1202
--
1203
+ -- Test rules containing INSERT ... SELECT, which is a very ugly special
1204
+ -- case as of 7.1. Example is based on bug report from Joel Burton.
1205
+ --
1206
+ create table pparent (pid int, txt text);
1207
+ insert into pparent values (1,'parent1');
1208
+ insert into pparent values (2,'parent2');
1209
+ create table cchild (pid int, descrip text);
1210
+ insert into cchild values (1,'descrip1');
1211
+ create view vview as
1212
+ select pparent.pid, txt, descrip from
1213
+ pparent left join cchild using (pid);
1214
+ create rule rrule as
1215
+ on update to vview do instead
1216
+ (
1217
+ insert into cchild (pid, descrip)
1218
+ select old.pid, new.descrip where old.descrip isnull;
1219
+ update cchild set descrip = new.descrip where cchild.pid = old.pid;
1220
+ );
1221
+ select * from vview;
1222
+ pid | txt | descrip
1223
+ -----+---------+----------
1224
+ 1 | parent1 | descrip1
1225
+ 2 | parent2 |
1226
+ (2 rows)
1227
+
1228
+ update vview set descrip='test1' where pid=1;
1229
+ select * from vview;
1230
+ pid | txt | descrip
1231
+ -----+---------+---------
1232
+ 1 | parent1 | test1
1233
+ 2 | parent2 |
1234
+ (2 rows)
1235
+
1236
+ update vview set descrip='test2' where pid=2;
1237
+ select * from vview;
1238
+ pid | txt | descrip
1239
+ -----+---------+---------
1240
+ 1 | parent1 | test1
1241
+ 2 | parent2 | test2
1242
+ (2 rows)
1243
+
1244
+ update vview set descrip='test3' where pid=3;
1245
+ select * from vview;
1246
+ pid | txt | descrip
1247
+ -----+---------+---------
1248
+ 1 | parent1 | test1
1249
+ 2 | parent2 | test2
1250
+ (2 rows)
1251
+
1252
+ select * from cchild;
1253
+ pid | descrip
1254
+ -----+---------
1255
+ 1 | test1
1256
+ 2 | test2
1257
+ (2 rows)
1258
+
1259
+ drop rule rrule;
1260
+ drop view vview;
1261
+ drop table pparent;
1262
+ drop table cchild;
1263
+ --
1203
1264
-- Check that ruleutils are working
1204
1265
--
1205
1266
SELECT viewname, definition FROM pg_views ORDER BY viewname;
0 commit comments