1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/nodes/window.rb

127 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-24 01:45:50 -05:00
2018-02-24 02:41:47 -05:00
module Arel # :nodoc: all
2012-02-22 09:25:10 -05:00
module Nodes
class Window < Arel::Nodes::Node
attr_accessor :orders, :framing, :partitions
2012-02-22 09:25:10 -05:00
def initialize
@orders = []
@partitions = []
@framing = nil
2012-02-22 09:25:10 -05:00
end
2018-02-24 01:45:50 -05:00
def order(*expr)
2012-02-22 09:25:10 -05:00
# FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
@orders.concat expr.map { |x|
String === x || Symbol === x ? Nodes::SqlLiteral.new(x.to_s) : x
}
self
end
2018-02-24 01:45:50 -05:00
def partition(*expr)
# FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
@partitions.concat expr.map { |x|
String === x || Symbol === x ? Nodes::SqlLiteral.new(x.to_s) : x
}
self
end
2012-02-22 09:25:10 -05:00
def frame(expr)
@framing = expr
end
def rows(expr = nil)
if @framing
Rows.new(expr)
else
frame(Rows.new(expr))
end
2012-02-22 09:25:10 -05:00
end
def range(expr = nil)
if @framing
Range.new(expr)
else
frame(Range.new(expr))
end
2012-02-22 09:25:10 -05:00
end
2018-02-24 01:45:50 -05:00
def initialize_copy(other)
2012-02-22 09:25:10 -05:00
super
@orders = @orders.map { |x| x.clone }
end
def hash
[@orders, @framing].hash
end
2018-02-24 01:45:50 -05:00
def eql?(other)
self.class == other.class &&
self.orders == other.orders &&
self.framing == other.framing &&
self.partitions == other.partitions
end
alias :== :eql?
2012-02-22 09:25:10 -05:00
end
class NamedWindow < Window
attr_accessor :name
2018-02-24 01:45:50 -05:00
def initialize(name)
2012-02-22 09:25:10 -05:00
super()
@name = name
end
2018-02-24 01:45:50 -05:00
def initialize_copy(other)
2012-02-22 09:25:10 -05:00
super
@name = other.name.clone
end
def hash
super ^ @name.hash
end
2018-02-24 01:45:50 -05:00
def eql?(other)
super && self.name == other.name
end
alias :== :eql?
2012-02-22 09:25:10 -05:00
end
class Rows < Unary
def initialize(expr = nil)
super(expr)
end
end
class Range < Unary
def initialize(expr = nil)
super(expr)
end
end
class CurrentRow < Node
def hash
self.class.hash
end
2018-02-24 01:45:50 -05:00
def eql?(other)
self.class == other.class
end
alias :== :eql?
end
2012-02-22 09:25:10 -05:00
class Preceding < Unary
def initialize(expr = nil)
super(expr)
end
end
class Following < Unary
def initialize(expr = nil)
super(expr)
end
end
end
end