class Sequel::SQL::BooleanExpression
Subclass of ComplexExpression
where the expression results in a boolean value in SQL
.
Public Class Methods
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
- 1,2
-
left IN (1,2)
- 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 1052 def self.from_value_pairs(pairs, op=:AND, negate=false) 1053 pairs = pairs.map{|l,r| from_value_pair(l, r)} 1054 pairs.map!{|ce| invert(ce)} if negate 1055 pairs.length == 1 ? pairs[0] : new(op, *pairs) 1056 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 1089 def self.invert(ce) 1090 case ce 1091 when BooleanExpression 1092 case op = ce.op 1093 when :AND, :OR 1094 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)}) 1095 else 1096 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1097 end 1098 when StringExpression, NumericExpression 1099 raise(Sequel::Error, "cannot invert #{ce.inspect}") 1100 when Constant 1101 CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}") 1102 else 1103 BooleanExpression.new(:NOT, ce) 1104 end 1105 end
Private Class Methods
Return a BooleanExpression
based on the right side of the pair.
# File lib/sequel/sql.rb 1059 def self.from_value_pair(l, r) 1060 case r 1061 when Range 1062 new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end)) 1063 when ::Array, ::Sequel::Dataset 1064 new(:IN, l, r) 1065 when NegativeBooleanConstant 1066 new(:"IS NOT", l, r.constant) 1067 when BooleanConstant 1068 new(:IS, l, r.constant) 1069 when NilClass, TrueClass, FalseClass 1070 new(:IS, l, r) 1071 when Regexp 1072 StringExpression.like(l, r) 1073 when DelayedEvaluation 1074 Sequel.delay{|ds| from_value_pair(l, r.call(ds))} 1075 when Dataset::PlaceholderLiteralizer::Argument 1076 r.transform{|v| from_value_pair(l, v)} 1077 else 1078 new(:'=', l, r) 1079 end 1080 end
Public Instance Methods
Always use an AND operator for & on BooleanExpressions
# File lib/sequel/sql.rb 1108 def &(ce) 1109 BooleanExpression.new(:AND, self, ce) 1110 end
Return self instead of creating a new object to save on memory.
# File lib/sequel/sql.rb 1118 def sql_boolean 1119 self 1120 end
Always use an OR operator for | on BooleanExpressions
# File lib/sequel/sql.rb 1113 def |(ce) 1114 BooleanExpression.new(:OR, self, ce) 1115 end