aboutsummaryrefslogblamecommitdiffstats
path: root/lib/arel/visitors/mysql.rb
blob: cf590598e1fb9fc5052b74f9f2d8b9acba694b82 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                       
             





                                                                      
                                             
                                                                      
                          
                                               

                         


                                   
                                              
                                                                        
                           
                                                 


                          
                   
            
                           


           

                                

         


                                                                    
                                                       


                                                                                    

             
 
                                                  



                                    
























                                                              




         
module Arel
  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
        "BINARY #{visit o.expr}"
      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(Nodes.build_quoted(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 << "SET "
          collector = inject_join o.wheres, collector, ' AND '
        end

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

        if o.limit
          collector << " "
          visit(o.limit, collector)
        else
          collector
        end
      end

    end
  end
end