blob: c423dc6fc6726f145a759600bc190e77923bef58 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
module Arel
module Visitors
class PostgreSQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Lock o
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
def visit_Arel_Nodes_DoesNotMatch o
"#{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
end
def aliased_orders orders
#orders = o.orders.map { |x| visit x }.join(', ').split(',')
list = []
orders.each_with_index do |o,i|
list <<
[
"id_list.alias_#{i}",
(o.index(/desc/i) && 'DESC')
].compact.join(' ')
end
list
end
end
end
end
|