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