From 65ffb5d6454725d84f960c238579a450449da270 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 11 Nov 2015 16:52:45 +0100 Subject: [PATCH 1/3] Added schema to client functions and fixed some errors --- client/sql/0.0.1/05_geocoder_server_conf.sql | 20 ++++++++++---------- client/sql/0.0.1/10_admin0.sql | 11 ++++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/client/sql/0.0.1/05_geocoder_server_conf.sql b/client/sql/0.0.1/05_geocoder_server_conf.sql index f491e2e..914e39f 100644 --- a/client/sql/0.0.1/05_geocoder_server_conf.sql +++ b/client/sql/0.0.1/05_geocoder_server_conf.sql @@ -4,35 +4,35 @@ -- The table and the function are considered to be private and therefore -- 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 -- 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 $$ BEGIN - PERFORM _config_remove(key); - EXECUTE 'INSERT INTO _config (KEY, VALUE) VALUES ($1, $2);' USING key, value; + PERFORM cdb_geocoder_client._config_remove(key); + EXECUTE 'INSERT INTO cdb_geocoder_client._config (KEY, VALUE) VALUES ($1, $2);' USING key, value; END $$ 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 $$ BEGIN - EXECUTE 'DELETE FROM _config WHERE KEY = $1;' USING key; + EXECUTE 'DELETE FROM cdb_geocoder_client._config WHERE KEY = $1;' USING key; END $$ 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 $$ DECLARE value JSON; 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; END -$$ LANGUAGE PLPGSQL STABLE; +$$ LANGUAGE PLPGSQL STABLE; \ No newline at end of file diff --git a/client/sql/0.0.1/10_admin0.sql b/client/sql/0.0.1/10_admin0.sql index 6b7c157..c652462 100644 --- a/client/sql/0.0.1/10_admin0.sql +++ b/client/sql/0.0.1/10_admin0.sql @@ -4,14 +4,15 @@ -- These are the only ones with permissions to publicuser role -- 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 $$ DECLARE db_connection_str text; ret Geometry; BEGIN - SELECT _config_get('db_connection_str') INTO db_connection_str; - SELECT _geocode_admin0_polygons(session_user, txid_current(), db_connection_str, country_name) INTO ret; + 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; + SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), db_connection_str, country_name) INTO ret; RETURN ret; END; $$ LANGUAGE 'plpgsql' SECURITY DEFINER; @@ -21,9 +22,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, db_connection_str text, country_name text) RETURNS Geometry AS $$ -- TODO check if we can move the config to its own function CONNECT db_connection_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; From 55337efc3ae57e8a27ed12fd76cc186cbff5b048 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 11 Nov 2015 16:54:19 +0100 Subject: [PATCH 2/3] Added schema to server functions and fixed some errors --- server/extension/sql/0.0.1/10_helper.sql | 2 +- server/extension/sql/0.0.1/20_geocode_street.sql | 4 ++-- server/extension/sql/0.0.1/30_admin0.sql | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/extension/sql/0.0.1/10_helper.sql b/server/extension/sql/0.0.1/10_helper.sql index 5247057..12415ff 100644 --- a/server/extension/sql/0.0.1/10_helper.sql +++ b/server/extension/sql/0.0.1/10_helper.sql @@ -1,5 +1,5 @@ -- 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 AS $$ DECLARE diff --git a/server/extension/sql/0.0.1/20_geocode_street.sql b/server/extension/sql/0.0.1/20_geocode_street.sql index 6f17067..4bb7ebb 100644 --- a/server/extension/sql/0.0.1/20_geocode_street.sql +++ b/server/extension/sql/0.0.1/20_geocode_street.sql @@ -1,5 +1,5 @@ -- 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 AS $$ import json @@ -19,4 +19,4 @@ AS $$ point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0] return point['st_setsrid'] -$$ LANGUAGE plpythonu; +$$ LANGUAGE plpythonu; \ No newline at end of file diff --git a/server/extension/sql/0.0.1/30_admin0.sql b/server/extension/sql/0.0.1/30_admin0.sql index 1c43db2..c5e4ed6 100644 --- a/server/extension/sql/0.0.1/30_admin0.sql +++ b/server/extension/sql/0.0.1/30_admin0.sql @@ -1,6 +1,6 @@ -- 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 $$ plpy.debug('Entering geocode_admin0_polygons') plpy.debug('user_id = %s' % user_id) @@ -26,7 +26,7 @@ $$ LANGUAGE plpythonu; -- Implementation of the server 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 $$ DECLARE ret Geometry; From 22239d2da6c7ba05d1aaec176f8c928767773233 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 11 Nov 2015 17:50:22 +0100 Subject: [PATCH 3/3] Added connection function to get the connection string to the server database --- client/sql/0.0.1/06_geocoder_server_conn.sql | 16 ++++++++++++++++ client/sql/0.0.1/10_admin0.sql | 9 +++------ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 client/sql/0.0.1/06_geocoder_server_conn.sql diff --git a/client/sql/0.0.1/06_geocoder_server_conn.sql b/client/sql/0.0.1/06_geocoder_server_conn.sql new file mode 100644 index 0000000..dd53dc3 --- /dev/null +++ b/client/sql/0.0.1/06_geocoder_server_conn.sql @@ -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'; \ No newline at end of file diff --git a/client/sql/0.0.1/10_admin0.sql b/client/sql/0.0.1/10_admin0.sql index c652462..a548627 100644 --- a/client/sql/0.0.1/10_admin0.sql +++ b/client/sql/0.0.1/10_admin0.sql @@ -7,12 +7,9 @@ CREATE OR REPLACE FUNCTION cdb_geocoder_client.geocode_admin0_polygons(country_name text) RETURNS Geometry AS $$ DECLARE - db_connection_str text; ret Geometry; 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; - SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), db_connection_str, country_name) INTO ret; + SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), country_name) INTO ret; RETURN ret; END; $$ LANGUAGE 'plpgsql' SECURITY DEFINER; @@ -22,9 +19,9 @@ $$ LANGUAGE 'plpgsql' SECURITY DEFINER; -------------------------------------------------------------------------------- -CREATE OR REPLACE FUNCTION cdb_geocoder_client._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 $$ -- TODO check if we can move the config to its own function - CONNECT db_connection_str; + CONNECT cdb_geocoder_client._server_conn_str(); SELECT cdb_geocoder_server.geocode_admin0_polygons(user_id, tx_id, country_name); $$ LANGUAGE plproxy;