Class: Sequel::SQL::BooleanExpression

Inherits:
ComplexExpression show all
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::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

#args, #op

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BooleanMethods

#~

Methods inherited from ComplexExpression

#initialize, #sql_number, #sql_string

Methods included from SubscriptMethods

#sql_subscript

Methods included from OrderMethods

#asc, #desc

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from AliasMethods

#as

Methods inherited from Expression

#==, attr_reader, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

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)


943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/sequel/sql.rb', line 943

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
      new(:IN, l, r)
    when NegativeBooleanConstant
      new(:"IS NOT", l, r.constant)
    when BooleanConstant
      new(:IS, l, r.constant)
    when NilClass, TrueClass, FalseClass
      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, except for possible issues with SQL NULL (i.e. 1 == NULL is NULL and 1 != NULL is also NULL).

BooleanExpression.invert(:a) # NOT "a"


972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/sequel/sql.rb', line 972

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 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



991
992
993
# File 'lib/sequel/sql.rb', line 991

def &(ce)
  BooleanExpression.new(:AND, self, ce)
end

#sql_booleanObject

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



1001
1002
1003
# File 'lib/sequel/sql.rb', line 1001

def sql_boolean
  self
end

#|(ce) ⇒ Object

Always use an OR operator for | on BooleanExpressions



996
997
998
# File 'lib/sequel/sql.rb', line 996

def |(ce)
  BooleanExpression.new(:OR, self, ce)
end