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:
0..10 : | left >= 0 AND left <= 10 |
nil : | left IS NULL |
true : | left IS TRUE |
false : | left IS FALSE |
/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)
# File lib/sequel/sql.rb, line 1054 1054: def self.from_value_pairs(pairs, op=:AND, negate=false) 1055: pairs = pairs.map{|l,r| from_value_pair(l, r)} 1056: pairs.map!{|ce| invert(ce)} if negate 1057: pairs.length == 1 ? pairs[0] : new(op, *pairs) 1058: 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 1091 1091: def self.invert(ce) 1092: case ce 1093: when BooleanExpression 1094: case op = ce.op 1095: when :AND, :OR 1096: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)}) 1097: else 1098: BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1099: end 1100: when StringExpression, NumericExpression 1101: raise(Sequel::Error, "cannot invert #{ce.inspect}") 1102: when Constant 1103: CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}") 1104: else 1105: BooleanExpression.new(:NOT, ce) 1106: end 1107: end
Always use an AND operator for & on BooleanExpressions
# File lib/sequel/sql.rb, line 1110 1110: def &(ce) 1111: BooleanExpression.new(:AND, self, ce) 1112: end