summaryrefslogtreecommitdiff
path: root/lib/gitlab/sql/glob.rb
blob: 5e89e12b2b12b22ccb860bc4da7f55c43e907e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Gitlab
  module SQL
    module Glob
      extend self

      # Convert a simple glob pattern with wildcard (*) to SQL LIKE pattern
      # with SQL expression
      def to_like(pattern)
        <<~SQL
          REPLACE(REPLACE(REPLACE(#{pattern},
                                  #{q('%')}, #{q('\\%')}),
                          #{q('_')}, #{q('\\_')}),
                  #{q('*')}, #{q('%')})
        SQL
      end

      def q(string)
        ActiveRecord::Base.connection.quote(string)
      end
    end
  end
end