Class: Array

Inherits:
Object show all
Defined in:
lib/sequel/lib/sequel/core_sql.rb,
lib/ruby_extensions.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#all_two_pairs?Boolean

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

Returns:

  • (Boolean)

23
24
25
# File 'lib/sequel/lib/sequel/core_sql.rb', line 23

def all_two_pairs?
  !empty? && all?{|i| (Array === i) && (i.length == 2)}
end

#case(default, expression = nil) ⇒ Object

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

32
33
34
# File 'lib/sequel/lib/sequel/core_sql.rb', line 32

def case(default, expression = nil)
  ::Sequel::SQL::CaseExpression.new(self, default, expression)
end

#shift(number_of_items = nil) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_extensions.rb', line 18

def shift(number_of_items = nil)
  if number_of_items
    collection = []
    number_of_items.times do |i|
      collection << shift_without_args
    end
    collection
  else
    shift_without_args
  end
end

#shift_without_argsObject


16
# File 'lib/ruby_extensions.rb', line 16

alias :shift_without_args :shift

#sql_arrayObject

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

42
43
44
# File 'lib/sequel/lib/sequel/core_sql.rb', line 42

def sql_array
  ::Sequel::SQL::SQLArray.new(self)
end

#sql_exprObject

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)

52
53
54
# File 'lib/sequel/lib/sequel/core_sql.rb', line 52

def sql_expr
  sql_expr_if_all_two_pairs
end

#sql_negateObject

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)

61
62
63
# File 'lib/sequel/lib/sequel/core_sql.rb', line 61

def sql_negate
  sql_expr_if_all_two_pairs(:AND, true)
end

#sql_orObject

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)

70
71
72
# File 'lib/sequel/lib/sequel/core_sql.rb', line 70

def sql_or
  sql_expr_if_all_two_pairs(:OR)
end

#sql_string_join(joiner = nil) ⇒ Object

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

83
84
85
86
87
88
89
90
91
92
# File 'lib/sequel/lib/sequel/core_sql.rb', line 83

def sql_string_join(joiner=nil)
  if joiner
    args = zip([joiner]*length).flatten
    args.pop
  else
    args = self
  end
  args = args.collect{|a| [Symbol, ::Sequel::SQL::Expression, ::Sequel::LiteralString, TrueClass, FalseClass, NilClass].any?{|c| a.is_a?(c)} ? a : a.to_s}
  ::Sequel::SQL::StringExpression.new(:'||', *args)
end

#~Object

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)

10
11
12
# File 'lib/sequel/lib/sequel/core_sql.rb', line 10

def ~
  sql_expr_if_all_two_pairs(:OR, true)
end