like_with_wildcard has been added

This commit is contained in:
Alexander Meindl 2023-11-19 12:36:23 +01:00
parent a009f37155
commit 33cd34842b
2 changed files with 42 additions and 0 deletions

View File

@ -60,6 +60,32 @@ module Additionals
"JOIN #{::EnabledModule.table_name} ON #{::EnabledModule.table_name}.project_id=#{table_name}.project_id" \
" AND #{::EnabledModule.table_name}.name='#{module_name}'"
end
def like_pattern(value, wildcard = nil)
cleaned_value = sanitize_sql_like value.to_s.strip
return cleaned_value if wildcard.nil? || wildcard == :none
case wildcard
when :both
"%#{cleaned_value}%"
when :left
"%#{cleaned_value}"
when :right
"#{cleaned_value}%"
else
raise 'unsupported wildcard rule'
end
end
def like_with_wildcard(columns:, value:, wildcard: :none)
sql = []
Array(columns).each do |column|
sql << "LOWER(#{column}) LIKE LOWER(:p) ESCAPE :s"
end
sql_string = sql.join ' OR '
where sql_string, p: like_pattern(value, wildcard), s: '\\'
end
end
module InstanceMethods

View File

@ -49,4 +49,20 @@ class EntityMethodTest < Additionals::TestCase
assert_sorted_equal [1, 2, 3], projects.ids
end
def test_like_pattern
assert_equal 'ss', Wiki.like_pattern(' ss ')
assert_equal 'ss%', Wiki.like_pattern('ss', :right)
assert_equal '%ss', Wiki.like_pattern('ss', :left)
assert_equal '%ss%', Wiki.like_pattern('ss', :both)
assert_equal 'ss', Wiki.like_pattern('ss', :none)
end
def test_like_with_wildcard
assert_empty Wiki.like_with_wildcard(columns: :start_page, value: 'nothing')
end
def test_like_with_wildcard_with_empty_value
assert_empty Wiki.like_with_wildcard(columns: :start_page, value: '')
end
end