Class | Sequel::SQL::BooleanExpression |
In: |
lib/sequel/sql.rb
|
Parent: | ComplexExpression |
Subclass of ComplexExpression where the expression results in a boolean value in SQL.
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:
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 558 558: def self.from_value_pairs(pairs, op=:AND, negate=false) 559: pairs = pairs.collect do |l,r| 560: ce = case r 561: when Range 562: new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end)) 563: when ::Array, ::Sequel::Dataset 564: new(:IN, l, r) 565: when NegativeBooleanConstant 566: new("IS NOT""IS NOT", l, r.constant) 567: when BooleanConstant 568: new(:IS, l, r.constant) 569: when NilClass, TrueClass, FalseClass 570: new(:IS, l, r) 571: when Regexp 572: StringExpression.like(l, r) 573: else 574: new('=''=', l, r) 575: end 576: negate ? invert(ce) : ce 577: end 578: pairs.length == 1 ? pairs.at(0) : new(op, *pairs) 579: 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).
BooleanExpression.invert(:a) # NOT "a"
# File lib/sequel/sql.rb, line 587 587: def self.invert(ce) 588: case ce 589: when BooleanExpression 590: case op = ce.op 591: when :AND, :OR 592: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)}) 593: else 594: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 595: end 596: when StringExpression, NumericExpression 597: raise(Sequel::Error, "cannot invert #{ce.inspect}") 598: else 599: BooleanExpression.new(:NOT, ce) 600: end 601: end