Merge pull request #10 from CartoDB/adds-conf-table
Connects with conf table, implements helper and adds tests
This commit is contained in:
commit
88a7e479c9
15
server/extension/expected/10_helper_test.out
Normal file
15
server/extension/expected/10_helper_test.out
Normal file
@ -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;
|
16
server/extension/sql/0.0.1/10_helper.sql
Normal file
16
server/extension/sql/0.0.1/10_helper.sql
Normal file
@ -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;
|
@ -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)
|
CREATE OR REPLACE FUNCTION geocode_street(searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
|
||||||
RETURNS Geometry
|
RETURNS Geometry
|
||||||
AS $$
|
AS $$
|
||||||
|
import json
|
||||||
from heremaps import heremapsgeocoder
|
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)
|
geocoder = heremapsgeocoder.Geocoder(app_id, app_code)
|
||||||
|
|
||||||
results = geocoder.geocode_address(searchtext=searchtext, city=city, state=state_province, country=country)
|
results = geocoder.geocode_address(searchtext=searchtext, city=city, state=state_province, country=country)
|
12
server/extension/sql/10_helper_test.sql
Normal file
12
server/extension/sql/10_helper_test.sql
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user