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/arel/visitors | |
parent | cae83ce964b9919b890bb7fa6f920a536e6b5425 (diff) | |
download | rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.tar.gz rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.tar.bz2 rails-0b9af9762a5f3431f83a9bba6919fef9346e310a.zip |
adding Distinct ON node
Diffstat (limited to 'lib/arel/visitors')
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 27 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 |
2 files changed, 4 insertions, 27 deletions
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), |