2017-08-22 12:51:53 -04:00
|
|
|
module Gitlab
|
|
|
|
module SQL
|
2017-08-28 18:14:41 -04:00
|
|
|
module Pattern
|
|
|
|
extend ActiveSupport::Concern
|
2017-08-22 12:51:53 -04:00
|
|
|
|
2017-08-28 18:14:41 -04:00
|
|
|
MIN_CHARS_FOR_PARTIAL_MATCHING = 3
|
2017-08-23 06:54:14 -04:00
|
|
|
REGEX_QUOTED_WORD = /(?<=^| )"[^"]+"(?= |$)/
|
2017-08-22 12:51:53 -04:00
|
|
|
|
2017-08-28 18:14:41 -04:00
|
|
|
class_methods do
|
|
|
|
def to_pattern(query)
|
2017-08-29 05:00:03 -04:00
|
|
|
if partial_matching?(query)
|
2017-08-28 18:14:41 -04:00
|
|
|
"%#{sanitize_sql_like(query)}%"
|
2017-08-29 05:00:03 -04:00
|
|
|
else
|
|
|
|
sanitize_sql_like(query)
|
2017-08-28 18:14:41 -04:00
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
|
2017-08-28 18:14:41 -04:00
|
|
|
def partial_matching?(query)
|
|
|
|
query.length >= MIN_CHARS_FOR_PARTIAL_MATCHING
|
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
|
|
|
|
def to_fuzzy_arel(column, query)
|
|
|
|
words = select_fuzzy_words(query)
|
|
|
|
|
|
|
|
matches = words.map { |word| arel_table[column].matches(to_pattern(word)) }
|
|
|
|
|
|
|
|
matches.reduce { |result, match| result.and(match) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_fuzzy_words(query)
|
|
|
|
quoted_words = query.scan(REGEX_QUOTED_WORD)
|
|
|
|
|
|
|
|
query = quoted_words.reduce(query) { |q, quoted_word| q.sub(quoted_word, '') }
|
|
|
|
|
|
|
|
words = query.split(/\s+/)
|
|
|
|
|
|
|
|
quoted_words.map! { |quoted_word| quoted_word[1..-2] }
|
|
|
|
|
|
|
|
words.concat(quoted_words)
|
|
|
|
|
|
|
|
words.select { |word| partial_matching?(word) }
|
|
|
|
end
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|