From aabc873eaccb41a7710d0d5282ca2d9c5aec0584 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 10 Nov 2015 15:31:53 +0100 Subject: [PATCH 1/2] Connects with conf table, implements helper and adds tests --- server/extension/expected/10_helper_test.out | 15 +++++++++++++++ server/extension/sql/0.0.1/10_helper.sql | 16 ++++++++++++++++ .../0.0.1/{10_geocoding.sql => 20_geocoding.sql} | 6 ++++++ server/extension/sql/10_helper_test.sql | 12 ++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 server/extension/expected/10_helper_test.out create mode 100644 server/extension/sql/0.0.1/10_helper.sql rename server/extension/sql/0.0.1/{10_geocoding.sql => 20_geocoding.sql} (76%) create mode 100644 server/extension/sql/10_helper_test.sql diff --git a/server/extension/expected/10_helper_test.out b/server/extension/expected/10_helper_test.out new file mode 100644 index 0000000..179599f --- /dev/null +++ b/server/extension/expected/10_helper_test.out @@ -0,0 +1,15 @@ +-- Create a conf table +CREATE TABLE conf (key TEXT NOT NULL PRIMARY KEY, values_json TEXT); +INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}'); +-- Test key retrieval +SELECT cdb_geocoder_server._get_conf('cds'); + _get_conf +---------------------------------------------------------------------------- + {"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}} +(1 row) + +-- Test no key exception +SELECT cdb_geocoder_server._get_conf('no existe'); +ERROR: Missing key 'no existe' in conf table +-- Drop conf table +DROP TABLE conf; diff --git a/server/extension/sql/0.0.1/10_helper.sql b/server/extension/sql/0.0.1/10_helper.sql new file mode 100644 index 0000000..5247057 --- /dev/null +++ b/server/extension/sql/0.0.1/10_helper.sql @@ -0,0 +1,16 @@ +-- Get values_json for provided key from conf table +CREATE OR REPLACE FUNCTION _get_conf(_key TEXT) + RETURNS text +AS $$ +DECLARE + rec RECORD; +BEGIN + SELECT INTO rec values_json FROM conf WHERE conf.key = _key; + + IF NOT FOUND THEN + RAISE EXCEPTION 'Missing key ''%'' in conf table', _key; + END IF; + + RETURN rec.values_json; +END +$$ LANGUAGE plpgsql; diff --git a/server/extension/sql/0.0.1/10_geocoding.sql b/server/extension/sql/0.0.1/20_geocoding.sql similarity index 76% rename from server/extension/sql/0.0.1/10_geocoding.sql rename to server/extension/sql/0.0.1/20_geocoding.sql index 70644b8..6f17067 100644 --- a/server/extension/sql/0.0.1/10_geocoding.sql +++ b/server/extension/sql/0.0.1/20_geocoding.sql @@ -2,8 +2,14 @@ CREATE OR REPLACE FUNCTION geocode_street(searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL) RETURNS Geometry AS $$ + import json from heremaps import heremapsgeocoder + heremaps_conf = json.loads(plpy.execute("SELECT cdb_geocoder_server._get_conf('heremaps')", 1)[0]['get_conf']) + + app_id = heremaps_conf['geocoder']['app_id'] + app_code = heremaps_conf['geocoder']['app_code'] + geocoder = heremapsgeocoder.Geocoder(app_id, app_code) results = geocoder.geocode_address(searchtext=searchtext, city=city, state=state_province, country=country) diff --git a/server/extension/sql/10_helper_test.sql b/server/extension/sql/10_helper_test.sql new file mode 100644 index 0000000..01c659f --- /dev/null +++ b/server/extension/sql/10_helper_test.sql @@ -0,0 +1,12 @@ +-- Create a conf table +CREATE TABLE conf (key TEXT NOT NULL PRIMARY KEY, values_json TEXT); +INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}'); + +-- Test key retrieval +SELECT cdb_geocoder_server._get_conf('cds'); + +-- Test no key exception +SELECT cdb_geocoder_server._get_conf('no existe'); + +-- Drop conf table +DROP TABLE conf; From 389b0a2b3125ab6f1f74c76c679067a4086e4dd2 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 10 Nov 2015 15:36:57 +0100 Subject: [PATCH 2/2] Changes conf table config --- server/extension/expected/10_helper_test.out | 2 +- server/extension/sql/10_helper_test.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/extension/expected/10_helper_test.out b/server/extension/expected/10_helper_test.out index 179599f..1314b51 100644 --- a/server/extension/expected/10_helper_test.out +++ b/server/extension/expected/10_helper_test.out @@ -1,5 +1,5 @@ -- Create a conf table -CREATE TABLE conf (key TEXT NOT NULL PRIMARY KEY, values_json TEXT); +CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL); INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}'); -- Test key retrieval SELECT cdb_geocoder_server._get_conf('cds'); diff --git a/server/extension/sql/10_helper_test.sql b/server/extension/sql/10_helper_test.sql index 01c659f..ce333f2 100644 --- a/server/extension/sql/10_helper_test.sql +++ b/server/extension/sql/10_helper_test.sql @@ -1,5 +1,5 @@ -- Create a conf table -CREATE TABLE conf (key TEXT NOT NULL PRIMARY KEY, values_json TEXT); +CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL); INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}'); -- Test key retrieval