class Sequel::SQL::BooleanExpression

Subclass of ComplexExpression where the expression results in a boolean value in SQL.

Public Class Methods

from_value_pairs(pairs, op=:AND, negate=false) click to toggle source

Take pairs of values (e.g. a hash or array of two element arrays) and converts it to a BooleanExpression. The operator and args used depends on the case of the right (2nd) argument:

0..10

left >= 0 AND left <= 10

1,2

left IN (1,2)

nil

left IS NULL

true

left IS TRUE

false

left IS FALSE

/as/

left ~ 'as'

:blah

left = blah

'blah'

left = 'blah'

If multiple arguments are given, they are joined with the op given (AND by default, OR possible). If negate is set to true, all subexpressions are inverted before used. Therefore, the following expressions are equivalent:

~from_value_pairs(hash)
from_value_pairs(hash, :OR, true)
# File lib/sequel/sql.rb, line 1108
def self.from_value_pairs(pairs, op=:AND, negate=false)
  pairs = pairs.map{|l,r| from_value_pair(l, r)}
  pairs.map!{|ce| invert(ce)} if negate
  pairs.length == 1 ? pairs.at(0) : new(op, *pairs)
end
invert(ce) click to toggle source

Invert the expression, if possible. If the expression cannot be inverted, raise an error. An inverted expression should match everything that the uninverted expression did not match, and vice-versa, except for possible issues with SQL NULL (i.e. 1 == NULL is NULL and 1 != NULL is also NULL).

BooleanExpression.invert(:a) # NOT "a"
# File lib/sequel/sql.rb, line 1145
def self.invert(ce)
  case ce
  when BooleanExpression
    case op = ce.op
    when :AND, :OR
      BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)})
    else
      BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
    end
  when StringExpression, NumericExpression
    raise(Sequel::Error, "cannot invert #{ce.inspect}")
  when Constant
    CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
  else
    BooleanExpression.new(:NOT, ce)
  end
end

Private Class Methods

from_value_pair(l, r) click to toggle source

Return a BooleanExpression based on the right side of the pair.

# File lib/sequel/sql.rb, line 1115
def self.from_value_pair(l, r)
  case r
  when Range
    new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end))
  when ::Array, ::Sequel::Dataset
    new(:IN, l, r)
  when NegativeBooleanConstant
    new(:"IS NOT", l, r.constant)
  when BooleanConstant
    new(:IS, l, r.constant)
  when NilClass, TrueClass, FalseClass
    new(:IS, l, r)
  when Regexp
    StringExpression.like(l, r)
  when DelayedEvaluation
    Sequel.delay{|ds| from_value_pair(l, r.call(ds))}
  when Dataset::PlaceholderLiteralizer::Argument
    r.transform{|v| from_value_pair(l, v)}
  else
    new(:'=', l, r)
  end
end

Public Instance Methods

&(ce) click to toggle source

Always use an AND operator for & on BooleanExpressions

# File lib/sequel/sql.rb, line 1164
def &(ce)
  BooleanExpression.new(:AND, self, ce)
end
sql_boolean() click to toggle source

Return self instead of creating a new object to save on memory.

# File lib/sequel/sql.rb, line 1174
def sql_boolean
  self
end
|(ce) click to toggle source

Always use an OR operator for | on BooleanExpressions

# File lib/sequel/sql.rb, line 1169
def |(ce)
  BooleanExpression.new(:OR, self, ce)
end