2020-01-31 11:45:59 +01:00
|
|
|
require './parser'
|
|
|
|
|
|
|
|
|
|
class TextToSqlQuery
|
|
|
|
|
def initialize(text, fields, default_field, fields_mappings = {})
|
|
|
|
|
@text = text.to_s.strip
|
|
|
|
|
@fields = fields.map(&:to_sym)
|
|
|
|
|
@default_field = default_field.to_sym
|
|
|
|
|
@fields_mappings = fields_mappings.merge(@fields.reduce({}) do |mappings, field|
|
|
|
|
|
_table_name, field_name = field.to_s.split('.')
|
|
|
|
|
mappings[field_name.to_sym] = field
|
|
|
|
|
mappings
|
|
|
|
|
end)
|
2020-01-31 19:23:10 +01:00
|
|
|
fields_mappings.each do |field, value|
|
|
|
|
|
@fields_mappings[field] = value if @fields_mappings[field]
|
|
|
|
|
end
|
2020-01-31 11:45:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def where_clause
|
|
|
|
|
@parser = Query.new
|
|
|
|
|
@parsed_tree = @parser.parse(@text)
|
|
|
|
|
generate_sql @parsed_tree
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def generate_sql(tree)
|
2020-01-31 13:15:27 +01:00
|
|
|
first_key = tree.keys.first
|
2020-01-31 13:58:09 +01:00
|
|
|
node_value = tree[first_key]
|
2020-01-31 13:15:27 +01:00
|
|
|
case first_key
|
2020-01-31 11:45:59 +01:00
|
|
|
when :DEFAULT_COLUMN
|
2020-01-31 16:11:12 +01:00
|
|
|
escaped_node_value = handle_special_chars node_value
|
2020-01-31 15:10:48 +01:00
|
|
|
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
2020-01-31 11:45:59 +01:00
|
|
|
when :OPERATOR_OR
|
2020-01-31 13:58:09 +01:00
|
|
|
generate_expression_for_logical_operator(:OR, node_value)
|
2020-01-31 11:45:59 +01:00
|
|
|
when :OPERATOR_AND
|
2020-01-31 13:58:09 +01:00
|
|
|
generate_expression_for_logical_operator(:AND, node_value)
|
2020-01-31 11:45:59 +01:00
|
|
|
when :OPERATOR_NOT
|
2020-01-31 13:58:09 +01:00
|
|
|
not_array = generate_sql node_value
|
2020-01-31 11:45:59 +01:00
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
if not_array.length < 2
|
2020-01-31 11:45:59 +01:00
|
|
|
raise "There should be more than 1 element for expression following NOT operator"
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
not_expression = not_array.first
|
|
|
|
|
not_params = not_array[1..]
|
2020-01-31 11:45:59 +01:00
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
["NOT #{not_expression}"] + not_params
|
2020-01-31 11:45:59 +01:00
|
|
|
|
|
|
|
|
else
|
|
|
|
|
# key is column name
|
2020-01-31 16:11:12 +01:00
|
|
|
escaped_node_value = handle_special_chars node_value
|
2020-01-31 13:15:27 +01:00
|
|
|
mapping = @fields_mappings[first_key.to_sym]
|
|
|
|
|
if mapping.nil?
|
2020-01-31 17:51:30 +01:00
|
|
|
raise "Unknown field '#{first_key.to_s}'"
|
2020-01-31 11:45:59 +01:00
|
|
|
else
|
2020-01-31 15:10:48 +01:00
|
|
|
["#{mapping.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
2020-01-31 11:45:59 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def generate_expression_for_logical_operator(operator, operator_array)
|
|
|
|
|
if operator_array.length != 2
|
|
|
|
|
raise "There should be two array elements for #{operator.to_s} operator"
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
first_operand = generate_sql operator_array.first
|
|
|
|
|
second_operand = generate_sql operator_array.last
|
2020-01-31 11:45:59 +01:00
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
if first_operand.length < 2
|
2020-01-31 11:45:59 +01:00
|
|
|
raise 'There should be more than 1 element in first operand array'
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
if second_operand.length < 2
|
2020-01-31 11:45:59 +01:00
|
|
|
raise 'There should be more than 1 element in second operand array'
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
first_operand_expression = first_operand.first
|
|
|
|
|
first_operand_params = first_operand[1..]
|
2020-01-31 11:45:59 +01:00
|
|
|
|
2020-01-31 13:15:27 +01:00
|
|
|
second_operand_expression = second_operand.first
|
|
|
|
|
second_operand_params = second_operand[1..]
|
2020-01-31 11:45:59 +01:00
|
|
|
|
2020-01-31 13:27:11 +01:00
|
|
|
["(#{first_operand_expression} #{operator.to_s} #{second_operand_expression})"] + first_operand_params + second_operand_params
|
2020-01-31 11:45:59 +01:00
|
|
|
end
|
2020-01-31 13:58:09 +01:00
|
|
|
|
2020-01-31 16:11:12 +01:00
|
|
|
def handle_special_chars(text)
|
|
|
|
|
result = text.gsub(/\"/, '')
|
2020-01-31 13:58:09 +01:00
|
|
|
result.gsub!(/\_/, '\_')
|
|
|
|
|
result.tr!('\\', '\\')
|
|
|
|
|
result.gsub!(/%/, '\%')
|
|
|
|
|
result
|
|
|
|
|
end
|
2020-01-31 11:45:59 +01:00
|
|
|
end
|