]> git.openstreetmap.org Git - nominatim.git/blob - sql/functions/postcode_triggers.sql
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / sql / functions / postcode_triggers.sql
1 -- Trigger functions for location_postcode table.
2
3
4 -- Trigger for updates of location_postcode
5 --
6 -- Computes the parent object the postcode most likely refers to.
7 -- This will be the place that determines the address displayed when
8 -- searching for this postcode.
9 CREATE OR REPLACE FUNCTION postcode_update()
10   RETURNS TRIGGER
11   AS $$
12 DECLARE
13   partition SMALLINT;
14   location RECORD;
15 BEGIN
16     IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
17         RETURN NEW;
18     END IF;
19
20     NEW.indexed_date = now();
21
22     partition := get_partition(NEW.country_code);
23
24     SELECT * FROM get_postcode_rank(NEW.country_code, NEW.postcode)
25       INTO NEW.rank_search, NEW.rank_address;
26
27     NEW.parent_place_id = 0;
28     FOR location IN
29       SELECT place_id
30         FROM getNearFeatures(partition, NEW.geometry, NEW.rank_search, '{}'::int[])
31         WHERE NOT isguess ORDER BY rank_address DESC LIMIT 1
32     LOOP
33         NEW.parent_place_id = location.place_id;
34     END LOOP;
35
36     RETURN NEW;
37 END;
38 $$
39 LANGUAGE plpgsql;
40