Included Modules

Class Index [+]

Quicksearch

Sequel::SQL::BooleanExpression

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

Public Class Methods

from_value_pairs(pairs, op=:AND, negate=false) click to toggle source

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)
     # File lib/sequel/sql.rb, line 887
887:       def self.from_value_pairs(pairs, op=:AND, negate=false)
888:         pairs = pairs.collect do |l,r|
889:           ce = case r
890:           when Range
891:             new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end))
892:           when ::Array, ::Sequel::Dataset
893:             new(:IN, l, r)
894:           when NegativeBooleanConstant
895:             new(:"IS NOT", l, r.constant)
896:           when BooleanConstant
897:             new(:IS, l, r.constant)
898:           when NilClass, TrueClass, FalseClass
899:             new(:IS, l, r)
900:           when Regexp
901:             StringExpression.like(l, r)
902:           else
903:             new(:'=', l, r)
904:           end
905:           negate ? invert(ce) : ce
906:         end
907:         pairs.length == 1 ? pairs.at(0) : new(op, *pairs)
908:       end
invert(ce) click to toggle source

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"
     # File lib/sequel/sql.rb, line 916
916:       def self.invert(ce)
917:         case ce
918:         when BooleanExpression
919:           case op = ce.op
920:           when :AND, :OR
921:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.collect{|a| BooleanExpression.invert(a)})
922:           else
923:             BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
924:           end
925:         when StringExpression, NumericExpression
926:           raise(Sequel::Error, "cannot invert #{ce.inspect}")
927:         when Constant
928:           CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
929:         else
930:           BooleanExpression.new(:NOT, ce)
931:         end
932:       end

Public Instance Methods

&(ce) click to toggle source

Always use an AND operator for & on BooleanExpressions

     # File lib/sequel/sql.rb, line 935
935:       def &(ce)
936:         BooleanExpression.new(:AND, self, ce)
937:       end
sql_boolean() click to toggle source

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

     # File lib/sequel/sql.rb, line 945
945:       def sql_boolean
946:         self
947:       end
|(ce) click to toggle source

Always use an OR operator for | on BooleanExpressions

     # File lib/sequel/sql.rb, line 940
940:       def |(ce)
941:         BooleanExpression.new(:OR, self, ce)
942:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.