Class: Sequel::Postgres::ArrayOp

Inherits:
SQL::Wrapper show all
Defined in:
lib/sequel/extensions/pg_array_ops.rb

Overview

The ArrayOp class is a simple container for a single object that defines methods that yield Sequel expression objects representing PostgreSQL array operators and functions.

In the method documentation examples, assume that:

array_op = :array.pg_array

Constant Summary collapse

CONCAT =
["(".freeze, " || ".freeze, ")".freeze].freeze
CONTAINS =
["(".freeze, " @> ".freeze, ")".freeze].freeze
CONTAINED_BY =
["(".freeze, " <@ ".freeze, ")".freeze].freeze
OVERLAPS =
["(".freeze, " && ".freeze, ")".freeze].freeze

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from SQL::Wrapper

#initialize

Methods included from SQL::IsDistinctFrom::Methods

#is_distinct_from

Methods included from SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from RangeOpMethods

#pg_range

Methods included from JSONOpMethods

#pg_json, #pg_jsonb

Methods included from InetOpMethods

#pg_inet

Methods included from PGRowOp::ExpressionMethods

#pg_row

Methods included from SQL::SubscriptMethods

#sql_subscript

Methods included from SQL::StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from SQL::PatternMatchMethods

#!~, #=~

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+, #coerce

Methods included from SQL::ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from SQL::BooleanMethods

#~

Methods included from SQL::AliasMethods

#as

Methods inherited from SQL::Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

This class inherits a constructor from Sequel::SQL::Wrapper

Instance Method Details

#[](key) ⇒ Object

Access a member of the array, returns an SQL::Subscript instance:

array_op[1] # array[1]


98
99
100
101
102
# File 'lib/sequel/extensions/pg_array_ops.rb', line 98

def [](key)
  s = Sequel::SQL::Subscript.new(self, [key])
  s = ArrayOp.new(s) if key.is_a?(Range)
  s
end

#allObject

Call the ALL function:

array_op.all # ALL(array)

Usually used like:

dataset.where(1=>array_op.all)
# WHERE (1 = ALL(array))


112
113
114
# File 'lib/sequel/extensions/pg_array_ops.rb', line 112

def all
  function(:ALL)
end

#anyObject

Call the ANY function:

array_op.any # ANY(array)

Usually used like:

dataset.where(1=>array_op.any)
# WHERE (1 = ANY(array))


124
125
126
# File 'lib/sequel/extensions/pg_array_ops.rb', line 124

def any
  function(:ANY)
end

#cardinalityObject

Call the cardinality method:

array_op.cardinality # cardinality(array)


131
132
133
# File 'lib/sequel/extensions/pg_array_ops.rb', line 131

def cardinality
  function(:cardinality)
end

#contained_by(other) ⇒ Object

Use the contained by (<@) operator:

array_op.contained_by(:a) # (array <@ a)


145
146
147
# File 'lib/sequel/extensions/pg_array_ops.rb', line 145

def contained_by(other)
  bool_op(CONTAINED_BY, wrap_array(other))
end

#contains(other) ⇒ Object

Use the contains (@>) operator:

array_op.contains(:a) # (array @> a)


138
139
140
# File 'lib/sequel/extensions/pg_array_ops.rb', line 138

def contains(other)
  bool_op(CONTAINS, wrap_array(other))
end

#dimsObject

Call the array_dims method:

array_op.dims # array_dims(array)


152
153
154
# File 'lib/sequel/extensions/pg_array_ops.rb', line 152

def dims
  function(:array_dims)
end

#hstore(arg = (no_arg_given=true; nil)) ⇒ Object

Convert the array into an hstore using the hstore function. If given an argument, use the two array form:

array_op.hstore          # hstore(array)
array_op.hstore(:array2) # hstore(array, array2)


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/sequel/extensions/pg_array_ops.rb', line 161

def hstore(arg=(no_arg_given=true; nil))
  v = if no_arg_given
    Sequel.function(:hstore, self)
  else
    Sequel.function(:hstore, self, wrap_array(arg))
  end
  # :nocov:
  if Sequel.respond_to?(:hstore_op)
  # :nocov:
    v = Sequel.hstore_op(v)
  end
  v
end

#length(dimension = 1) ⇒ Object

Call the array_length method:

array_op.length    # array_length(array, 1)
array_op.length(2) # array_length(array, 2)


179
180
181
# File 'lib/sequel/extensions/pg_array_ops.rb', line 179

def length(dimension = 1)
  function(:array_length, dimension)
end

#lower(dimension = 1) ⇒ Object

Call the array_lower method:

array_op.lower    # array_lower(array, 1)
array_op.lower(2) # array_lower(array, 2)


187
188
189
# File 'lib/sequel/extensions/pg_array_ops.rb', line 187

def lower(dimension = 1)
  function(:array_lower, dimension)
end

#overlaps(other) ⇒ Object

Use the overlaps (&&) operator:

array_op.overlaps(:a) # (array && a)


194
195
196
# File 'lib/sequel/extensions/pg_array_ops.rb', line 194

def overlaps(other)
  bool_op(OVERLAPS, wrap_array(other))
end

#pg_arrayObject

Return the receiver.



208
209
210
# File 'lib/sequel/extensions/pg_array_ops.rb', line 208

def pg_array
  self
end

#push(other) ⇒ Object Also known as: concat

Use the concatentation (||) operator:

array_op.push(:a) # (array || a)
array_op.concat(:a) # (array || a)


202
203
204
# File 'lib/sequel/extensions/pg_array_ops.rb', line 202

def push(other)
  array_op(CONCAT, [self, wrap_array(other)])
end

#remove(element) ⇒ Object

Remove the given element from the array:

array_op.remove(1) # array_remove(array, 1)


215
216
217
# File 'lib/sequel/extensions/pg_array_ops.rb', line 215

def remove(element)
  ArrayOp.new(function(:array_remove, element))
end

#replace(element, replacement) ⇒ Object

Replace the given element in the array with another element:

array_op.replace(1, 2) # array_replace(array, 1, 2)


223
224
225
# File 'lib/sequel/extensions/pg_array_ops.rb', line 223

def replace(element, replacement)
  ArrayOp.new(function(:array_replace, element, replacement))
end

#reverseObject

Call the array_reverse method:

array_op.reverse # array_reverse(array)


230
231
232
# File 'lib/sequel/extensions/pg_array_ops.rb', line 230

def reverse
  function(:array_reverse)
end

#sort(opts = OPTS) ⇒ Object

Call the array_sort method. Options:

:desc

Sort in descending order instead of ascending order.

:nulls

If sorting in ascending order and value is :first, include NULL values before non-NULL values. If sorting in descending order and value is :last, include non-NULL values before NULL values.

array_op.sort                           # array_sort(array)
array_op.sort(desc: true)               # array_sort(array, true)
array_op.sort(nulls: :first)            # array_sort(array, false, true)
array_op.sort(desc: true, nulls: :last) # array_sort(array, true, false)


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/sequel/extensions/pg_array_ops.rb', line 245

def sort(opts=OPTS)
  desc = opts[:desc]
  nulls = opts[:nulls]
  if desc
    if nulls == :last
      function(:array_sort, true, false)
    else
      function(:array_sort, true)
    end
  elsif nulls == :first
    function(:array_sort, false, true)
  else
    function(:array_sort)
  end
end

#to_string(joiner = "", null = nil) ⇒ Object Also known as: join

Call the array_to_string method:

array_op.join           # array_to_string(array, '')
array_op.to_string      # array_to_string(array, '')
array_op.join(":")      # array_to_string(array, ':')
array_op.join(":", "*") # array_to_string(array, ':', '*')


267
268
269
270
271
272
273
# File 'lib/sequel/extensions/pg_array_ops.rb', line 267

def to_string(joiner="", null=nil)
  if null.nil?
    function(:array_to_string, joiner)
  else
    function(:array_to_string, joiner, null)
  end
end

#unnest(*args) ⇒ Object

Call the unnest method:

array_op.unnest # unnest(array)


279
280
281
# File 'lib/sequel/extensions/pg_array_ops.rb', line 279

def unnest(*args)
  function(:unnest, *args.map{|a| wrap_array(a)})
end

#unshift(other) ⇒ Object

Use the concatentation (||) operator, reversing the order:

array_op.unshift(:a) # (a || array)


286
287
288
# File 'lib/sequel/extensions/pg_array_ops.rb', line 286

def unshift(other)
  array_op(CONCAT, [wrap_array(other), self])
end