Files

Class Index [+]

Quicksearch

Arel::SqlCompiler::PostgreSQLCompiler

Public Instance Methods

aliased_orders(orders) click to toggle source
    # File lib/arel/engines/sql/compilers/postgresql_compiler.rb, line 38
38:       def aliased_orders(orders)
39:         # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this
40:         # by wrapping the +sql+ string as a sub-select and ordering in that query.
41:         order = orders.join(', ').split(/,/).map { |s| s.strip }.reject(&:blank?)
42:         order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{'DESC' if s =~ /\bdesc$/i}" }.join(', ')
43:       end
select_sql() click to toggle source
    # File lib/arel/engines/sql/compilers/postgresql_compiler.rb, line 5
 5:       def select_sql
 6:         if !relation.orders.blank? && using_distinct_on?
 7:           selects = relation.select_clauses
 8:           joins   = relation.joins(self)
 9:           wheres  = relation.where_clauses
10:           groups  = relation.group_clauses
11:           havings = relation.having_clauses
12:           orders  = relation.order_clauses
13: 
14:           subquery_clauses = [ "",
15:             "SELECT     #{selects.kind_of?(::Array) ? selects.join("") : selects.to_s}",
16:             "FROM       #{relation.from_clauses}",
17:             joins,
18:             ("WHERE     #{wheres.join(' AND ')}" unless wheres.empty?),
19:             ("GROUP BY  #{groups.join(', ')}" unless groups.empty?),
20:             ("HAVING    #{havings.join(' AND ')}" unless havings.empty?)
21:           ].compact.join ' '
22:           subquery_clauses << " #{locked}" unless locked.blank?
23: 
24:           build_query              "SELECT * FROM (#{build_query subquery_clauses}) AS id_list",
25:             "ORDER BY #{aliased_orders(orders)}",
26:             ("LIMIT #{relation.taken}" unless relation.taken.blank? ),
27:             ("OFFSET #{relation.skipped}" unless relation.skipped.blank? )
28:         else
29:           super
30:         end
31:       end
supports_insert_with_returning?() click to toggle source
    # File lib/arel/engines/sql/compilers/postgresql_compiler.rb, line 45
45:       def supports_insert_with_returning?
46:         engine.connection.send(:postgresql_version) >= 80200
47:       end
using_distinct_on?() click to toggle source
    # File lib/arel/engines/sql/compilers/postgresql_compiler.rb, line 34
34:       def using_distinct_on?
35:         relation.select_clauses.any? { |x| x =~ /DISTINCT ON/ }
36:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.