Class: Array

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

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. This is used to determine if the array could be a specifier of conditions, used similarly to a hash but allowing for duplicate keys.

hash.to_a.all_two_pairs? # => true unless hash is empty

Returns:

  • (Boolean)


14
15
16
# File 'lib/sequel/core_sql.rb', line 14

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.



20
21
22
# File 'lib/sequel/core_sql.rb', line 20

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

#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.



27
28
29
# File 'lib/sequel/core_sql.rb', line 27

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.



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

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.



39
40
41
# File 'lib/sequel/core_sql.rb', line 39

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.



45
46
47
# File 'lib/sequel/core_sql.rb', line 45

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sequel/core_sql.rb', line 53

def sql_string_join(joiner=nil)
  if joiner
    args = self.inject([]) do |m, a|
      m << a
      m << joiner
    end
    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

#to_sqlObject



296
297
298
299
300
# File 'lib/sequel/deprecated.rb', line 296

def to_sql
  Sequel::Deprecation.deprecate('Array#to_sql', 'It may be a good idea to refactor your implementation so this type of method is not required')
  map {|l| ((m = /^(.*)--/.match(l)) ? m[1] : l).chomp}.join(' '). \
    gsub(/\/\*.*\*\//, '').gsub(/\s+/, ' ').strip
end

#~Object

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



4
5
6
# File 'lib/sequel/core_sql.rb', line 4

def ~
  sql_expr_if_all_two_pairs(:OR, true)
end