From 1c44fbbf56ca1165093c89f338745ad7fe4b51c9 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 11 Nov 2015 15:38:18 +0100 Subject: [PATCH] Adds 90_geocode_ip and tests --- ...40_admin1_test.out => 40_admin1_test.out} | 0 .../extension/expected/90_geocode_ip_test.out | 17 ++++++++ server/extension/sql/0.0.1/30_admin1.sql | 1 - server/extension/sql/0.0.1/90_geocode_ip.sql | 40 +++++++++++++++++++ server/extension/sql/90_geocode_ip_test.sql | 9 +++++ 5 files changed, 66 insertions(+), 1 deletion(-) rename server/extension/expected/{40_admin1_test.out => 40_admin1_test.out} (100%) create mode 100644 server/extension/expected/90_geocode_ip_test.out delete mode 100644 server/extension/sql/0.0.1/30_admin1.sql create mode 100644 server/extension/sql/0.0.1/90_geocode_ip.sql create mode 100644 server/extension/sql/90_geocode_ip_test.sql diff --git a/server/extension/expected/40_admin1_test.out b/server/extension/expected/40_admin1_test.out similarity index 100% rename from server/extension/expected/40_admin1_test.out rename to server/extension/expected/40_admin1_test.out diff --git a/server/extension/expected/90_geocode_ip_test.out b/server/extension/expected/90_geocode_ip_test.out new file mode 100644 index 0000000..1f81910 --- /dev/null +++ b/server/extension/expected/90_geocode_ip_test.out @@ -0,0 +1,17 @@ +-- Check that the public function is callable, even with no data +-- It should return NULL +SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'::inet); + geocode_ip_point +------------------ + +(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))); +-- This should return the polygon inserted above +SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'::inet); + geocode_ip_point +---------------------------------------------------- + 0101000020E61000003333333333334440AE47E17A14AE0D40 +(1 row) + diff --git a/server/extension/sql/0.0.1/30_admin1.sql b/server/extension/sql/0.0.1/30_admin1.sql deleted file mode 100644 index 8b13789..0000000 --- a/server/extension/sql/0.0.1/30_admin1.sql +++ /dev/null @@ -1 +0,0 @@ - diff --git a/server/extension/sql/0.0.1/90_geocode_ip.sql b/server/extension/sql/0.0.1/90_geocode_ip.sql new file mode 100644 index 0000000..1692a1a --- /dev/null +++ b/server/extension/sql/0.0.1/90_geocode_ip.sql @@ -0,0 +1,40 @@ +-- Interface of the server extension + +CREATE OR REPLACE FUNCTION geocode_ip_point(user_id NAME, tx_id BIGINT, ip INET) +RETURNS Geometry AS $$ + plpy.debug('Entering _geocode_ip_point') + plpy.debug('user_id = %s' % user_id) + + #-- Access control + #-- TODO: this should be part of cdb python library + if user_id == 'publicuser': + plpy.error('The api_key must be provided') + + #--TODO: rate limiting check + #--TODO: quota check + + #-- Copied from the doc, see http://www.postgresql.org/docs/9.4/static/plpython-database.html + plan = plpy.prepare("SELECT cdb_geocoder_server._geocode_ip_point($1) AS point", ["INET"]) + rv = plpy.execute(plan, [ip], 1) + + plpy.debug('Returning from _geocode_ip_point') + return rv[0]["point"] +$$ LANGUAGE plpythonu; + + +-------------------------------------------------------------------------------- + +-- Implementation of the server extension +-- Note: these functions depend on the cdb_geocoder extension +CREATE OR REPLACE FUNCTION _geocode_ip_point(ip INET) +RETURNS Geometry AS $$ + DECLARE + ret Geometry; + BEGIN + SELECT ips.the_geom as geom INTO ret + FROM public.ip_address_locations ips + WHERE ips.network_start_ip = ip; + + RETURN ret; + END +$$ LANGUAGE plpgsql; diff --git a/server/extension/sql/90_geocode_ip_test.sql b/server/extension/sql/90_geocode_ip_test.sql new file mode 100644 index 0000000..337a1ae --- /dev/null +++ b/server/extension/sql/90_geocode_ip_test.sql @@ -0,0 +1,9 @@ +-- Check that the public function is callable, even with no data +-- It should return NULL +SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'::inet); + +-- 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))); + +-- This should return the polygon inserted above +SELECT cdb_geocoder_server.geocode_ip_point(session_user, txid_current(), '0.0.0.0'::inet);