diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-21 15:46:24 -0500 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-21 15:46:24 -0500 |
commit | 0b9af9762a5f3431f83a9bba6919fef9346e310a (patch) | |
tree | 7731a00a00ff0588835fe2e7f7164be0a4264116 /lib | |
parent | cae83ce964b9919b890bb7fa6f920a536e6b5425 (diff) | |
download | rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.tar.gz rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.tar.bz2 rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.zip |
adding Distinct ON node
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/nodes.rb | 4 | ||||
-rw-r--r-- | lib/arel/nodes/terminal.rb | 6 | ||||
-rw-r--r-- | lib/arel/nodes/unary.rb | 7 | ||||
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 27 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 |
5 files changed, 15 insertions, 33 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index cf2aeb2ddc..9576930a54 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -5,6 +5,10 @@ require 'arel/nodes/select_core' require 'arel/nodes/insert_statement' require 'arel/nodes/update_statement' +# terminal + +require 'arel/nodes/terminal' + # unary require 'arel/nodes/unary' require 'arel/nodes/unqualified_column' diff --git a/lib/arel/nodes/terminal.rb b/lib/arel/nodes/terminal.rb new file mode 100644 index 0000000000..c6b4f4e1e2 --- /dev/null +++ b/lib/arel/nodes/terminal.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class Distinct < Arel::Nodes::Node + end + end +end diff --git a/lib/arel/nodes/unary.rb b/lib/arel/nodes/unary.rb index e6e40e6b13..5c4add4792 100644 --- a/lib/arel/nodes/unary.rb +++ b/lib/arel/nodes/unary.rb @@ -20,14 +20,9 @@ module Arel On Top Lock + DistinctOn }.each do |name| const_set(name, Class.new(Unary)) end - - class Distinct < Unary - def initialize expr = nil - super - end - end end end diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index c423dc6fc6..377a65a216 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -6,25 +6,6 @@ module Arel visit o.expr end - def visit_Arel_Nodes_SelectStatement o - if !o.orders.empty? && using_distinct_on?(o) - subquery = o.dup - subquery.orders = [] - subquery.limit = nil - subquery.offset = nil - - sql = super(subquery) - [ - "SELECT * FROM (#{sql}) AS id_list", - "ORDER BY #{aliased_orders(o.orders).join(', ')}", - (visit(o.limit) if o.limit), - (visit(o.offset) if o.offset), - ].compact.join ' ' - else - super - end - end - def visit_Arel_Nodes_Matches o "#{visit o.left} ILIKE #{visit o.right}" end @@ -33,12 +14,8 @@ module Arel "#{visit o.left} NOT ILIKE #{visit o.right}" end - def using_distinct_on?(o) - o.cores.any? do |core| - core.projections.any? do |projection| - /DISTINCT ON/ === projection - end - end + def visit_Arel_Nodes_DistinctOn o + "DISTINCT ON ( #{visit o.expr} )" end def aliased_orders orders diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 6aba31d94d..5319aeb418 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -126,8 +126,8 @@ 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), + ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?), + (visit(o.source) if o.source), ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?), ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?), (visit(o.having) if o.having), |