1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/visitors/postgresql.rb
Kevin Deisz 66b19b5dec
Allow #nulls_first and #nulls_last in PostgreSQL
When using PostgreSQL, it's useful to be able to specify NULLS FIRST and NULLS LAST on ordered columns. With this commit you can do that now, as in:

```ruby
User.arel_table[:first_name].desc.nulls_last
```
2019-12-31 15:59:59 -05:00

119 lines
3.4 KiB
Ruby

# frozen_string_literal: true
module Arel # :nodoc: all
module Visitors
class PostgreSQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Matches(o, collector)
op = o.case_sensitive ? " LIKE " : " ILIKE "
collector = infix_value o, collector, op
if o.escape
collector << " ESCAPE "
visit o.escape, collector
else
collector
end
end
def visit_Arel_Nodes_DoesNotMatch(o, collector)
op = o.case_sensitive ? " NOT LIKE " : " NOT ILIKE "
collector = infix_value o, collector, op
if o.escape
collector << " ESCAPE "
visit o.escape, collector
else
collector
end
end
def visit_Arel_Nodes_Regexp(o, collector)
op = o.case_sensitive ? " ~ " : " ~* "
infix_value o, collector, op
end
def visit_Arel_Nodes_NotRegexp(o, collector)
op = o.case_sensitive ? " !~ " : " !~* "
infix_value o, collector, op
end
def visit_Arel_Nodes_DistinctOn(o, collector)
collector << "DISTINCT ON ( "
visit(o.expr, collector) << " )"
end
def visit_Arel_Nodes_BindParam(o, collector)
collector.add_bind(o.value) { |i| "$#{i}" }
end
def visit_Arel_Nodes_GroupingElement(o, collector)
collector << "( "
visit(o.expr, collector) << " )"
end
def visit_Arel_Nodes_Cube(o, collector)
collector << "CUBE"
grouping_array_or_grouping_element o, collector
end
def visit_Arel_Nodes_RollUp(o, collector)
collector << "ROLLUP"
grouping_array_or_grouping_element o, collector
end
def visit_Arel_Nodes_GroupingSet(o, collector)
collector << "GROUPING SETS"
grouping_array_or_grouping_element o, collector
end
def visit_Arel_Nodes_Lateral(o, collector)
collector << "LATERAL "
grouping_parentheses o, collector
end
def visit_Arel_Nodes_IsNotDistinctFrom(o, collector)
collector = visit o.left, collector
collector << " IS NOT DISTINCT FROM "
visit o.right, collector
end
def visit_Arel_Nodes_IsDistinctFrom(o, collector)
collector = visit o.left, collector
collector << " IS DISTINCT FROM "
visit o.right, collector
end
def visit_Arel_Nodes_NullsFirst(o, collector)
visit o.expr, collector
collector << " NULLS FIRST"
end
def visit_Arel_Nodes_NullsLast(o, collector)
visit o.expr, collector
collector << " NULLS LAST"
end
# Used by Lateral visitor to enclose select queries in parentheses
def grouping_parentheses(o, collector)
if o.expr.is_a? Nodes::SelectStatement
collector << "("
visit o.expr, collector
collector << ")"
else
visit o.expr, collector
end
end
# Utilized by GroupingSet, Cube & RollUp visitors to
# handle grouping aggregation semantics
def grouping_array_or_grouping_element(o, collector)
if o.expr.is_a? Array
collector << "( "
visit o.expr, collector
collector << " )"
else
visit o.expr, collector
end
end
end
end
end