Enable multiple default columns

This commit is contained in:
Bilal
2020-04-16 14:59:51 +02:00
parent afbfdb87cd
commit 838b60ed92

View File

@@ -1,10 +1,17 @@
require_relative 'parser'
class TextToSqlQuery
def initialize(text, fields, default_field, fields_mappings = {})
def initialize(text, fields, default_fields, fields_mappings = {})
@text = text.to_s.strip
@fields = fields.map(&:to_sym)
@default_field = default_field.to_sym
# Keep compatibility with previous version where default_field(s) was string/symbol
@default_fields = if default_fields.is_a? Array
default_fields.map(&:to_sym)
else
[default_fields.to_sym]
end
@fields_mappings = fields_mappings.merge(@fields.reduce({}) do |mappings, field|
_table_name, field_name = field.to_s.split('.')
mappings[field_name.to_sym] = field
@@ -29,7 +36,14 @@ class TextToSqlQuery
case first_key
when :DEFAULT_COLUMN
escaped_node_value = handle_special_chars node_value
["CAST(#{@default_field.to_s} AS TEXT) ILIKE ?", "%#{escaped_node_value}%"]
query_part = '('
params_part = []
@default_fields.each do |default_field|
query_part += "CAST(#{default_field.to_s} AS TEXT) ILIKE ? OR "
params_part << "%#{escaped_node_value}%"
end
query_part = query_part[0...-4] + ')'
[query_part, *params_part]
when :OPERATOR_OR
generate_expression_for_logical_operator(:OR, node_value)
when :OPERATOR_AND