From e0259a8b8c2a5b4544b8823f269d8d7ed6b2ff52 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 17 Nov 2015 16:28:33 +0100 Subject: [PATCH] Add code generator for public API function definitions and their plproxied counterparts --- interface/generate-grant-execute.rb | 21 +++++++++++++++- .../templates/public-function-definition.erb | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 interface/templates/public-function-definition.erb diff --git a/interface/generate-grant-execute.rb b/interface/generate-grant-execute.rb index b572723..2cb001f 100644 --- a/interface/generate-grant-execute.rb +++ b/interface/generate-grant-execute.rb @@ -23,9 +23,28 @@ class GrantExecute end end +class PublicFunctionDefinition + TEMPLATE_FILE = 'templates/public-function-definition.erb' + + attr_reader :function_signature + + def initialize(function_signature) + @function_signature = function_signature + @template = File.read(TEMPLATE_FILE) + end + + def render + ERB.new(@template).result(binding) + end +end CSV.foreach(INTERFACE_SOURCE_FILE, {headers: true}) do |function_signature| + + function_definition = PublicFunctionDefinition.new(function_signature).render + puts function_definition + + grant = GrantExecute.new(function_signature).render - puts grant + #puts grant end diff --git a/interface/templates/public-function-definition.erb b/interface/templates/public-function-definition.erb new file mode 100644 index 0000000..8bef626 --- /dev/null +++ b/interface/templates/public-function-definition.erb @@ -0,0 +1,24 @@ +-- +-- Public geocoder API function +-- +-- These are the only ones with permissions to publicuser role +-- and should also be the only ones with SECURITY DEFINER + +CREATE OR REPLACE FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= function_signature['function_name'] %> (<%= function_signature['argument_data_types'] %>) +RETURNS <%= function_signature['result_data_type'] %> AS $$ +DECLARE + ret <%= function_signature['result_data_type'] %>; +BEGIN + -- TODO: this is to be changed according to the feature doc + SELECT <%= GEOCODER_CLIENT_SCHEMA %>._geocode_admin0_polygon(session_user, txid_current(), <%= function_signature['argument_data_types'] %>) INTO ret; + RETURN ret; +END; +$$ LANGUAGE 'plpgsql' SECURITY DEFINER; + +-------------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>._<%= function_signature['function_name'] %> (user_id name, tx_id bigint, <%= function_signature['argument_data_types'] %>) +RETURNS Geometry AS $$ + CONNECT <%= GEOCODER_CLIENT_SCHEMA %>._server_conn_str(); + SELECT cdb_geocoder_server.<%= function_signature['function_name'] %> (user_id, tx_id, function_signature['argument_data_types']); +$$ LANGUAGE plproxy;