2016-08-31 11:20:36 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-09-23 14:45:29 -04:00
|
|
|
module Visitors
|
|
|
|
class PostgreSQL < Arel::Visitors::ToSql
|
2018-02-24 01:45:50 -05:00
|
|
|
CUBE = "CUBE"
|
|
|
|
ROLLUP = "ROLLUP"
|
2018-05-28 06:11:57 -04:00
|
|
|
GROUPING_SETS = "GROUPING SETS"
|
2018-02-24 01:45:50 -05:00
|
|
|
LATERAL = "LATERAL"
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2010-09-23 16:07:48 -04:00
|
|
|
private
|
2010-10-13 16:23:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
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
|
2014-12-24 22:58:42 -05:00
|
|
|
end
|
2010-09-29 15:50:24 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
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
|
2014-12-24 22:58:42 -05:00
|
|
|
end
|
2010-09-29 15:50:24 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Regexp(o, collector)
|
|
|
|
op = o.case_sensitive ? " ~ " : " ~* "
|
|
|
|
infix_value o, collector, op
|
|
|
|
end
|
2014-03-07 08:15:33 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_NotRegexp(o, collector)
|
|
|
|
op = o.case_sensitive ? " !~ " : " !~* "
|
|
|
|
infix_value o, collector, op
|
|
|
|
end
|
2014-03-07 08:15:33 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_DistinctOn(o, collector)
|
|
|
|
collector << "DISTINCT ON ( "
|
|
|
|
visit(o.expr, collector) << " )"
|
|
|
|
end
|
2014-11-17 17:52:38 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_BindParam(o, collector)
|
|
|
|
collector.add_bind(o.value) { |i| "$#{i}" }
|
|
|
|
end
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_GroupingElement(o, collector)
|
|
|
|
collector << "( "
|
|
|
|
visit(o.expr, collector) << " )"
|
|
|
|
end
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Cube(o, collector)
|
|
|
|
collector << CUBE
|
|
|
|
grouping_array_or_grouping_element o, collector
|
|
|
|
end
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_RollUp(o, collector)
|
|
|
|
collector << ROLLUP
|
|
|
|
grouping_array_or_grouping_element o, collector
|
|
|
|
end
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_GroupingSet(o, collector)
|
2018-05-28 06:11:57 -04:00
|
|
|
collector << GROUPING_SETS
|
2018-02-24 01:45:50 -05:00
|
|
|
grouping_array_or_grouping_element o, collector
|
|
|
|
end
|
2016-05-03 22:15:06 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Lateral(o, collector)
|
|
|
|
collector << LATERAL
|
|
|
|
collector << SPACE
|
|
|
|
grouping_parentheses o, collector
|
|
|
|
end
|
2017-04-25 00:41:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
# 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
|
2017-04-25 00:41:52 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
# 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
|
2016-05-03 22:15:06 -04:00
|
|
|
end
|
2010-09-23 14:45:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|