Class: Arel::SqlCompiler::PostgreSQLCompiler

Inherits:
GenericCompiler show all
Defined in:
lib/arel/engines/sql/compilers/postgresql_compiler.rb

Instance Attribute Summary

Attributes inherited from GenericCompiler

#engine, #relation

Instance Method Summary collapse

Methods inherited from GenericCompiler

#add_limit_on_delete, #build_clauses, #christener, #delete_sql, #initialize, #insert_sql, #update_sql

Constructor Details

This class inherits a constructor from Arel::SqlCompiler::GenericCompiler

Instance Method Details

#aliased_orders(orders) ⇒ Object



38
39
40
41
42
43
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 38

def aliased_orders(orders)
  # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this
  # by wrapping the +sql+ string as a sub-select and ordering in that query.
  order = orders.join(', ').split(/,/).map { |s| s.strip }.reject(&:blank?)
  order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{'DESC' if s =~ /\bdesc$/i}" }.join(', ')
end

#select_sqlObject



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
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 5

def select_sql
  if !relation.orders.blank? && using_distinct_on?
    selects = relation.select_clauses
    joins   = relation.joins(self)
    wheres  = relation.where_clauses
    groups  = relation.group_clauses
    havings = relation.having_clauses
    orders  = relation.order_clauses

    subquery_clauses = [ "",
      "SELECT     #{selects.kind_of?(::Array) ? selects.join("") : selects.to_s}",
      "FROM       #{relation.from_clauses}",
      joins,
      ("WHERE     #{wheres.join(' AND ')}" unless wheres.empty?),
      ("GROUP BY  #{groups.join(', ')}" unless groups.empty?),
      ("HAVING    #{havings.join(' AND ')}" unless havings.empty?)
    ].compact.join ' '
    subquery_clauses << " #{locked}" unless locked.blank?

    build_query \
      "SELECT * FROM (#{build_query subquery_clauses}) AS id_list",
      "ORDER BY #{aliased_orders(orders)}",
      ("LIMIT #{relation.taken}" unless relation.taken.blank? ),
      ("OFFSET #{relation.skipped}" unless relation.skipped.blank? )
  else
    super
  end
end

#supports_insert_with_returning?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 45

def supports_insert_with_returning?
  engine.connection.send(:postgresql_version) >= 80200
end

#using_distinct_on?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 34

def using_distinct_on?
  relation.select_clauses.any? { |x| x =~ /DISTINCT ON/ }
end