From 0b9af9762a5f3431f83a9bba6919fef9346e310a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 21 Apr 2011 15:46:24 -0500 Subject: adding Distinct ON node --- lib/arel/nodes.rb | 4 ++++ lib/arel/nodes/terminal.rb | 6 ++++++ lib/arel/nodes/unary.rb | 7 +------ lib/arel/visitors/postgresql.rb | 27 ++------------------------- lib/arel/visitors/to_sql.rb | 4 ++-- 5 files changed, 15 insertions(+), 33 deletions(-) create mode 100644 lib/arel/nodes/terminal.rb (limited to 'lib') 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), -- cgit v1.2.3