aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/postgresql.rb
blob: 1ef0261bdde2c9e02fbf3669aca79bff84365d1c (plain) (blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Arel
  module Visitors
    class PostgreSQL < Arel::Visitors::ToSql
      private

      def visit_Arel_Nodes_Matches o, collector
        op = o.case_sensitive ? ' LIKE ' : ' ILIKE '
        collector = infix_value o, collector, op
        if o.escape
          collector << ' ESCAPE '
          visit o.escape, collector
        else
          collector
        end
      end

      def visit_Arel_Nodes_DoesNotMatch o, collector
        op = o.case_sensitive ? ' NOT LIKE ' : ' NOT ILIKE '
        collector = infix_value o, collector, op
        if o.escape
          collector << ' ESCAPE '
          visit o.escape, collector
        else
          collector
        end
      end

      def visit_Arel_Nodes_Regexp o, collector
        op = o.case_sensitive ? ' ~ ' : ' ~* '
        infix_value o, collector, op
      end

      def visit_Arel_Nodes_NotRegexp o, collector
        op = o.case_sensitive ? ' !~ ' : ' !~* '
        infix_value o, collector, op
      end

      def visit_Arel_Nodes_DistinctOn o, collector
        collector << "DISTINCT ON ( "
        visit(o.expr, collector) << " )"
      end

      def visit_Arel_Nodes_BindParam o, collector
        collector.add_bind(o) { |i| "$#{i}" }
      end
    end
  end
end