Merge pull request #10 from CartoDB/adds-conf-table

Connects with conf table, implements helper and adds tests
This commit is contained in:
Guido Fioravantti 2015-11-10 15:56:13 +01:00
commit 88a7e479c9
4 changed files with 49 additions and 0 deletions

View 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;

View 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;

View File

@ -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)

View 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;