mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
adding a select statment visitor
This commit is contained in:
parent
aa4c957fa1
commit
9e9e590675
2 changed files with 29 additions and 1 deletions
|
@ -14,7 +14,8 @@ module Arel
|
|||
|
||||
def initialize_copy other
|
||||
super
|
||||
@cores = @cores.map { |x| x.clone }
|
||||
@cores = @cores.map { |x| x.clone }
|
||||
@orders = @orders.map { |x| x.clone }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,33 @@
|
|||
module Arel
|
||||
module Visitors
|
||||
class PostgreSQL < Arel::Visitors::ToSql
|
||||
private
|
||||
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 #{o.orders.map { |x| visit x }.join(', ')}",
|
||||
("LIMIT #{o.limit}" if o.limit),
|
||||
(visit(o.offset) if o.offset),
|
||||
].compact.join ' '
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def using_distinct_on?(o)
|
||||
o.cores.any? do |core|
|
||||
core.projections.any? do |projection|
|
||||
/DISTINCT ON/ === projection
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue