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 1081 def self.from_value_pairs(pairs, op=:AND, negate=false) 1082 pairs = pairs.map{|l,r| from_value_pair(l, r)} 1083 pairs.map!{|ce| invert(ce)} if negate 1084 pairs.length == 1 ? pairs[0] : new(op, *pairs) 1085 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 1140 def self.invert(ce) 1141 case ce 1142 when BooleanExpression 1143 case op = ce.op 1144 when :AND, :OR 1145 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)}) 1146 when :IN, :"NOT IN" 1147 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1148 else 1149 if ce.args.length == 2 1150 case ce.args[1] 1151 when Function, LiteralString, PlaceholderLiteralString 1152 # Special behavior to not push down inversion in this case because doing so 1153 # can result in incorrect behavior for ANY/SOME/ALL operators. 1154 BooleanExpression.new(:NOT, ce) 1155 else 1156 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1157 end 1158 else 1159 BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) 1160 end 1161 end 1162 when StringExpression, NumericExpression 1163 raise(Sequel::Error, "cannot invert #{ce.inspect}") 1164 when Constant 1165 CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}") 1166 else 1167 BooleanExpression.new(:NOT, ce) 1168 end 1169 end
Private Class Methods
Return a BooleanExpression
based on the right side of the pair.
# File lib/sequel/sql.rb 1088 def self.from_value_pair(l, r) 1089 case r 1090 when Range 1091 unless r.begin.nil? 1092 begin_expr = new(:>=, l, r.begin) 1093 end 1094 unless r.end.nil? 1095 end_expr = new(r.exclude_end? ? :< : :<=, l, r.end) 1096 end 1097 if begin_expr 1098 if end_expr 1099 new(:AND, begin_expr, end_expr) 1100 else 1101 begin_expr 1102 end 1103 elsif end_expr 1104 end_expr 1105 else 1106 new(:'=', 1, 1) 1107 end 1108 when ::Array 1109 r = r.dup.freeze unless r.frozen? 1110 new(:IN, l, r) 1111 when ::String 1112 r = r.dup.freeze unless r.frozen? 1113 new(:'=', l, r) 1114 when ::Sequel::Dataset 1115 new(:IN, l, r) 1116 when NegativeBooleanConstant 1117 new(:"IS NOT", l, r.constant) 1118 when BooleanConstant 1119 new(:IS, l, r.constant) 1120 when NilClass, TrueClass, FalseClass 1121 new(:IS, l, r) 1122 when Regexp 1123 StringExpression.like(l, r) 1124 when DelayedEvaluation 1125 Sequel.delay{|ds| from_value_pair(l, r.call(ds))} 1126 when Dataset::PlaceholderLiteralizer::Argument 1127 r.transform{|v| from_value_pair(l, v)} 1128 else 1129 new(:'=', l, r) 1130 end 1131 end
Public Instance Methods
Always use an AND operator for & on BooleanExpressions
# File lib/sequel/sql.rb 1172 def &(ce) 1173 BooleanExpression.new(:AND, self, ce) 1174 end
Return self instead of creating a new object to save on memory.
# File lib/sequel/sql.rb 1182 def sql_boolean 1183 self 1184 end
Always use an OR operator for | on BooleanExpressions
# File lib/sequel/sql.rb 1177 def |(ce) 1178 BooleanExpression.new(:OR, self, ce) 1179 end