From 50abb76b6b8b843c40a54d9c309ca77e34deb250 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 11 Nov 2015 17:48:08 +0100 Subject: [PATCH] Fixes cdb_geocoder_server.geocode_ip_point --- .../extension/expected/90_geocode_ip_test.out | 2 +- server/extension/sql/0.0.1/90_geocode_ip.sql | 35 +++++++++++++------ server/extension/sql/90_geocode_ip_test.sql | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/server/extension/expected/90_geocode_ip_test.out b/server/extension/expected/90_geocode_ip_test.out index db16fa5..fb24a88 100644 --- a/server/extension/expected/90_geocode_ip_test.out +++ b/server/extension/expected/90_geocode_ip_test.out @@ -7,7 +7,7 @@ SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0 (1 row) -- Insert dummy data into ip_address_locations -INSERT INTO ip_address_locations VALUES ('0.0.0.0'::inet, (ST_SetSRID(ST_MakePoint('40.40', '3.71'), 4326))); +INSERT INTO ip_address_locations VALUES ('::ffff:0.0.0.0'::inet, (ST_SetSRID(ST_MakePoint('40.40', '3.71'), 4326))); -- This should return the polygon inserted above SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'); geocode_ip_point diff --git a/server/extension/sql/0.0.1/90_geocode_ip.sql b/server/extension/sql/0.0.1/90_geocode_ip.sql index 515ae17..6f9a306 100644 --- a/server/extension/sql/0.0.1/90_geocode_ip.sql +++ b/server/extension/sql/0.0.1/90_geocode_ip.sql @@ -28,16 +28,29 @@ $$ LANGUAGE plpythonu; -- Note: these functions depend on the cdb_geocoder extension CREATE OR REPLACE FUNCTION _geocode_ip_point(ip TEXT) RETURNS Geometry AS $$ - DECLARE - ret Geometry; + DECLARE + ret geocode_ip_v1%rowtype; - LIM INTEGER := 1; - BEGIN - SELECT ips.the_geom as geom INTO ret - FROM public.ip_address_locations ips - WHERE ips.network_start_ip = ip::inet - LIMIT LIM; - - RETURN ret; - END + new_ips INET[]; + old_ips TEXT[]; + BEGIN + BEGIN + IF family(ip::inet) = 6 THEN + new_ips := array_append(new_ips, ip::inet); + old_ips := array_append(old_ips, ip); + ELSE + new_ips := array_append(new_ips, ('::ffff:' || ip)::inet); + old_ips := array_append(old_ips, ip); + END IF; + EXCEPTION WHEN OTHERS THEN + SELECT ip AS q, NULL as geom, FALSE as success INTO ret; + RETURN ret; + END; + FOR ret IN 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) + SELECT s, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM matches + LOOP + RETURN ret.geom; + END LOOP; +END $$ LANGUAGE plpgsql; diff --git a/server/extension/sql/90_geocode_ip_test.sql b/server/extension/sql/90_geocode_ip_test.sql index 63ff315..a53b6bf 100644 --- a/server/extension/sql/90_geocode_ip_test.sql +++ b/server/extension/sql/90_geocode_ip_test.sql @@ -3,7 +3,7 @@ SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'); -- Insert dummy data into ip_address_locations -INSERT INTO ip_address_locations VALUES ('0.0.0.0'::inet, (ST_SetSRID(ST_MakePoint('40.40', '3.71'), 4326))); +INSERT INTO ip_address_locations VALUES ('::ffff:0.0.0.0'::inet, (ST_SetSRID(ST_MakePoint('40.40', '3.71'), 4326))); -- This should return the polygon inserted above SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0');