2017-02-13 13:58:58 -05: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-10-27 09:43:20 -04:00
|
|
|
module Predications
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_eq(other)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::NotEqual.new self, quoted_node(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_eq_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :not_eq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_eq_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :not_eq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eq(other)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::Equality.new self, quoted_node(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eq_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :eq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eq_all(others)
|
2014-10-27 18:58:05 -04:00
|
|
|
grouping_all :eq, quoted_array(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def between(other)
|
2014-12-26 17:45:34 -05:00
|
|
|
if equals_quoted?(other.begin, -Float::INFINITY)
|
|
|
|
if equals_quoted?(other.end, Float::INFINITY)
|
2014-10-25 08:38:56 -04:00
|
|
|
not_in([])
|
|
|
|
elsif other.exclude_end?
|
|
|
|
lt(other.end)
|
|
|
|
else
|
|
|
|
lteq(other.end)
|
|
|
|
end
|
2014-12-26 17:45:34 -05:00
|
|
|
elsif equals_quoted?(other.end, Float::INFINITY)
|
2014-10-25 08:38:56 -04:00
|
|
|
gteq(other.begin)
|
|
|
|
elsif other.exclude_end?
|
|
|
|
gteq(other.begin).and(lt(other.end))
|
|
|
|
else
|
2014-10-27 18:58:05 -04:00
|
|
|
left = quoted_node(other.begin)
|
|
|
|
right = quoted_node(other.end)
|
2014-10-25 08:38:56 -04:00
|
|
|
Nodes::Between.new(self, left.and(right))
|
|
|
|
end
|
2014-10-25 08:24:18 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def in(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
case other
|
|
|
|
when Arel::SelectManager
|
2010-12-07 13:46:30 -05:00
|
|
|
Arel::Nodes::In.new(self, other.ast)
|
2010-10-27 09:43:20 -04:00
|
|
|
when Range
|
2014-10-25 08:38:56 -04:00
|
|
|
if $VERBOSE
|
|
|
|
warn <<-eowarn
|
|
|
|
Passing a range to `#in` is deprecated. Call `#between`, instead.
|
|
|
|
eowarn
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
2014-10-25 08:38:56 -04:00
|
|
|
between(other)
|
2014-11-02 14:00:04 -05:00
|
|
|
when Enumerable
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::In.new self, quoted_array(other)
|
2014-03-24 20:18:56 -04:00
|
|
|
else
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::In.new self, quoted_node(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def in_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :in, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def in_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :in, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_between(other)
|
2014-12-26 17:45:34 -05:00
|
|
|
if equals_quoted?(other.begin, -Float::INFINITY)
|
|
|
|
if equals_quoted?(other.end, Float::INFINITY)
|
2014-10-25 08:38:56 -04:00
|
|
|
self.in([])
|
|
|
|
elsif other.exclude_end?
|
|
|
|
gteq(other.end)
|
|
|
|
else
|
|
|
|
gt(other.end)
|
|
|
|
end
|
2014-12-26 17:45:34 -05:00
|
|
|
elsif equals_quoted?(other.end, Float::INFINITY)
|
2014-10-25 08:38:56 -04:00
|
|
|
lt(other.begin)
|
|
|
|
else
|
|
|
|
left = lt(other.begin)
|
|
|
|
right = if other.exclude_end?
|
|
|
|
gteq(other.end)
|
|
|
|
else
|
|
|
|
gt(other.end)
|
|
|
|
end
|
|
|
|
left.or(right)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_in(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
case other
|
|
|
|
when Arel::SelectManager
|
2010-12-07 13:46:30 -05:00
|
|
|
Arel::Nodes::NotIn.new(self, other.ast)
|
2010-10-27 09:43:20 -04:00
|
|
|
when Range
|
2014-10-25 08:38:56 -04:00
|
|
|
if $VERBOSE
|
|
|
|
warn <<-eowarn
|
|
|
|
Passing a range to `#not_in` is deprecated. Call `#not_between`, instead.
|
|
|
|
eowarn
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
2014-10-25 08:38:56 -04:00
|
|
|
not_between(other)
|
2014-11-02 14:00:04 -05:00
|
|
|
when Enumerable
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::NotIn.new self, quoted_array(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
else
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::NotIn.new self, quoted_node(other)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_in_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :not_in, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def not_in_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :not_in, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def matches(other, escape = nil, case_sensitive = false)
|
2015-12-05 18:54:09 -05:00
|
|
|
Nodes::Matches.new self, quoted_node(other), escape, case_sensitive
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def matches_regexp(other, case_sensitive = true)
|
2015-12-05 20:23:12 -05:00
|
|
|
Nodes::Regexp.new self, quoted_node(other), case_sensitive
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def matches_any(others, escape = nil, case_sensitive = false)
|
2015-12-05 18:54:09 -05:00
|
|
|
grouping_any :matches, others, escape, case_sensitive
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def matches_all(others, escape = nil, case_sensitive = false)
|
2015-12-05 18:54:09 -05:00
|
|
|
grouping_all :matches, others, escape, case_sensitive
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def does_not_match(other, escape = nil, case_sensitive = false)
|
2015-12-05 18:54:09 -05:00
|
|
|
Nodes::DoesNotMatch.new self, quoted_node(other), escape, case_sensitive
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def does_not_match_regexp(other, case_sensitive = true)
|
2015-12-05 20:23:12 -05:00
|
|
|
Nodes::NotRegexp.new self, quoted_node(other), case_sensitive
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def does_not_match_any(others, escape = nil)
|
2014-09-24 22:01:33 -04:00
|
|
|
grouping_any :does_not_match, others, escape
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def does_not_match_all(others, escape = nil)
|
2014-09-24 22:01:33 -04:00
|
|
|
grouping_all :does_not_match, others, escape
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gteq(right)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::GreaterThanOrEqual.new self, quoted_node(right)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gteq_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :gteq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gteq_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :gteq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gt(right)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::GreaterThan.new self, quoted_node(right)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gt_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :gt, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def gt_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :gt, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lt(right)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::LessThan.new self, quoted_node(right)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lt_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :lt, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lt_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :lt, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lteq(right)
|
2014-10-27 18:58:05 -04:00
|
|
|
Nodes::LessThanOrEqual.new self, quoted_node(right)
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lteq_any(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_any :lteq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def lteq_all(others)
|
2010-10-27 09:43:20 -04:00
|
|
|
grouping_all :lteq, others
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def when(right)
|
2015-12-19 13:40:46 -05:00
|
|
|
Nodes::Case.new(self).when quoted_node(right)
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def concat(other)
|
2015-12-31 20:22:07 -05:00
|
|
|
Nodes::Concat.new self, other
|
|
|
|
end
|
|
|
|
|
2010-10-27 09:43:20 -04:00
|
|
|
private
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def grouping_any(method_id, others, *extras)
|
|
|
|
nodes = others.map { |expr| send(method_id, expr, *extras) }
|
|
|
|
Nodes::Grouping.new nodes.inject { |memo, node|
|
|
|
|
Nodes::Or.new(memo, node)
|
|
|
|
}
|
|
|
|
end
|
2010-10-27 09:43:20 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def grouping_all(method_id, others, *extras)
|
|
|
|
nodes = others.map { |expr| send(method_id, expr, *extras) }
|
|
|
|
Nodes::Grouping.new Nodes::And.new(nodes)
|
|
|
|
end
|
2014-10-27 18:58:05 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def quoted_node(other)
|
|
|
|
Nodes.build_quoted(other, self)
|
|
|
|
end
|
2014-10-27 18:58:05 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def quoted_array(others)
|
|
|
|
others.map { |v| quoted_node(v) }
|
|
|
|
end
|
2014-12-26 17:45:34 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def equals_quoted?(maybe_quoted, value)
|
|
|
|
if maybe_quoted.is_a?(Nodes::Quoted)
|
|
|
|
maybe_quoted.val == value
|
|
|
|
else
|
|
|
|
maybe_quoted == value
|
|
|
|
end
|
2014-12-26 17:45:34 -05:00
|
|
|
end
|
2010-10-27 09:43:20 -04:00
|
|
|
end
|
2010-11-23 21:22:42 -05:00
|
|
|
end
|