Class Sequel::SQL::BooleanExpression
In: lib/sequel/sql.rb
Parent: ComplexExpression

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

Methods

Included Modules

BooleanMethods

Public Class methods

Take pairs of values (e.g. a hash or array of arrays of two pairs) 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
  • /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)

[Source]

     # File lib/sequel/sql.rb, line 478
478:       def self.from_value_pairs(pairs, op=:AND, negate=false)
479:         pairs = pairs.collect do |l,r|
480:           ce = case r
481:           when Range
482:             new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end))
483:           when Array, ::Sequel::Dataset, SQLArray
484:             new(:IN, l, r)
485:           when NegativeBooleanConstant
486:             new("IS NOT""IS NOT", l, r.constant)
487:           when BooleanConstant
488:             new(:IS, l, r.constant)
489:           when NilClass, TrueClass, FalseClass
490:             new(:IS, l, r)
491:           when Regexp
492:             StringExpression.like(l, r)
493:           else
494:             new('=''=', l, r)
495:           end
496:           negate ? invert(ce) : ce
497:         end
498:         pairs.length == 1 ? pairs.at(0) : new(op, *pairs)
499:       end

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).

[Source]

     # File lib/sequel/sql.rb, line 505
505:       def self.invert(ce)
506:         case ce
507:         when BooleanExpression
508:           case op = ce.op
509:           when :AND, :OR
510:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)})
511:           else
512:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
513:           end
514:         when StringExpression, NumericExpression
515:           raise(Sequel::Error, "cannot invert #{ce.inspect}")
516:         else
517:           BooleanExpression.new(:NOT, ce)
518:         end
519:       end

[Validate]