diff options
Diffstat (limited to 'lib/arel/nodes')
-rw-r--r-- | lib/arel/nodes/extract.rb | 23 | ||||
-rw-r--r-- | lib/arel/nodes/function.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/over.rb | 15 | ||||
-rw-r--r-- | lib/arel/nodes/select_core.rb | 4 | ||||
-rw-r--r-- | lib/arel/nodes/window.rb | 78 |
5 files changed, 120 insertions, 1 deletions
diff --git a/lib/arel/nodes/extract.rb b/lib/arel/nodes/extract.rb new file mode 100644 index 0000000000..1c9ee78816 --- /dev/null +++ b/lib/arel/nodes/extract.rb @@ -0,0 +1,23 @@ +module Arel + module Nodes + + class Extract < Arel::Nodes::Unary + include Arel::Expression + include Arel::Predications + + attr_accessor :field + attr_accessor :alias + + def initialize expr, field, aliaz = nil + super(expr) + @field = field + @alias = aliaz && SqlLiteral.new(aliaz) + end + + def as aliaz + self.alias = SqlLiteral.new(aliaz) + self + end + end + end +end diff --git a/lib/arel/nodes/function.rb b/lib/arel/nodes/function.rb index b6f6644678..5f6056a6b6 100644 --- a/lib/arel/nodes/function.rb +++ b/lib/arel/nodes/function.rb @@ -3,6 +3,7 @@ module Arel class Function < Arel::Nodes::Node include Arel::Expression include Arel::Predications + include Arel::WindowPredications attr_accessor :expressions, :alias, :distinct def initialize expr, aliaz = nil diff --git a/lib/arel/nodes/over.rb b/lib/arel/nodes/over.rb new file mode 100644 index 0000000000..21d1b5029e --- /dev/null +++ b/lib/arel/nodes/over.rb @@ -0,0 +1,15 @@ +module Arel + module Nodes + + class Over < Binary + include Arel::AliasPredication + + def initialize(left, right = nil) + super(left, right) + end + + def operator; 'OVER' end + end + + end +end
\ No newline at end of file diff --git a/lib/arel/nodes/select_core.rb b/lib/arel/nodes/select_core.rb index bee0a5930c..9b8c4a2a1f 100644 --- a/lib/arel/nodes/select_core.rb +++ b/lib/arel/nodes/select_core.rb @@ -1,7 +1,7 @@ module Arel module Nodes class SelectCore < Arel::Nodes::Node - attr_accessor :top, :projections, :wheres, :groups + attr_accessor :top, :projections, :wheres, :groups, :windows attr_accessor :having, :source, :set_quantifier def initialize @@ -14,6 +14,7 @@ module Arel @wheres = [] @groups = [] @having = nil + @windows = [] end def from @@ -34,6 +35,7 @@ module Arel @wheres = @wheres.clone @groups = @groups.clone @having = @having.clone if @having + @windows = @windows.clone end end end diff --git a/lib/arel/nodes/window.rb b/lib/arel/nodes/window.rb new file mode 100644 index 0000000000..b54eb7fe64 --- /dev/null +++ b/lib/arel/nodes/window.rb @@ -0,0 +1,78 @@ +module Arel + module Nodes + class Window < Arel::Nodes::Node + include Arel::Expression + attr_accessor :orders, :framing + + def initialize + @orders = [] + end + + def order *expr + # 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 + + def frame(expr) + raise ArgumentError, "Window frame cannot be set more than once" if @frame + @framing = expr + end + + def rows(expr = nil) + frame(Rows.new(expr)) + end + + def range(expr = nil) + frame(Range.new(expr)) + end + + def initialize_copy other + super + @orders = @orders.map { |x| x.clone } + end + end + + class NamedWindow < Window + attr_accessor :name + + def initialize name + super() + @name = name + end + + def initialize_copy other + super + @name = other.name.clone + end + 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 < Arel::Nodes::Node; end + + class Preceding < Unary + def initialize(expr = nil) + super(expr) + end + end + + class Following < Unary + def initialize(expr = nil) + super(expr) + end + end + end +end
\ No newline at end of file |