aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/arel/visitors/mysql.rb
blob: 37bfb661f052a3ae8b60d4d9bb8b77e3ec4af74e (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

module Arel # :nodoc: all
  module Visitors
    class MySQL < Arel::Visitors::ToSql
      private
        def visit_Arel_Nodes_Union(o, collector, suppress_parens = false)
          unless suppress_parens
            collector << "( "
          end

          collector =   case o.left
                        when Arel::Nodes::Union
                          visit_Arel_Nodes_Union o.left, collector, true
                        else
                          visit o.left, collector
          end

          collector << " UNION "

          collector =    case o.right
                         when Arel::Nodes::Union
                           visit_Arel_Nodes_Union o.right, collector, true
                         else
                           visit o.right, collector
          end

          if suppress_parens
            collector
          else
            collector << " )"
          end
        end

        def visit_Arel_Nodes_Bin(o, collector)
          collector << "BINARY "
          visit o.expr, collector
        end

        ###
        # :'(
        # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
        def visit_Arel_Nodes_SelectStatement(o, collector)
          if o.offset && !o.limit
            o.limit = Arel::Nodes::Limit.new(18446744073709551615)
          end
          super
        end

        def visit_Arel_Nodes_SelectCore(o, collector)
          o.froms ||= Arel.sql("DUAL")
          super
        end

        def visit_Arel_Nodes_UpdateStatement(o, collector)
          collector << "UPDATE "
          collector = visit o.relation, collector

          unless o.values.empty?
            collector << " SET "
            collector = inject_join o.values, collector, ", "
          end

          unless o.wheres.empty?
            collector << " WHERE "
            collector = inject_join o.wheres, collector, " AND "
          end

          unless o.orders.empty?
            collector << " ORDER BY "
            collector = inject_join o.orders, collector, ", "
          end

          maybe_visit o.limit, collector
        end

        def visit_Arel_Nodes_Concat(o, collector)
          collector << " CONCAT("
          visit o.left, collector
          collector << ", "
          visit o.right, collector
          collector << ") "
          collector
        end
    end
  end
end