Mercurial > gemma
comparison schema/updates/1436/01.amend_prevent_st_equals.sql @ 5141:722b7c305319
Prevent equal axis geometries with intersecting validity
Such things usually cannot happen via importing, but it is at least
consistent with constraints on other tables and the functionality
is needed for fairway dimensions, since they are first stored in the
staging area and equal geometries with intersecting validity could
be generated by a concurrent import.
Set the CONSTRAINT TRIGGER as INITIALLY DEFERRED because new entries
are inserted before the validity of existing entries is adapted
in the same transaction.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 31 Mar 2020 12:50:30 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5140:11c4c5b8041c | 5141:722b7c305319 |
---|---|
1 CREATE OR REPLACE FUNCTION prevent_st_equals() RETURNS trigger AS | |
2 $$ | |
3 DECLARE | |
4 new_geom geometry; | |
5 tg_arg text; | |
6 col varchar; | |
7 opr varchar; | |
8 filters text; | |
9 has_equal boolean; | |
10 BEGIN | |
11 EXECUTE format('SELECT CAST($1.%I AS geometry)', TG_ARGV[0]) | |
12 INTO new_geom | |
13 USING NEW; | |
14 FOREACH tg_arg IN ARRAY TG_ARGV[1:] LOOP | |
15 tg_arg = lower(tg_arg); | |
16 col = split_part(tg_arg, ' with ', 1); | |
17 opr = COALESCE(NULLIF(split_part(tg_arg, ' with ', 2), ''), '='); | |
18 filters = format('%s AND %I %s $2.%2$I', filters, col, opr); | |
19 END LOOP; | |
20 EXECUTE format( | |
21 'SELECT EXISTS(SELECT 1 FROM %I.%I ' | |
22 'WHERE id <> $2.id AND ST_Equals($1, CAST(%I AS geometry))' | |
23 '%s)', | |
24 TG_TABLE_SCHEMA, TG_TABLE_NAME, TG_ARGV[0], filters) | |
25 INTO has_equal | |
26 USING new_geom, NEW; | |
27 IF has_equal THEN | |
28 RAISE EXCEPTION | |
29 'new row for relation "%" violates constraint trigger "%"', | |
30 TG_TABLE_NAME, TG_NAME | |
31 USING | |
32 DETAIL = format('Failing row contains geometry in %s', | |
33 Box2D(new_geom)), | |
34 ERRCODE = 23505, | |
35 SCHEMA = TG_TABLE_SCHEMA, | |
36 TABLE = TG_TABLE_NAME, | |
37 COLUMN = TG_ARGV[0], | |
38 CONSTRAINT = TG_NAME; | |
39 END IF; | |
40 RETURN NEW; | |
41 END; | |
42 $$ | |
43 LANGUAGE plpgsql; |