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

#relation

Instance Method Summary collapse

Methods inherited from GenericCompiler

#add_limit_on_delete, #delete_sql, #initialize, #insert_sql, #update_sql

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arel::SqlCompiler::GenericCompiler

Instance Method Details

#aliased_orders(orders) ⇒ Object



30
31
32
33
34
35
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 30

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

def select_sql
  if !orders.blank? && using_distinct_on?
    subquery = build_query \
      "SELECT #{select_clauses.kind_of?(::Array) ? select_clauses.join("") : select_clauses.to_s}",
      "FROM #{from_clauses}",
      (joins(self) unless joins(self).blank? ),
      ("WHERE #{where_clauses.join(" AND ")}" unless wheres.blank? ),
      ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ),
      ("HAVING #{having_clauses.join(', ')}" unless havings.blank? ),
      ("#{locked}" unless locked.blank? )

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

#supports_insert_with_returning?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 37

def supports_insert_with_returning?
  engine.postgresql_version >= 80200
end

#using_distinct_on?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/arel/engines/sql/compilers/postgresql_compiler.rb', line 26

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