class Sequel::SQL::BooleanExpression

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

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

Methods

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:

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)
[show 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
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
1143 def self.invert(ce)
1144   case ce
1145   when BooleanExpression
1146     case op = ce.op
1147     when :AND, :OR
1148       BooleanExpression.new(OPERATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)})
1149     when :IN, :"NOT IN"
1150       BooleanExpression.new(OPERATOR_INVERSIONS[op], *ce.args.dup)
1151     else
1152       if ce.args.length == 2
1153         case ce.args[1]
1154         when Function, LiteralString, PlaceholderLiteralString
1155           # Special behavior to not push down inversion in this case because doing so
1156           # can result in incorrect behavior for ANY/SOME/ALL operators.
1157           BooleanExpression.new(:NOT, ce)
1158         else
1159           BooleanExpression.new(OPERATOR_INVERSIONS[op], *ce.args.dup)
1160         end
1161       else
1162         BooleanExpression.new(OPERATOR_INVERSIONS[op], *ce.args.dup)
1163       end
1164     end
1165   when StringExpression, NumericExpression
1166     raise(Sequel::Error, "cannot invert #{ce.inspect}")
1167   when Constant
1168     CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
1169   else
1170     BooleanExpression.new(:NOT, ce)
1171   end
1172 end

Public Instance methods

&(ce)

Always use an AND operator for & on BooleanExpressions

[show source]
     # File lib/sequel/sql.rb
1175 def &(ce)
1176   BooleanExpression.new(:AND, self, ce)
1177 end
sql_boolean()

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

[show source]
     # File lib/sequel/sql.rb
1185 def sql_boolean
1186   self
1187 end
|(ce)

Always use an OR operator for | on BooleanExpressions

[show source]
     # File lib/sequel/sql.rb
1180 def |(ce)
1181   BooleanExpression.new(:OR, self, ce)
1182 end