Class Sequel::SQL::BooleanExpression
In: lib/sequel/sql.rb
Parent: ComplexExpression

Subclass of ComplexExpression where the expression results in a boolean value in SQL.

Methods

&   from_value_pairs   invert   sql_boolean   |  

Included Modules

BooleanMethods

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)

[Source]

      # File lib/sequel/sql.rb, line 1005
1005:       def self.from_value_pairs(pairs, op=:AND, negate=false)
1006:         pairs = pairs.map{|l,r| from_value_pair(l, r)}
1007:         pairs.map!{|ce| invert(ce)} if negate
1008:         pairs.length == 1 ? pairs.at(0) : new(op, *pairs)
1009:       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"

[Source]

      # File lib/sequel/sql.rb, line 1042
1042:       def self.invert(ce)
1043:         case ce
1044:         when BooleanExpression
1045:           case op = ce.op
1046:           when :AND, :OR
1047:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)})
1048:           else
1049:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
1050:           end
1051:         when StringExpression, NumericExpression
1052:           raise(Sequel::Error, "cannot invert #{ce.inspect}")
1053:         when Constant
1054:           CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
1055:         else
1056:           BooleanExpression.new(:NOT, ce)
1057:         end
1058:       end

Public Instance methods

Always use an AND operator for & on BooleanExpressions

[Source]

      # File lib/sequel/sql.rb, line 1061
1061:       def &(ce)
1062:         BooleanExpression.new(:AND, self, ce)
1063:       end

Return self instead of creating a new object to save on memory.

[Source]

      # File lib/sequel/sql.rb, line 1071
1071:       def sql_boolean
1072:         self
1073:       end

Always use an OR operator for | on BooleanExpressions

[Source]

      # File lib/sequel/sql.rb, line 1066
1066:       def |(ce)
1067:         BooleanExpression.new(:OR, self, ce)
1068:       end

[Validate]