Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/sequel/core_sql.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.
Direct Known Subclasses
Instance Method Summary (collapse)
-
- (Boolean) all_two_pairs?
true if the array is not empty and all of its elements are arrays of size 2, false otherwise.
-
- (Object) case(*args)
Return a Sequel::SQL::CaseExpression with this array as the conditions and the given default value and expression.
-
- (Object) pg_array(type = nil)
Return a PGArray proxy to the receiver, using a specific database type if given.
-
- (Object) sql_expr
Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions.
-
- (Object) sql_negate
Return a Sequel::SQL::BooleanExpression created from this array, matching none of the conditions.
-
- (Object) sql_or
Return a Sequel::SQL::BooleanExpression created from this array, matching any of the conditions.
-
- (Object) sql_string_join(joiner = nil)
Return a Sequel::SQL::BooleanExpression representing an SQL string made up of the concatenation of this array's elements.
-
- (Object) sql_value_list
(also: #sql_array)
Return a Sequel::SQL::ValueList created from this array.
-
- (Object) ~
Return a Sequel::SQL::BooleanExpression created from this array, not matching all of the conditions.
Instance Method Details
- (Boolean) 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
23 24 25 |
# File 'lib/sequel/core_sql.rb', line 23 def all_two_pairs? !empty? && all?{|i| (Array === i) && (i.length == 2)} end |
- (Object) case(*args)
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/core_sql.rb', line 32 def case(*args) ::Sequel::SQL::CaseExpression.new(self, *args) end |
- (Object) pg_array(type = nil)
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.
457 458 459 |
# File 'lib/sequel/extensions/pg_array.rb', line 457 def pg_array(type=nil) Sequel::Postgres::PGArray.new(self, type) end |
- (Object) 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 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)
61 62 63 |
# File 'lib/sequel/core_sql.rb', line 61 def sql_expr sql_expr_if_all_two_pairs end |
- (Object) 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)
70 71 72 |
# File 'lib/sequel/core_sql.rb', line 70 def sql_negate sql_expr_if_all_two_pairs(:AND, true) end |
- (Object) 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)
79 80 81 |
# File 'lib/sequel/core_sql.rb', line 79 def sql_or sql_expr_if_all_two_pairs(:OR) end |
- (Object) 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.
[: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
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sequel/core_sql.rb', line 92 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) sql_value_list 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))
45 46 47 |
# File 'lib/sequel/core_sql.rb', line 45 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)
10 11 12 |
# File 'lib/sequel/core_sql.rb', line 10 def ~ sql_expr_if_all_two_pairs(:OR, true) end |