未验证 提交 66b19b5d 编写于 作者: K Kevin Deisz

Allow #nulls_first and #nulls_last in PostgreSQL

When using PostgreSQL, it's useful to be able to specify NULLS FIRST and NULLS LAST on ordered columns. With this commit you can do that now, as in:

```ruby
User.arel_table[:first_name].desc.nulls_last
```
上级 3c28e79b
......@@ -18,6 +18,7 @@
# unary
require "arel/nodes/unary"
require "arel/nodes/grouping"
require "arel/nodes/ordering"
require "arel/nodes/ascending"
require "arel/nodes/descending"
require "arel/nodes/unqualified_column"
......
# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class Ordering < Unary
def nulls_first
NullsFirst.new(self)
end
def nulls_last
NullsLast.new(self)
end
end
class NullsFirst < Ordering; end
class NullsLast < Ordering; end
end
end
......@@ -36,7 +36,6 @@ def eql?(other)
Offset
On
OptimizerHints
Ordering
RollUp
}.each do |name|
const_set(name, Class.new(Unary))
......
......@@ -82,6 +82,16 @@ def visit_Arel_Nodes_IsDistinctFrom(o, collector)
visit o.right, collector
end
def visit_Arel_Nodes_NullsFirst(o, collector)
visit o.expr, collector
collector << " NULLS FIRST"
end
def visit_Arel_Nodes_NullsLast(o, collector)
visit o.expr, collector
collector << " NULLS LAST"
end
# Used by Lateral visitor to enclose select queries in parentheses
def grouping_parentheses(o, collector)
if o.expr.is_a? Nodes::SelectStatement
......
......@@ -315,6 +315,22 @@ def compile(node)
_(sql).must_be_like %{ "users"."name" IS DISTINCT FROM NULL }
end
end
describe "Nodes::Ordering" do
it "should handle nulls first" do
test = Table.new(:users)[:first_name].desc.nulls_first
_(compile(test)).must_be_like %{
"users"."first_name" DESC NULLS FIRST
}
end
it "should handle nulls last" do
test = Table.new(:users)[:first_name].desc.nulls_last
_(compile(test)).must_be_like %{
"users"."first_name" DESC NULLS LAST
}
end
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册