Class: Sequel::SQL::BooleanExpression
- Inherits:
-
ComplexExpression
- Object
- Expression
- ComplexExpression
- Sequel::SQL::BooleanExpression
- Includes:
- BooleanMethods
- Defined in:
- lib/sequel/sql.rb
Overview
Subclass of ComplexExpression
where the expression results in a boolean value in SQL.
Constant Summary
Constants inherited from ComplexExpression
ComplexExpression::ASSOCIATIVE_OPERATORS, ComplexExpression::BITWISE_OPERATORS, ComplexExpression::BOOLEAN_OPERATOR_METHODS, ComplexExpression::CONSTANT_INVERSIONS, ComplexExpression::CUSTOM_EXPRESSIONS, ComplexExpression::EQUALITY_OPERATORS, ComplexExpression::INEQUALITY_OPERATORS, ComplexExpression::IN_OPERATORS, ComplexExpression::IS_OPERATORS, ComplexExpression::LIKE_OPERATORS, ComplexExpression::MATHEMATICAL_OPERATORS, ComplexExpression::N_ARITY_OPERATORS, ComplexExpression::ONE_ARITY_OPERATORS, ComplexExpression::OPERTATOR_INVERSIONS, ComplexExpression::REGEXP_OPERATORS, ComplexExpression::TWO_ARITY_OPERATORS
Instance Attribute Summary
Attributes inherited from ComplexExpression
Class Method Summary collapse
-
.from_value_pairs(pairs, op = :AND, negate = false) ⇒ Object
Take pairs of values (e.g. a hash or array of two element arrays) and converts it to a
BooleanExpression
. -
.invert(ce) ⇒ Object
Invert the expression, if possible.
Instance Method Summary collapse
-
#&(ce) ⇒ Object
Always use an AND operator for & on BooleanExpressions.
-
#sql_boolean ⇒ Object
Return self instead of creating a new object to save on memory.
-
#|(ce) ⇒ Object
Always use an OR operator for | on BooleanExpressions.
Methods included from BooleanMethods
Methods inherited from ComplexExpression
#initialize, #sql_number, #sql_string
Methods included from SubscriptMethods
Methods included from PatternMatchMethods
Methods included from OrderMethods
Methods included from CastMethods
#cast, #cast_numeric, #cast_string
Methods included from AliasMethods
Methods inherited from Expression
#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect
Constructor Details
This class inherits a constructor from Sequel::SQL::ComplexExpression
Class Method Details
.from_value_pairs(pairs, op = :AND, negate = false) ⇒ Object
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)
1081 1082 1083 1084 1085 |
# File 'lib/sequel/sql.rb', line 1081 def self.from_value_pairs(pairs, op=:AND, negate=false) pairs = pairs.map{|l,r| from_value_pair(l, r)} pairs.map!{|ce| invert(ce)} if negate pairs.length == 1 ? pairs[0] : new(op, *pairs) end |
.invert(ce) ⇒ Object
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"
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 |
# File 'lib/sequel/sql.rb', line 1140 def self.invert(ce) case ce when BooleanExpression case op = ce.op when :AND, :OR BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)}) when :IN, :"NOT IN" BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) else if ce.args.length == 2 case ce.args[1] when Function, LiteralString, PlaceholderLiteralString # Special behavior to not push down inversion in this case because doing so # can result in incorrect behavior for ANY/SOME/ALL operators. BooleanExpression.new(:NOT, ce) else BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) end else BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) end end when StringExpression, NumericExpression raise(Sequel::Error, "cannot invert #{ce.inspect}") when Constant CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}") else BooleanExpression.new(:NOT, ce) end end |
Instance Method Details
#&(ce) ⇒ Object
Always use an AND operator for & on BooleanExpressions
1172 1173 1174 |
# File 'lib/sequel/sql.rb', line 1172 def &(ce) BooleanExpression.new(:AND, self, ce) end |
#sql_boolean ⇒ Object
Return self instead of creating a new object to save on memory.
1182 1183 1184 |
# File 'lib/sequel/sql.rb', line 1182 def sql_boolean self end |
#|(ce) ⇒ Object
Always use an OR operator for | on BooleanExpressions
1177 1178 1179 |
# File 'lib/sequel/sql.rb', line 1177 def |(ce) BooleanExpression.new(:OR, self, ce) end |