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
|
|
|
|
|
2016-02-06 00:57:56 +08:00
|
|
|
DATASERVICES_CLIENT_SCHEMA = 'cdb_dataservices_client'
|
|
|
|
DATASERVICES_SERVER_SCHEMA = 'cdb_dataservices_server'
|
2015-11-18 00:39:15 +08:00
|
|
|
|
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
|
2017-03-30 19:18:38 +08:00
|
|
|
ERB.new(@template, _save_level=nil, _trim_mode='-').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
|
|
|
|
|
2018-09-05 23:02:02 +08:00
|
|
|
def requires_permission
|
|
|
|
@function_signature['requires_permission']
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_name
|
|
|
|
@function_signature['permission_name']
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_error
|
|
|
|
@function_signature['permission_error']
|
|
|
|
end
|
|
|
|
|
2016-02-24 23:58:14 +08:00
|
|
|
def multi_field
|
|
|
|
@function_signature['multi_field']
|
|
|
|
end
|
|
|
|
|
2016-02-11 18:10:12 +08:00
|
|
|
def multi_row
|
|
|
|
@function_signature['multi_row']
|
|
|
|
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
|
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def parameters_info(with_user_org)
|
2017-03-22 23:31:45 +08:00
|
|
|
parameters = []
|
2017-03-28 16:37:21 +08:00
|
|
|
if with_user_org
|
2017-03-22 23:31:45 +08:00
|
|
|
parameters << { 'name' => 'username', 'type' => 'text' }
|
|
|
|
parameters << { 'name' => 'orgname', 'type' => 'text' }
|
|
|
|
end
|
|
|
|
parameters + @function_signature['params'].reject(&:empty?)
|
|
|
|
end
|
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def user_org_declaration()
|
|
|
|
"username text;\n orgname text;" unless superuser_function?
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def params(with_user_org = superuser_function?)
|
|
|
|
parameters_info(with_user_org).map { |p| p['name'].to_s }
|
2016-01-22 17:55:07 +08:00
|
|
|
end
|
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def params_with_type(with_user_org = superuser_function?)
|
|
|
|
parameters_info(with_user_org).map { |p| "#{p['name']} #{p['type']}" }
|
2017-03-22 23:31:45 +08:00
|
|
|
end
|
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def params_with_type_and_default(with_user_org = superuser_function?)
|
|
|
|
parameters = parameters_info(with_user_org).map do |p|
|
2016-01-22 17:38:34 +08:00
|
|
|
if not p['default'].nil?
|
|
|
|
"#{p['name']} #{p['type']} DEFAULT #{p['default']}"
|
|
|
|
else
|
|
|
|
"#{p['name']} #{p['type']}"
|
|
|
|
end
|
|
|
|
end
|
2016-11-24 20:27:32 +08:00
|
|
|
return parameters
|
2015-11-19 01:38:06 +08:00
|
|
|
end
|
2017-03-22 23:31:45 +08:00
|
|
|
|
2017-03-28 16:37:21 +08:00
|
|
|
def superuser_function?
|
|
|
|
!!@function_signature['superuser']
|
2017-03-22 23:31:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def void_return_type?
|
|
|
|
return_type.downcase == 'void'
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_declaration
|
|
|
|
"ret #{return_type};" unless void_return_type? || multi_row
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_statement(&block)
|
|
|
|
if block
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out = block.binding.eval('_erbout')
|
2017-03-22 23:31:45 +08:00
|
|
|
|
|
|
|
if multi_row
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << 'RETURN QUERY SELECT * FROM '
|
2017-03-22 23:31:45 +08:00
|
|
|
elsif multi_field
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << 'SELECT * FROM '
|
2017-03-22 23:31:45 +08:00
|
|
|
elsif void_return_type?
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << 'PERFORM '
|
2017-03-22 23:31:45 +08:00
|
|
|
else
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << 'SELECT '
|
2017-03-22 23:31:45 +08:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
if multi_row || void_return_type?
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << ';'
|
2017-03-22 23:31:45 +08:00
|
|
|
else
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << ' INTO ret;'
|
2017-03-22 23:31:45 +08:00
|
|
|
end
|
|
|
|
if !multi_row && !void_return_type?
|
2017-03-28 16:42:03 +08:00
|
|
|
erb_out << ' RETURN ret;'
|
2017-03-22 23:31:45 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if !multi_row && !void_return_type?
|
|
|
|
' RETURN ret;'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|