forked from rails/arel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ANSI SQL2003 window functions.
- Loading branch information
Alexander Staubo
committed
Feb 22, 2012
1 parent
6e427e5
commit a1a6fbc
Showing
14 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Arel | ||
module Nodes | ||
|
||
class Over < Binary | ||
def initialize(left, right = nil) | ||
super(left, right) | ||
end | ||
|
||
def operator; 'OVER' end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Arel | ||
module WindowPredications | ||
|
||
def over(expr = nil) | ||
Nodes::Over.new(self, expr) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'helper' | ||
|
||
describe Arel::Nodes::Over do | ||
describe 'with literal' do | ||
it 'should reference the window definition by name' do | ||
table = Arel::Table.new :users | ||
table[:id].count.over('foo').to_sql.must_be_like %{ | ||
COUNT("users"."id") OVER "foo" | ||
} | ||
end | ||
end | ||
|
||
describe 'with SQL literal' do | ||
it 'should reference the window definition by name' do | ||
table = Arel::Table.new :users | ||
table[:id].count.over(Arel.sql('foo')).to_sql.must_be_like %{ | ||
COUNT("users"."id") OVER foo | ||
} | ||
end | ||
end | ||
|
||
describe 'with no expression' do | ||
it 'should use empty definition' do | ||
table = Arel::Table.new :users | ||
table[:id].count.over.to_sql.must_be_like %{ | ||
COUNT("users"."id") OVER () | ||
} | ||
end | ||
end | ||
|
||
describe 'with expression' do | ||
it 'should use definition in sub-expression' do | ||
table = Arel::Table.new :users | ||
window = Arel::Nodes::Window.new.order(table['foo']) | ||
table[:id].count.over(window).to_sql.must_be_like %{ | ||
COUNT("users"."id") OVER (ORDER BY \"users\".\"foo\") | ||
} | ||
end | ||
end | ||
end |
Oops, something went wrong.