annotate schema/updates/1422/01.amend_trigger_func.sql @ 5005:f697a398eff4 wmst-config

Amend trigger function Additional column names given as arguments to the trigger function will be tested for equality along with the geometry. This allows to implement behavior similar to a multi-column unique constraint (in preparation to store historic versions of axis segments with differing temporal validity). Using EXISTS instead of the aggregate should be more efficient.
author Tom Gottfried <tom@intevation.de>
date Wed, 11 Mar 2020 11:24:20 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5005
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
1 CREATE OR REPLACE FUNCTION prevent_st_equals() RETURNS trigger AS
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
2 $$
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
3 DECLARE
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
4 new_geom geometry;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
5 tg_arg text;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
6 filters text;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
7 has_equal boolean;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
8 BEGIN
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
9 EXECUTE format('SELECT CAST($1.%I AS geometry)', TG_ARGV[0])
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
10 INTO new_geom
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
11 USING NEW;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
12 FOREACH tg_arg IN ARRAY TG_ARGV[1:] LOOP
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
13 -- Test each additional argument for equality
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
14 filters = format('%s AND %2$I = $2.%2$I', filters, tg_arg);
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
15 END LOOP;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
16 EXECUTE format(
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
17 'SELECT EXISTS(SELECT 1 FROM %I.%I '
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
18 'WHERE id <> $2.id AND ST_Equals($1, CAST(%I AS geometry))'
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
19 '%s)',
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
20 TG_TABLE_SCHEMA, TG_TABLE_NAME, TG_ARGV[0], filters)
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
21 INTO has_equal
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
22 USING new_geom, NEW;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
23 IF has_equal THEN
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
24 RAISE EXCEPTION
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
25 'new row for relation "%" violates constraint trigger "%"',
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
26 TG_TABLE_NAME, TG_NAME
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
27 USING
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
28 DETAIL = format('Failing row contains geometry in %s',
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
29 Box2D(new_geom)),
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
30 ERRCODE = 23505,
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
31 SCHEMA = TG_TABLE_SCHEMA,
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
32 TABLE = TG_TABLE_NAME,
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
33 COLUMN = TG_ARGV[0],
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
34 CONSTRAINT = TG_NAME;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
35 END IF;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
36 RETURN NEW;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
37 END;
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
38 $$
f697a398eff4 Amend trigger function
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
39 LANGUAGE plpgsql;