aboutsummaryrefslogblamecommitdiffstats
path: root/lib/arel/algebra/relations/operations/order.rb
blob: d91e400419e553e474b29346d7a62a29d9ec5858 (plain) (tree)
1
2
3
4
5
6
7
8
9
           
                        
                          
 
                                       
                     
                                                             

       






                                     
            
              

                                                                                              
       
 





                                                                               
     
   
module Arel
  class Order < Compound
    attr_reader :orderings

    def initialize(relation, orderings)
      super(relation)
      @orderings = orderings.collect { |o| o.bind(relation) }
    end

    def == other
      super ||
        Order === other &&
        relation == other.relation &&
        orderings == other.orderings
    end

    # TESTME
    def orders
      # QUESTION - do we still need relation.orders ?
      (orderings + relation.orders).collect { |o| o.bind(self) }.collect { |o| o.to_ordering }
    end

    def eval
      unoperated_rows.sort do |row1, row2|
        ordering = orders.detect { |o| o.eval(row1, row2) != 0 } || orders.last
        ordering.eval(row1, row2)
      end
    end
  end
end