Merge pull request #18 from CartoDB/add_schema_client_and_server

Add schema client and server
This commit is contained in:
Mario de Frutos 2015-11-11 18:13:10 +01:00
commit 86555eec15
6 changed files with 36 additions and 22 deletions

View File

@ -4,35 +4,35 @@
-- The table and the function are considered to be private and therefore -- The table and the function are considered to be private and therefore
-- no permissions are granted for any other user but the creator. -- no permissions are granted for any other user but the creator.
CREATE TABLE IF NOT EXISTS _config ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL ); CREATE TABLE IF NOT EXISTS cdb_geocoder_client._config ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL );
-- Needed to dump config in backups -- Needed to dump config in backups
-- This can only be called from an SQL script executed by CREATE EXTENSION -- This can only be called from an SQL script executed by CREATE EXTENSION
SELECT pg_catalog.pg_extension_config_dump('_config', ''); SELECT pg_catalog.pg_extension_config_dump('cdb_geocoder_client._config', '');
CREATE OR REPLACE FUNCTION _config_set(key text, value JSON) CREATE OR REPLACE FUNCTION cdb_geocoder_client._config_set(key text, value JSON)
RETURNS VOID AS $$ RETURNS VOID AS $$
BEGIN BEGIN
PERFORM _config_remove(key); PERFORM cdb_geocoder_client._config_remove(key);
EXECUTE 'INSERT INTO _config (KEY, VALUE) VALUES ($1, $2);' USING key, value; EXECUTE 'INSERT INTO cdb_geocoder_client._config (KEY, VALUE) VALUES ($1, $2);' USING key, value;
END END
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE FUNCTION _config_remove(key text) CREATE OR REPLACE FUNCTION cdb_geocoder_client._config_remove(key text)
RETURNS VOID AS $$ RETURNS VOID AS $$
BEGIN BEGIN
EXECUTE 'DELETE FROM _config WHERE KEY = $1;' USING key; EXECUTE 'DELETE FROM cdb_geocoder_client._config WHERE KEY = $1;' USING key;
END END
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE FUNCTION _config_get(key text) CREATE OR REPLACE FUNCTION cdb_geocoder_client._config_get(key text)
RETURNS JSON AS $$ RETURNS JSON AS $$
DECLARE DECLARE
value JSON; value JSON;
BEGIN BEGIN
EXECUTE 'SELECT VALUE FROM _config WHERE KEY = $1;' INTO value USING key; EXECUTE 'SELECT VALUE FROM cdb_geocoder_client._config WHERE KEY = $1;' INTO value USING key;
RETURN value; RETURN value;
END END
$$ LANGUAGE PLPGSQL STABLE; $$ LANGUAGE PLPGSQL STABLE;

View File

@ -0,0 +1,16 @@
--
-- Geocoder server connection config
--
-- The purpose of this function is provide to the PL/Proxy functions
-- the connection string needed to connect with the server
CREATE OR REPLACE FUNCTION cdb_geocoder_client._server_conn_str()
RETURNS text AS $$
DECLARE
db_connection_str text;
BEGIN
SELECT cdb_geocoder_client._config_get('db_server_config')->'connection_str' INTO db_connection_str;
SELECT trim(both '"' FROM db_connection_str) INTO db_connection_str;
RETURN db_connection_str;
END;
$$ LANGUAGE 'plpgsql';

View File

@ -4,14 +4,12 @@
-- These are the only ones with permissions to publicuser role -- These are the only ones with permissions to publicuser role
-- and should also be the only ones with SECURITY DEFINER -- and should also be the only ones with SECURITY DEFINER
CREATE OR REPLACE FUNCTION geocode_admin0_polygons(country_name text) CREATE OR REPLACE FUNCTION cdb_geocoder_client.geocode_admin0_polygons(country_name text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
DECLARE DECLARE
db_connection_str text;
ret Geometry; ret Geometry;
BEGIN BEGIN
SELECT _config_get('db_connection_str') INTO db_connection_str; SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), country_name) INTO ret;
SELECT _geocode_admin0_polygons(session_user, txid_current(), db_connection_str, country_name) INTO ret;
RETURN ret; RETURN ret;
END; END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER; $$ LANGUAGE 'plpgsql' SECURITY DEFINER;
@ -21,9 +19,9 @@ $$ LANGUAGE 'plpgsql' SECURITY DEFINER;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION _geocode_admin0_polygons(user_id name, tx_id bigint, db_connection_str text, country_name text) CREATE OR REPLACE FUNCTION cdb_geocoder_client._geocode_admin0_polygons(user_id name, tx_id bigint, country_name text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
-- TODO check if we can move the config to its own function -- TODO check if we can move the config to its own function
CONNECT db_connection_str; CONNECT cdb_geocoder_client._server_conn_str();
SELECT geocode_admin0(user_id, tx_id, country_name); SELECT cdb_geocoder_server.geocode_admin0_polygons(user_id, tx_id, country_name);
$$ LANGUAGE plproxy; $$ LANGUAGE plproxy;

View File

@ -1,5 +1,5 @@
-- Get values_json for provided key from conf table -- Get values_json for provided key from conf table
CREATE OR REPLACE FUNCTION _get_conf(_key TEXT) CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_conf(_key TEXT)
RETURNS text RETURNS text
AS $$ AS $$
DECLARE DECLARE

View File

@ -1,5 +1,5 @@
-- Geocodes a street address given a searchtext and a state and/or country -- Geocodes a street address given a searchtext and a state and/or country
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 cdb_geocoder_server.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 import json
@ -19,4 +19,4 @@ AS $$
point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0] point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0]
return point['st_setsrid'] return point['st_setsrid']
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;

View File

@ -1,6 +1,6 @@
-- Interface of the server extension -- Interface of the server extension
CREATE OR REPLACE FUNCTION geocode_admin0_polygons(user_id name, tx_id bigint, country_name text) CREATE OR REPLACE FUNCTION cdb_geocoder_server.geocode_admin0_polygons(user_id name, tx_id bigint, country_name text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
plpy.debug('Entering geocode_admin0_polygons') plpy.debug('Entering geocode_admin0_polygons')
plpy.debug('user_id = %s' % user_id) plpy.debug('user_id = %s' % user_id)
@ -26,7 +26,7 @@ $$ LANGUAGE plpythonu;
-- Implementation of the server extension -- Implementation of the server extension
-- Note: these functions depend on the cdb_geocoder extension -- Note: these functions depend on the cdb_geocoder extension
CREATE OR REPLACE FUNCTION _geocode_admin0_polygons(country_name text) CREATE OR REPLACE FUNCTION cdb_geocoder_server._geocode_admin0_polygons(country_name text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
DECLARE DECLARE
ret Geometry; ret Geometry;