change LIKE to ILIKE
This commit is contained in:
@@ -5,91 +5,91 @@ class SqlGeneratorTester
|
|||||||
it 'tests simple search term without column name and without quotes' do
|
it 'tests simple search term without column name and without quotes' do
|
||||||
@query = TextToSqlQuery.new('-123', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('-123', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.device_id LIKE ?', '%-123%']
|
expect(@query.where_clause).to eq ['players.device_id ILIKE ?', '%-123%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests simple search term with column name and without quotes' do
|
it 'tests simple search term with column name and without quotes' do
|
||||||
@query = TextToSqlQuery.new('title:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.title LIKE ?', '%ab%']
|
expect(@query.where_clause).to eq ['players.title ILIKE ?', '%ab%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests simple search term with unknown column name and without quotes' do
|
it 'tests simple search term with unknown column name and without quotes' do
|
||||||
@query = TextToSqlQuery.new('unknown:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('unknown:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['players.device_id LIKE ?', '%ab%']
|
expect(@query.where_clause).to eq ['players.device_id ILIKE ?', '%ab%']
|
||||||
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 LIKE ?', '%"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 LIKE ?', '%"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
|
||||||
@query = TextToSqlQuery.new('123 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('123 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%']
|
expect(@query.where_clause).to eq ['(players.device_id ILIKE ? OR players.device_id ILIKE ?)', '%123%', '%456%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with OR operator' do
|
it 'tests search with OR operator' do
|
||||||
@query = TextToSqlQuery.new('123 or 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('123 or 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%']
|
expect(@query.where_clause).to eq ['(players.device_id ILIKE ? OR players.device_id ILIKE ?)', '%123%', '%456%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with AND operator' do
|
it 'tests search with AND operator' do
|
||||||
@query = TextToSqlQuery.new('123 and 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('123 and 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.device_id LIKE ? AND players.device_id LIKE ?)', '%123%', '%456%']
|
expect(@query.where_clause).to eq ['(players.device_id ILIKE ? AND players.device_id ILIKE ?)', '%123%', '%456%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with NOT operator on default column' do
|
it 'tests search with NOT operator on default column' do
|
||||||
@query = TextToSqlQuery.new('not 23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('not 23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['NOT players.device_id LIKE ?', '%23%']
|
expect(@query.where_clause).to eq ['NOT players.device_id ILIKE ?', '%23%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with NOT operator on non-default column' do
|
it 'tests search with NOT operator on non-default column' do
|
||||||
@query = TextToSqlQuery.new('not tag:23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('not tag:23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['NOT players.tag LIKE ?', '%23%']
|
expect(@query.where_clause).to eq ['NOT players.tag ILIKE ?', '%23%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with mixed logical operators' do
|
it 'tests search with mixed logical operators' do
|
||||||
@query = TextToSqlQuery.new('title:ab and not tag:hf-1', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:ab and not tag:hf-1', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT players.tag LIKE ?)', '%ab%', '%hf-1%']
|
expect(@query.where_clause).to eq ['(players.title ILIKE ? AND NOT players.tag ILIKE ?)', '%ab%', '%hf-1%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with mixed logical operators without NOT' do
|
it 'tests search with mixed logical operators without NOT' do
|
||||||
@query = TextToSqlQuery.new('title:a and title:b or title:c', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:a and title:b or title:c', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['((players.title LIKE ? AND players.title LIKE ?) OR players.title LIKE ?)', '%a%', '%b%', '%c%']
|
expect(@query.where_clause).to eq ['((players.title ILIKE ? AND players.title ILIKE ?) OR players.title ILIKE ?)', '%a%', '%b%', '%c%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with brackets in expression' do
|
it 'tests search with brackets in expression' do
|
||||||
@query = TextToSqlQuery.new('title:a and (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:a and (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.title LIKE ? AND (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%']
|
expect(@query.where_clause).to eq ['(players.title ILIKE ? AND (players.title ILIKE ? OR players.title ILIKE ?))', '%a%', '%b%', '%c%']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'tests search with brackets in expression and with NOT operator' do
|
it 'tests search with brackets in expression and with NOT operator' do
|
||||||
@query = TextToSqlQuery.new('title:a and not (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
@query = TextToSqlQuery.new('title:a and not (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id')
|
||||||
|
|
||||||
expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%']
|
expect(@query.where_clause).to eq ['(players.title ILIKE ? AND NOT (players.title ILIKE ? OR players.title ILIKE ?))', '%a%', '%b%', '%c%']
|
||||||
end
|
end
|
||||||
|
|
||||||
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 LIKE ?', '%"\%a\_\\"%']
|
expect(@query.where_clause).to eq ['players.title ILIKE ?', '%"\%a\_\\"%']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -26,7 +26,7 @@ class TextToSqlQuery
|
|||||||
case first_key
|
case first_key
|
||||||
when :DEFAULT_COLUMN
|
when :DEFAULT_COLUMN
|
||||||
escaped_node_value = escape_special_chars node_value
|
escaped_node_value = escape_special_chars node_value
|
||||||
["#{@default_field.to_s} LIKE ?", "%#{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)
|
||||||
when :OPERATOR_AND
|
when :OPERATOR_AND
|
||||||
@@ -48,9 +48,9 @@ class TextToSqlQuery
|
|||||||
escaped_node_value = escape_special_chars node_value
|
escaped_node_value = escape_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} LIKE ?", "%#{escaped_node_value}%"]
|
["#{@default_field.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
||||||
else
|
else
|
||||||
["#{mapping.to_s} LIKE ?", "%#{escaped_node_value}%"]
|
["#{mapping.to_s} ILIKE ?", "%#{escaped_node_value}%"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user