Class: Hash

Class | Hash
In: | lib/sequel/core_sql.rb
Parent: | Object

Sequel extends the Hash class to add methods to implement the SQL DSL.

### Methods

& case sql_expr sql_negate sql_or | ~

### Public Instance methods

&(ce)

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash and the condition specified by the given argument.

      {:a=>1} & :b # SQL: a = 1 AND b
      {:a=>true} & ~:b # SQL: a IS TRUE AND NOT b

[Source]

         # File lib/sequel/core_sql.rb, line 111
    111:   def &(ce)
    112:     ::Sequel::SQL::BooleanExpression.new(:AND, self, ce)
    113:   end

case(default, expression = nil)

Return a Sequel::SQL::CaseExpression with this hash as the conditions and the given default value. Note that the order of the conditions will be arbitrary, so all conditions should be orthogonal.

      {{:a=>[2,3]}=>1}.case(0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
      {:a=>1, {:b=>2}].case(:d, :c) # SQL: CASE c WHEN a THEN 1 WHEN b THEN 2 ELSE d END
                                    #  or: CASE c WHEN b THEN 2 WHEN a THEN 1 ELSE d END

[Source]

         # File lib/sequel/core_sql.rb, line 141
    141:   def case(default, expression = nil)
    142:     ::Sequel::SQL::CaseExpression.new(to_a, default, expression)
    143:   end

sql_expr()

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions. Rarely do you need to call this explicitly, as Sequel generally assumes that hashes specify this type of condition.

      {:a=>true}.sql_expr # SQL: a IS TRUE
      {:a=>1, :b=>[2, 3]}.sql_expr # SQL: a = 1 AND b IN (2, 3)

[Source]

         # File lib/sequel/core_sql.rb, line 151
    151:   def sql_expr
    152:     ::Sequel::SQL::BooleanExpression.from_value_pairs(self)
    153:   end

sql_negate()

Return a Sequel::SQL::BooleanExpression created from this hash, matching none of the conditions.

      {:a=>true}.sql_negate # SQL: a IS NOT TRUE
      {:a=>1, :b=>[2, 3]}.sql_negate # SQL: a != 1 AND b NOT IN (2, 3)

[Source]

         # File lib/sequel/core_sql.rb, line 160
    160:   def sql_negate
    161:     ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :AND, true)
    162:   end

sql_or()

Return a Sequel::SQL::BooleanExpression created from this hash, matching any of the conditions.

      {:a=>true}.sql_or # SQL: a IS TRUE
      {:a=>1, :b=>[2, 3]}.sql_or # SQL: a = 1 OR b IN (2, 3)

[Source]

         # File lib/sequel/core_sql.rb, line 169
    169:   def sql_or
    170:     ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR)
    171:   end

|(ce)

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash or the condition specified by the given argument.

      {:a=>1} | :b # SQL: a = 1 OR b
      {:a=>true} | ~:b # SQL: a IS TRUE OR NOT b

[Source]

         # File lib/sequel/core_sql.rb, line 121
    121:   def |(ce)
    122:     ::Sequel::SQL::BooleanExpression.new(:OR, self, ce)
    123:   end

~()

Return a Sequel::SQL::BooleanExpression created from this hash, not matching all of the conditions.

      ~{:a=>true} # SQL: a IS NOT TRUE
      ~{:a=>1, :b=>[2, 3]} # SQL: a != 1 OR b NOT IN (2, 3)

[Source]

         # File lib/sequel/core_sql.rb, line 130
    130:   def ~
    131:     ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR, true)
    132:   end

[Validate]