remove double quotes from search term
This commit is contained in:
@@ -21,15 +21,15 @@ class SqlGeneratorTester
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'tests simple search term without column name and with quotes' do
|
it 'tests simple search term without column name and with quotes' do
|
||||||
@query = TextToSqlQuery.new('"ab"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.device_id ILIKE ?', '%"ab"%']
|
expect(@query.where_clause).to eq ['players.device_id ILIKE ?', '%ab%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests simple search term with column name and with quotes' do
|
it 'tests simple search term with column name and with quotes' do
|
||||||
@query = TextToSqlQuery.new('tag:"ab"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('tag:"ab"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.tag ILIKE ?', '%"ab"%']
|
expect(@query.where_clause).to eq ['players.tag ILIKE ?', '%ab%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search without operators' do
|
it 'tests search without operators' do
|
||||||
@@ -89,7 +89,13 @@ class SqlGeneratorTester
|
|||||||
it 'tests search with special characters in search term' do
|
it 'tests search with special characters in search term' do
|
||||||
@query = TextToSqlQuery.new('title:"%a_\"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:"%a_\"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.title ILIKE ?', '%"\%a\_\\"%']
|
expect(@query.where_clause).to eq ['players.title ILIKE ?', '%\%a\_\\%']
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'tests search with field mappings' do
|
||||||
|
@query = TextToSqlQuery.new('tags:h1-r', [:'players.title', :'players.name', :'players.device_id'], :'players.device_id', { tags: "tags.name" })
|
||||||
|
|
||||||
|
expect(@query.where_clause).to eq ['tags.name ILIKE ?', '%h1-r%']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -25,7 +25,7 @@ class TextToSqlQuery
|
|||||||
node_value = tree[first_key]
|
node_value = tree[first_key]
|
||||||
case first_key
|
case first_key
|
||||||
when :DEFAULT_COLUMN
|
when :DEFAULT_COLUMN
|
||||||
escaped_node_value = escape_special_chars node_value
|
escaped_node_value = handle_special_chars node_value
|
||||||
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
||||||
when :OPERATOR_OR
|
when :OPERATOR_OR
|
||||||
generate_expression_for_logical_operator(:OR, node_value)
|
generate_expression_for_logical_operator(:OR, node_value)
|
||||||
@@ -45,7 +45,7 @@ class TextToSqlQuery
|
|||||||
|
|
||||||
else
|
else
|
||||||
# key is column name
|
# key is column name
|
||||||
escaped_node_value = escape_special_chars node_value
|
escaped_node_value = handle_special_chars node_value
|
||||||
mapping = @fields_mappings[first_key.to_sym]
|
mapping = @fields_mappings[first_key.to_sym]
|
||||||
if mapping.nil?
|
if mapping.nil?
|
||||||
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
||||||
@@ -80,8 +80,8 @@ class TextToSqlQuery
|
|||||||
["(#{first_operand_expression} #{operator.to_s} #{second_operand_expression})"] + first_operand_params + second_operand_params
|
["(#{first_operand_expression} #{operator.to_s} #{second_operand_expression})"] + first_operand_params + second_operand_params
|
||||||
end
|
end
|
||||||
|
|
||||||
def escape_special_chars(text)
|
def handle_special_chars(text)
|
||||||
result = text
|
result = text.gsub(/\"/, '')
|
||||||
result.gsub!(/\_/, '\_')
|
result.gsub!(/\_/, '\_')
|
||||||
result.tr!('\\', '\\')
|
result.tr!('\\', '\\')
|
||||||
result.gsub!(/%/, '\%')
|
result.gsub!(/%/, '\%')
|
||||||
|
|||||||
Reference in New Issue
Block a user