Improves ip query

This commit is contained in:
Guido Fioravantti 2015-11-12 11:57:28 +01:00
parent 1216e58120
commit 4ba76df016

View File

@ -29,7 +29,7 @@ $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION _geocode_ip_point(ip TEXT) CREATE OR REPLACE FUNCTION _geocode_ip_point(ip TEXT)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
DECLARE DECLARE
ret geocode_ip_v1%rowtype; ret Geometry;
new_ips INET[]; new_ips INET[];
old_ips TEXT[]; old_ips TEXT[];
@ -46,11 +46,12 @@ RETURNS Geometry AS $$
SELECT ip AS q, NULL as geom, FALSE as success INTO ret; SELECT ip AS q, NULL as geom, FALSE as success INTO ret;
RETURN ret; RETURN ret;
END; END;
FOR ret IN WITH ips AS (SELECT unnest(old_ips) s, unnest(new_ips) net),
WITH
ips AS (SELECT unnest(old_ips) s, unnest(new_ips) net),
matches AS (SELECT s, (SELECT the_geom FROM ip_address_locations WHERE network_start_ip <= ips.net ORDER BY network_start_ip DESC LIMIT 1) geom FROM ips) matches AS (SELECT s, (SELECT the_geom FROM ip_address_locations WHERE network_start_ip <= ips.net ORDER BY network_start_ip DESC LIMIT 1) geom FROM ips)
SELECT s, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM matches SELECT geom INTO ret
LOOP FROM matches;
RETURN ret.geom; RETURN ret;
END LOOP;
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;