diff --git a/server/extension/expected/10_helper_test.out b/server/extension/expected/10_helper_test.out new file mode 100644 index 0000000..1314b51 --- /dev/null +++ b/server/extension/expected/10_helper_test.out @@ -0,0 +1,15 @@ +-- Create a conf table +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'); + _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..ce333f2 --- /dev/null +++ b/server/extension/sql/10_helper_test.sql @@ -0,0 +1,12 @@ +-- Create a conf table +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'); + +-- Test no key exception +SELECT cdb_geocoder_server._get_conf('no existe'); + +-- Drop conf table +DROP TABLE conf;