Class: Array

Inherits:
Object show all
Defined in:
lib/sequel/extensions/core_extensions.rb,
lib/sequel/extensions/pg_row.rb,
lib/sequel/extensions/pg_json.rb,
lib/sequel/extensions/pg_array.rb

Overview

Sequel extends Array 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

#case(*args) ⇒ 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


35
36
37
# File 'lib/sequel/extensions/core_extensions.rb', line 35

def case(*args)
  ::Sequel::SQL::CaseExpression.new(self, *args)
end

#pg_array(type = nil) ⇒ Object

Return a PGArray proxy to the receiver, using a specific database type if given. This is mostly useful as a short cut for creating PGArray objects that didn’t come from the database.



591
592
593
# File 'lib/sequel/extensions/pg_array.rb', line 591

def pg_array(type=nil)
  Sequel::Postgres::PGArray.new(self, type)
end

#pg_jsonObject

Return a Sequel::Postgres::JSONArray proxy to the receiver. This is mostly useful as a short cut for creating JSONArray objects that didn’t come from the database.



313
314
315
# File 'lib/sequel/extensions/pg_json.rb', line 313

def pg_json
  Sequel::Postgres::JSONArray.new(self)
end

#pg_jsonbObject

Return a Sequel::Postgres::JSONArray proxy to the receiver. This is mostly useful as a short cut for creating JSONArray objects that didn’t come from the database.



320
321
322
# File 'lib/sequel/extensions/pg_json.rb', line 320

def pg_jsonb
  Sequel::Postgres::JSONBArray.new(self)
end

#pg_rowObject

Wraps the receiver in an anonymous Sequel::Postgres::PGRow::ArrayRow instance.



600
601
602
# File 'lib/sequel/extensions/pg_row.rb', line 600

def pg_row
  Sequel::Postgres::PGRow::ArrayRow.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 two element arrays specify this type of condition. One case where it can be necessary to use this is if you are using the object as a value in a filter hash and want to use the = operator instead of the IN operator (which is used by default for arrays of two element arrays).

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


62
63
64
# File 'lib/sequel/extensions/core_extensions.rb', line 62

def sql_expr
  Sequel.expr(self)
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)


71
72
73
# File 'lib/sequel/extensions/core_extensions.rb', line 71

def sql_negate
  Sequel.negate(self)
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)


80
81
82
# File 'lib/sequel/extensions/core_extensions.rb', line 80

def sql_or
  Sequel.or(self)
end

#sql_string_join(joiner = nil) ⇒ Object

Return a Sequel::SQL::StringExpression 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.

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


93
94
95
# File 'lib/sequel/extensions/core_extensions.rb', line 93

def sql_string_join(joiner=nil)
  Sequel.join(self, joiner)
end

#sql_value_listObject Also known as: sql_array

Return a Sequel::SQL::ValueList created from this array. Used if this array contains all two element arrays and you want it treated as an SQL value list (IN predicate) instead of as a conditions specifier (similar to a hash). This is not necessary if you are using this array as a value in a filter, but may be necessary if you are using it as a value with placeholder SQL:

DB[:a].filter([:a, :b]=>[[1, 2], [3, 4]]) # SQL: (a, b) IN ((1, 2), (3, 4))
DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: (a, b) IN ((1 = 2) AND (3 = 4))
DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]].sql_value_list) # SQL: (a, b) IN ((1, 2), (3, 4))


48
49
50
# File 'lib/sequel/extensions/core_extensions.rb', line 48

def sql_value_list
  ::Sequel::SQL::ValueList.new(self)
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)


26
27
28
# File 'lib/sequel/extensions/core_extensions.rb', line 26

def ~
  Sequel.~(self)
end