Add code generator for public API function definitions and their plproxied counterparts

This commit is contained in:
Rafa de la Torre 2015-11-17 16:28:33 +01:00
parent 0f953dd5b6
commit e0259a8b8c
2 changed files with 44 additions and 1 deletions

View File

@ -23,9 +23,28 @@ class GrantExecute
end end
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| 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 grant = GrantExecute.new(function_signature).render
puts grant #puts grant
end end

View File

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