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 983 983: def self.from_value_pairs(pairs, op=:AND, negate=false) 984: pairs = pairs.map{|l,r| from_value_pair(l, r)} 985: pairs.map!{|ce| invert(ce)} if negate 986: pairs.length == 1 ? pairs.at(0) : new(op, *pairs) 987: 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 1018 1018: def self.invert(ce) 1019: case ce 1020: when BooleanExpression 1021: case op = ce.op 1022: when :AND, :OR 1023: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)}) 1024: else 1025: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1026: end 1027: when StringExpression, NumericExpression 1028: raise(Sequel::Error, "cannot invert #{ce.inspect}") 1029: when Constant 1030: CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}") 1031: else 1032: BooleanExpression.new(:NOT, ce) 1033: end 1034: end
Always use an AND operator for & on BooleanExpressions
# File lib/sequel/sql.rb, line 1037 1037: def &(ce) 1038: BooleanExpression.new(:AND, self, ce) 1039: end