2017-02-13 13:58:58 -05:00
|
|
|
# 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
|
2013-02-19 16:27:17 -05:00
|
|
|
attr_accessor :orders, :framing, :partitions
|
2012-02-22 09:25:10 -05:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@orders = []
|
2013-02-19 16:27:17 -05:00
|
|
|
@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)
|
2013-02-19 16:27:17 -05:00
|
|
|
# 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)
|
2013-02-19 16:28:16 -05:00
|
|
|
if @framing
|
|
|
|
Rows.new(expr)
|
|
|
|
else
|
|
|
|
frame(Rows.new(expr))
|
|
|
|
end
|
2012-02-22 09:25:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def range(expr = nil)
|
2013-02-19 16:28:16 -05:00
|
|
|
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
|
2012-08-18 22:33:25 -04:00
|
|
|
|
|
|
|
def hash
|
|
|
|
[@orders, @framing].hash
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eql?(other)
|
2012-08-18 22:33:25 -04:00
|
|
|
self.class == other.class &&
|
|
|
|
self.orders == other.orders &&
|
2013-02-19 16:27:17 -05:00
|
|
|
self.framing == other.framing &&
|
|
|
|
self.partitions == other.partitions
|
2012-08-18 22:33:25 -04:00
|
|
|
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
|
2012-08-18 22:33:25 -04:00
|
|
|
|
|
|
|
def hash
|
|
|
|
super ^ @name.hash
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eql?(other)
|
2012-08-18 22:33:25 -04:00
|
|
|
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
|
|
|
|
|
2012-08-18 22:33:25 -04:00
|
|
|
class CurrentRow < Node
|
|
|
|
def hash
|
|
|
|
self.class.hash
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eql?(other)
|
2012-08-18 22:33:25 -04:00
|
|
|
self.class == other.class
|
|
|
|
end
|
2017-07-25 09:12:11 -04:00
|
|
|
alias :== :eql?
|
2012-08-18 22:33:25 -04:00
|
|
|
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
|
2012-07-13 06:09:47 -04:00
|
|
|
end
|