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 arrays of two pairs) 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 446 446: def self.from_value_pairs(pairs, op=:AND, negate=false) 447: pairs = pairs.collect do |l,r| 448: ce = case r 449: when Range 450: new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end)) 451: when Array, ::Sequel::Dataset, SQLArray 452: new(:IN, l, r) 453: when NilClass, TrueClass, FalseClass 454: new(:IS, l, r) 455: when Regexp 456: StringExpression.like(l, r) 457: else 458: new('=''=', l, r) 459: end 460: negate ? invert(ce) : ce 461: end 462: pairs.length == 1 ? pairs.at(0) : new(op, *pairs) 463: 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).
# File lib/sequel/sql.rb, line 469 469: def self.invert(ce) 470: case ce 471: when BooleanExpression 472: case op = ce.op 473: when :AND, :OR 474: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)}) 475: else 476: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 477: end 478: when ComplexExpression 479: raise(Sequel::Error, "operator #{ce.op} cannot be inverted") 480: else 481: BooleanExpression.new(:NOT, ce) 482: end 483: end