diff --git a/client/Makefile b/client/Makefile index 02066e2..1e0696c 100644 --- a/client/Makefile +++ b/client/Makefile @@ -16,7 +16,7 @@ SOURCES_DATA_DIR = sql/$(EXTVERSION) # The interface definition is used along with some templates to automatically generate code RENDERER = ../sql-template-renderer -INTERFACE_FILE = ../interface.csv +INTERFACE_FILE = ../interface.yaml TEMPLATE_DIR = templates TEMPLATE_FILES = $(wildcard $(TEMPLATE_DIR)/*.erb) GENERATED_SQL_FILES = $(patsubst $(TEMPLATE_DIR)/%.erb, $(SOURCES_DATA_DIR)/%.sql, $(TEMPLATE_FILES)) @@ -24,18 +24,15 @@ GENERATED_SQL_FILES = $(patsubst $(TEMPLATE_DIR)/%.erb, $(SOURCES_DATA_DIR)/%.sq $(GENERATED_SQL_FILES): $(SOURCES_DATA_DIR)/%.sql: $(TEMPLATE_DIR)/%.erb $(INTERFACE_FILE) $(RENDERER) $(RENDERER) $(INTERFACE_FILE) $< > $@ -all: $(GENERATED_SQL_FILES) - @echo $(GENERATED_SQL_FILES) - - -SOURCES_DATA = $(wildcard $(SOURCES_DATA_DIR)/*.sql) +SOURCES_DATA = $(wildcard $(SOURCES_DATA_DIR)/*.sql) $(GENERATED_SQL_FILES) $(DATA): $(SOURCES_DATA) rm -f $@ - cat $(SOURCES_DATA) >> $@ + cat $(SOURCES_DATA_DIR)/*.sql >> $@ -#all: $(DATA) +all: $(DATA) # Only meant for development time, do not use once a version is released devclean: rm -f $(DATA) + rm -f $(GENERATED_SQL_FILES) diff --git a/client/templates/20_public_functions.erb b/client/templates/20_public_functions.erb index 3d994b0..127f54e 100644 --- a/client/templates/20_public_functions.erb +++ b/client/templates/20_public_functions.erb @@ -4,13 +4,12 @@ -- 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 %>.<%= signature['function_name'] %> (<%= signature['argument_data_types'] %>) -RETURNS <%= signature['result_data_type'] %> AS $$ +CREATE OR REPLACE FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= name %> (<%= params_with_type %>) +RETURNS <%= return_type %> AS $$ DECLARE - ret <%= signature['result_data_type'] %>; + ret <%= return_type %>; BEGIN - -- TODO: this is to be changed according to the feature doc - SELECT <%= GEOCODER_CLIENT_SCHEMA %>._geocode_admin0_polygon(session_user, txid_current(), <%= signature['argument_data_types'] %>) INTO ret; + SELECT <%= GEOCODER_CLIENT_SCHEMA %>._geocode_admin0_polygon(session_user, txid_current(), <%= params %>) INTO ret; RETURN ret; END; $$ LANGUAGE 'plpgsql' SECURITY DEFINER; diff --git a/client/templates/30_plproxy_functions.erb b/client/templates/30_plproxy_functions.erb index f877f25..22d6ffc 100644 --- a/client/templates/30_plproxy_functions.erb +++ b/client/templates/30_plproxy_functions.erb @@ -1,5 +1,5 @@ -CREATE OR REPLACE FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>._<%= signature['function_name'] %> (user_id name, tx_id bigint, <%= signature['argument_data_types'] %>) +CREATE OR REPLACE FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>._<%= name %> (user_id name, tx_id bigint, <%= params_with_type %>) RETURNS Geometry AS $$ CONNECT <%= GEOCODER_CLIENT_SCHEMA %>._server_conn_str(); - SELECT cdb_geocoder_server.<%= signature['function_name'] %> (user_id, tx_id, signature['argument_data_types']); + SELECT cdb_geocoder_server.<%= name %> (user_id, tx_id, <%= params %>); $$ LANGUAGE plproxy; diff --git a/client/templates/90_grant_execute.erb b/client/templates/90_grant_execute.erb index 33503b0..97415ca 100644 --- a/client/templates/90_grant_execute.erb +++ b/client/templates/90_grant_execute.erb @@ -1 +1 @@ -GRANT EXECUTE ON FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= signature['function_name'] %>(<%= signature['argument_data_types'] %>) TO publicuser; \ No newline at end of file +GRANT EXECUTE ON FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= name %>(<%= params_with_type %>) TO publicuser; \ No newline at end of file diff --git a/interface.yaml b/interface.yaml new file mode 100644 index 0000000..1fbdc9d --- /dev/null +++ b/interface.yaml @@ -0,0 +1,16 @@ +--- +- name: geocode_admin0_polygon + return_type: Geometry + params: + - { name: country_name, type: text } + +- name: geocode_admin1_polygon + return_type: Geometry + params: + - { name: admin1_name, type: text } + +- name: geocode_admin1_polygon + return_type: Geometry + params: + - { name: admin1_name, type: text } + - { name: country_name, type: text } \ No newline at end of file diff --git a/sql-template-renderer b/sql-template-renderer index 950a372..526bd89 100755 --- a/sql-template-renderer +++ b/sql-template-renderer @@ -3,33 +3,52 @@ # A script to automatically generate SQL files from an interface definition. # To be called like this: sql-template-renderer interface.csv templates/sql-template.erb -require 'csv' +require 'yaml' require 'erb' class SqlTemplateRenderer GEOCODER_CLIENT_SCHEMA = 'cdb_geocoder_client' - attr_reader :signature - - def initialize(template_file, signature) - @signature = signature + def initialize(template_file, function_signature) + @f = function_signature @template = File.read(template_file) end def render ERB.new(@template).result(binding) - end + end + + def name + @f['name'] + end + + def return_type + @f['return_type'] + end + + def params + @f['params'].map { |p| p['name'] }.join(', ') + end + + def params_with_type + @f['params'].map { |p| "#{p['name']} #{p['type']}"}.join(', ') + end + end + if ARGV.length != 2 then - puts "Usage: sql-template-renderer " + puts "Usage: sql-template-renderer " exit end interface_source_file = ARGV[0] template_file = ARGV[1] -CSV.foreach(interface_source_file, {headers: true}) do |function_signature| - puts SqlTemplateRenderer.new(template_file, function_signature).render + +functions = YAML.load(File.open(interface_source_file)) + +functions.each do |f| + puts SqlTemplateRenderer.new(template_file, f).render end