2015-11-18 00:39:15 +08:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
# A script to automatically generate SQL files from an interface definition.
|
2015-11-19 22:50:57 +08:00
|
|
|
# To be called like this: sql-template-renderer interface.yaml sql-template.erb
|
2015-11-18 00:39:15 +08:00
|
|
|
|
2015-11-19 01:38:06 +08:00
|
|
|
require 'yaml'
|
2015-11-18 00:39:15 +08:00
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
class SqlTemplateRenderer
|
|
|
|
|
|
|
|
GEOCODER_CLIENT_SCHEMA = 'cdb_geocoder_client'
|
|
|
|
|
2015-11-19 01:38:06 +08:00
|
|
|
def initialize(template_file, function_signature)
|
2015-11-19 22:51:24 +08:00
|
|
|
@function_signature = function_signature
|
2015-11-18 00:39:15 +08:00
|
|
|
@template = File.read(template_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
ERB.new(@template).result(binding)
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2015-11-19 22:51:24 +08:00
|
|
|
@function_signature['name']
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def return_type
|
2015-11-19 22:51:24 +08:00
|
|
|
@function_signature['return_type']
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
2015-11-23 20:09:03 +08:00
|
|
|
def user_config_key
|
|
|
|
@function_signature['user_config_key']
|
|
|
|
end
|
|
|
|
|
|
|
|
def geocoder_config_key
|
|
|
|
@function_signature['geocoder_config_key']
|
|
|
|
end
|
|
|
|
|
2015-11-19 01:38:06 +08:00
|
|
|
def params
|
2015-11-19 22:51:24 +08:00
|
|
|
@function_signature['params'].map { |p| p['name'] }.join(', ')
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def params_with_type
|
2015-11-19 22:51:24 +08:00
|
|
|
@function_signature['params'].map { |p| "#{p['name']} #{p['type']}"}.join(', ')
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
2015-11-18 00:39:15 +08:00
|
|
|
end
|
|
|
|
|
2015-11-19 01:38:06 +08:00
|
|
|
|
2015-11-18 00:39:15 +08:00
|
|
|
if ARGV.length != 2 then
|
2015-11-19 01:38:06 +08:00
|
|
|
puts "Usage: sql-template-renderer <interface.yaml> <template.erb>"
|
2015-11-18 00:39:15 +08:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
interface_source_file = ARGV[0]
|
|
|
|
template_file = ARGV[1]
|
|
|
|
|
2015-11-19 01:38:06 +08:00
|
|
|
|
|
|
|
functions = YAML.load(File.open(interface_source_file))
|
|
|
|
|
|
|
|
functions.each do |f|
|
|
|
|
puts SqlTemplateRenderer.new(template_file, f).render
|
2015-11-18 00:39:15 +08:00
|
|
|
end
|