remove double quotes from search term
This commit is contained in:
@@ -21,15 +21,15 @@ class SqlGeneratorTester
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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')
|
||||
|
||||
expect(@query.where_clause).to eq ['players.tag ILIKE ?', '%"ab"%']
|
||||
expect(@query.where_clause).to eq ['players.tag ILIKE ?', '%ab%']
|
||||
end
|
||||
|
||||
it 'tests search without operators' do
|
||||
@@ -89,7 +89,13 @@ class SqlGeneratorTester
|
||||
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')
|
||||
|
||||
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
|
||||
@@ -25,7 +25,7 @@ class TextToSqlQuery
|
||||
node_value = tree[first_key]
|
||||
case first_key
|
||||
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}%"]
|
||||
when :OPERATOR_OR
|
||||
generate_expression_for_logical_operator(:OR, node_value)
|
||||
@@ -45,7 +45,7 @@ class TextToSqlQuery
|
||||
|
||||
else
|
||||
# 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]
|
||||
if mapping.nil?
|
||||
["#{@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
|
||||
end
|
||||
|
||||
def escape_special_chars(text)
|
||||
result = text
|
||||
def handle_special_chars(text)
|
||||
result = text.gsub(/\"/, '')
|
||||
result.gsub!(/\_/, '\_')
|
||||
result.tr!('\\', '\\')
|
||||
result.gsub!(/%/, '\%')
|
||||
|
||||
Reference in New Issue
Block a user