mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
adding a DISTINCT node
This commit is contained in:
parent
885a3acb1c
commit
cae83ce964
4 changed files with 44 additions and 21 deletions
|
@ -2,15 +2,18 @@ module Arel
|
|||
module Nodes
|
||||
class SelectCore < Arel::Nodes::Node
|
||||
attr_accessor :top, :projections, :wheres, :groups
|
||||
attr_accessor :having, :source
|
||||
attr_accessor :having, :source, :set_quantifier
|
||||
|
||||
def initialize
|
||||
@source = JoinSource.new nil
|
||||
@top = nil
|
||||
@projections = []
|
||||
@wheres = []
|
||||
@groups = []
|
||||
@having = nil
|
||||
@source = JoinSource.new nil
|
||||
@top = nil
|
||||
|
||||
# http://savage.net.au/SQL/sql-92.bnf.html#set%20quantifier
|
||||
@set_quantifier = nil
|
||||
@projections = []
|
||||
@wheres = []
|
||||
@groups = []
|
||||
@having = nil
|
||||
end
|
||||
|
||||
def from
|
||||
|
|
|
@ -23,5 +23,11 @@ module Arel
|
|||
}.each do |name|
|
||||
const_set(name, Class.new(Unary))
|
||||
end
|
||||
|
||||
class Distinct < Unary
|
||||
def initialize expr = nil
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -125,6 +125,7 @@ key on UpdateManager using UpdateManager#key=
|
|||
[
|
||||
"SELECT",
|
||||
(visit(o.top) if o.top),
|
||||
(visit(o.set_quantifier) if o.set_quantifier),
|
||||
"#{o.projections.map { |x| visit x }.join ', '}",
|
||||
visit(o.source),
|
||||
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
|
||||
|
@ -137,6 +138,10 @@ key on UpdateManager using UpdateManager#key=
|
|||
visit o.expr
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Distinct o
|
||||
'DISTINCT'
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_With o
|
||||
"WITH #{o.children.map { |x| visit x }.join(', ')}"
|
||||
end
|
||||
|
|
|
@ -1,22 +1,31 @@
|
|||
require 'helper'
|
||||
|
||||
describe Arel::Nodes::SelectCore do
|
||||
describe "#clone" do
|
||||
it "clones froms, projections and wheres" do
|
||||
core = Arel::Nodes::SelectCore.new
|
||||
core.froms = %w[a b c]
|
||||
core.projections = %w[d e f]
|
||||
core.wheres = %w[g h i]
|
||||
module Arel
|
||||
module Nodes
|
||||
class TestSelectCore < MiniTest::Unit::TestCase
|
||||
def test_clone
|
||||
core = Arel::Nodes::SelectCore.new
|
||||
core.froms = %w[a b c]
|
||||
core.projections = %w[d e f]
|
||||
core.wheres = %w[g h i]
|
||||
|
||||
dolly = core.clone
|
||||
dolly = core.clone
|
||||
|
||||
dolly.froms.must_equal core.froms
|
||||
dolly.projections.must_equal core.projections
|
||||
dolly.wheres.must_equal core.wheres
|
||||
dolly.froms.must_equal core.froms
|
||||
dolly.projections.must_equal core.projections
|
||||
dolly.wheres.must_equal core.wheres
|
||||
|
||||
dolly.froms.wont_be_same_as core.froms
|
||||
dolly.projections.wont_be_same_as core.projections
|
||||
dolly.wheres.wont_be_same_as core.wheres
|
||||
dolly.froms.wont_be_same_as core.froms
|
||||
dolly.projections.wont_be_same_as core.projections
|
||||
dolly.wheres.wont_be_same_as core.wheres
|
||||
end
|
||||
|
||||
def test_set_quantifier
|
||||
core = Arel::Nodes::SelectCore.new
|
||||
core.set_quantifier = Arel::Nodes::Distinct.new
|
||||
viz = Arel::Visitors::ToSql.new Table.engine
|
||||
assert_match 'DISTINCT', viz.accept(core)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue