aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/mysql.rb
blob: 3b911e826fe9749ed777efce806f3441b64ff548 (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
module Arel
  module Visitors
    class MySQL < Arel::Visitors::ToSql
      private
      def visit_Arel_Nodes_Union o, suppress_parens = false
        left_result = case o.left
                      when Arel::Nodes::Union
                        visit_Arel_Nodes_Union o.left, true
                      else
                        visit o.left
                      end

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

        if suppress_parens
          "#{left_result} UNION #{right_result}"
        else
          "( #{left_result} UNION #{right_result} )"
        end
      end

      def visit_Arel_Nodes_Bin o
        "BINARY #{visit o.expr}"
      end

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

      def visit_Arel_Nodes_SelectCore o
        o.froms ||= Arel.sql('DUAL')
        super
      end

      def visit_Arel_Nodes_UpdateStatement o
        [
          "UPDATE #{visit o.relation}",
          ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?),
          ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
          ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
          (visit(o.limit) if o.limit),
        ].compact.join ' '
      end

    end
  end
end