Class: Sequel::SQL::BooleanExpression
- Inherits:
-
ComplexExpression
- Object
- Expression
- ComplexExpression
- Sequel::SQL::BooleanExpression
- Includes:
- BooleanMethods
- Defined in:
- lib/sequel_core/sql.rb
Overview
Subclass of ComplexExpression where the expression results in a boolean value in SQL.
Constant Summary
Constants inherited from ComplexExpression
ComplexExpression::BITWISE_OPERATORS, ComplexExpression::BOOLEAN_OPERATOR_METHODS, ComplexExpression::INEQUALITY_OPERATORS, ComplexExpression::MATHEMATICAL_OPERATORS, ComplexExpression::N_ARITY_OPERATORS, ComplexExpression::ONE_ARITY_OPERATORS, ComplexExpression::OPERTATOR_INVERSIONS, 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 arrays of two pairs) and converts it to a BooleanExpression.
-
.invert(ce) ⇒ Object
Invert the expression, if possible.
Methods included from BooleanMethods
Methods inherited from ComplexExpression
Methods included from OrderMethods
Methods included from CastMethods
#cast, #cast_numeric, #cast_string
Methods included from AliasMethods
Methods inherited from Expression
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 arrays of two pairs) 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
-
/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)
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/sequel_core/sql.rb', line 461 def self.from_value_pairs(pairs, op=:AND, negate=false) pairs = pairs.collect do |l,r| ce = case r when Range new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end)) when Array, ::Sequel::Dataset, SQLArray new(:IN, l, r) when NilClass new(:IS, l, r) when Regexp StringExpression.like(l, r) else new(:'=', l, r) end negate ? invert(ce) : ce end pairs.length == 1 ? pairs.at(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.
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/sequel_core/sql.rb', line 483 def self.invert(ce) case ce when BooleanExpression case op = ce.op when :AND, :OR BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)}) else BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup) end when ComplexExpression raise(Sequel::Error, "operator #{ce.op} cannot be inverted") else BooleanExpression.new(:NOT, ce) end end |