class Sequel::SQL::BooleanExpression

  1. lib/sequel/sql.rb
Superclass: ComplexExpression

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


Public Class

  1. from_value_pairs
  2. invert

Public Instance

  1. &
  2. sql_boolean
  3. |

Included modules

  1. BooleanMethods

Public Class methods

from_value_pairs (pairs, op=:AND, negate=false)

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:


left >= 0 AND left <= 10


left IN (1,2)


left IS NULL


left IS TRUE




left ~ 'as'


left = 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, :OR, true)
[show source]
     # File lib/sequel/sql.rb
1073 def self.from_value_pairs(pairs, op=:AND, negate=false)
1074   pairs ={|l,r| from_value_pair(l, r)}
1075!{|ce| invert(ce)} if negate
1076   pairs.length == 1 ? pairs[0] : new(op, *pairs)
1077 end
invert (ce)

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"
[show source]
     # File lib/sequel/sql.rb
1120 def self.invert(ce)
1121   case ce
1122   when BooleanExpression
1123     case op = ce.op
1124     when :AND, :OR
1125[op], *{|a| BooleanExpression.invert(a)})
1126     when :IN, :"NOT IN"
1127[op], *ce.args.dup)
1128     else
1129       if ce.args.length == 2
1130         case ce.args[1]
1131         when Function, LiteralString, PlaceholderLiteralString
1132           # Special behavior to not push down inversion in this case because doing so
1133           # can result in incorrect behavior for ANY/SOME/ALL operators.
1134 , ce)
1135         else
1136 [op], *ce.args.dup)
1137         end
1138       else
1139[op], *ce.args.dup)
1140       end
1141     end
1142   when StringExpression, NumericExpression
1143     raise(Sequel::Error, "cannot invert #{ce.inspect}")
1144   when Constant
1145     CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
1146   else
1147, ce)
1148   end
1149 end

Public Instance methods

& (ce)

Always use an AND operator for & on BooleanExpressions

[show source]
     # File lib/sequel/sql.rb
1152 def &(ce)
1153, self, ce)
1154 end
sql_boolean ()

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

[show source]
     # File lib/sequel/sql.rb
1162 def sql_boolean
1163   self
1164 end
| (ce)

Always use an OR operator for | on BooleanExpressions

[show source]
     # File lib/sequel/sql.rb
1157 def |(ce)
1158, self, ce)
1159 end