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

Sequel extends the Array class to add methods to implement the SQL DSL. Most of these methods require that the array not be empty and that it must consist solely of other arrays that have exactly two elements.

### Methods

all_two_pairs? case sql_array sql_expr sql_negate sql_or sql_string_join ~

### Public Instance methods

all_two_pairs?()

True if the array is not empty and all of its elements are arrays of size 2, false otherwise. This is used to determine if the array could be a specifier of conditions, used similarly to a hash but allowing for duplicate keys and a specific order.

       [].to_a.all_two_pairs? # => false
       [:a].to_a.all_two_pairs? # => false
       [[:b]].to_a.all_two_pairs? # => false
       [[:a, 1]].to_a.all_two_pairs? # => true

[Source]

        # File lib/sequel/core_sql.rb, line 23
    23:   def all_two_pairs?
    24:     !empty? && all?{|i| (Array === i) && (i.length == 2)}
    25:   end

case(default, expression = nil)

Return a Sequel::SQL::CaseExpression with this array as the conditions and the given default value and expression.

      [[{: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

[Source]

        # File lib/sequel/core_sql.rb, line 32
    32:   def case(default, expression = nil)
    33:     ::Sequel::SQL::CaseExpression.new(self, default, expression)
    34:   end

sql_array()

Return a Sequel::SQL::Array created from this array. Used if this array contains all two pairs and you want it treated as an SQL array instead of a ordered hash-like conditions.

      [[1, 2], [3, 4]] # SQL: 1 = 2 AND 3 = 4
      [[1, 2], [3, 4]].sql_array # SQL: ((1, 2), (3, 4))

[Source]

        # File lib/sequel/core_sql.rb, line 42
    42:   def sql_array
    43:     ::Sequel::SQL::SQLArray.new(self)
    44:   end

sql_expr()

Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions. Rarely do you need to call this explicitly, as Sequel generally assumes that arrays of all two pairs 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 52
    52:   def sql_expr
    53:     sql_expr_if_all_two_pairs
    54:   end

sql_negate()

Return a Sequel::SQL::BooleanExpression created from this array, 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 61
    61:   def sql_negate
    62:     sql_expr_if_all_two_pairs(:AND, true)
    63:   end

sql_or()

Return a Sequel::SQL::BooleanExpression created from this array, 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 70
    70:   def sql_or
    71:     sql_expr_if_all_two_pairs(:OR)
    72:   end

sql_string_join(joiner=nil)

Return a Sequel::SQL::BooleanExpression representing an SQL string made up of the concatenation of this array‘s elements. If an argument is passed it is used in between each element of the array in the SQL concatenation. This does not require that the array be made up of all two pairs.

      [:a].sql_string_join # SQL: a
      [:a, :b].sql_string_join # SQL: a || b
      [:a, 'b'].sql_string_join # SQL: a || 'b'
      ['a', :b].sql_string_join(' ') # SQL: 'a' || ' ' || b

[Source]

        # File lib/sequel/core_sql.rb, line 83
    83:   def sql_string_join(joiner=nil)
    84:     if joiner
    85:       args = zip([joiner]*length).flatten
    86:       args.pop
    87:     else
    88:       args = self
    89:     end
    90:     args = args.collect{|a| [Symbol, ::Sequel::SQL::Expression, ::Sequel::LiteralString, TrueClass, FalseClass, NilClass].any?{|c| a.is_a?(c)} ? a : a.to_s}
    91:     ::Sequel::SQL::StringExpression.new('||''||', *args)
    92:   end

~()

Return a Sequel::SQL::BooleanExpression created from this array, 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 10
    10:   def ~
    11:     sql_expr_if_all_two_pairs(:OR, true)
    12:   end

[Validate]