Adapt to yaml (WIP)

This commit is contained in:
Rafa de la Torre 2015-11-18 18:38:06 +01:00
parent c82359f7d7
commit 0d85afc99a
6 changed files with 56 additions and 25 deletions

View File

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

View File

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

View File

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

View File

@ -1 +1 @@
GRANT EXECUTE ON FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= signature['function_name'] %>(<%= signature['argument_data_types'] %>) TO publicuser;
GRANT EXECUTE ON FUNCTION <%= GEOCODER_CLIENT_SCHEMA %>.<%= name %>(<%= params_with_type %>) TO publicuser;

16
interface.yaml Normal file
View File

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

View File

@ -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 <interface.csv> <template.erb>"
puts "Usage: sql-template-renderer <interface.yaml> <template.erb>"
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