summaryrefslogtreecommitdiff
path: root/lib/gitlab/sql/pattern.rb
blob: 46c973d8a115dc3fff424ff088f4390f2d322d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Gitlab
  module SQL
    class Pattern
      MIN_CHARS_FOR_PARTIAL_MATCHING = 3

      attr_reader :query

      def initialize(query)
        @query = query
      end

      def to_sql
        if exact_matching?
          sanitized_query
        else
          "%#{sanitized_query}%"
        end
      end

      def exact_matching?
        !partial_matching?
      end

      def partial_matching?
        @query.length >= MIN_CHARS_FOR_PARTIAL_MATCHING
      end

      def sanitized_query
        # Note: ActiveRecord::Base.sanitize_sql_like is a protected method
        ActiveRecord::Base.__send__(:sanitize_sql_like, query)
      end
    end
  end
end