Class | Sequel::SQL::BooleanExpression |
In: |
lib/sequel_core/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_core/sql.rb, line 454 454: def self.from_value_pairs(pairs, op=:AND, negate=false) 455: pairs = pairs.collect do |l,r| 456: ce = case r 457: when Range 458: new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end)) 459: when Array, ::Sequel::Dataset, SQLArray 460: new(:IN, l, r) 461: when NilClass 462: new(:IS, l, r) 463: when Regexp 464: StringExpression.like(l, r) 465: else 466: new('=''=', l, r) 467: end 468: negate ? invert(ce) : ce 469: end 470: pairs.length == 1 ? pairs.at(0) : new(op, *pairs) 471: 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.
# File lib/sequel_core/sql.rb, line 476 476: def self.invert(ce) 477: case ce 478: when BooleanExpression 479: case op = ce.op 480: when :AND, :OR 481: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)}) 482: else 483: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 484: end 485: when ComplexExpression 486: raise(Sequel::Error, "operator #{ce.op} cannot be inverted") 487: else 488: BooleanExpression.new(:NOT, ce) 489: end 490: end