class Sequel::Postgres::ArrayOp

  1. lib/sequel/extensions/pg_array_ops.rb
Superclass: Sequel::SQL::Wrapper

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

Constants

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

Public Instance Aliases

concat -> push
join -> to_string

Public Instance methods

[](key)

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

array_op[1] # array[1]
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
103 def [](key)
104   s = Sequel::SQL::Subscript.new(self, [key])
105   s = ArrayOp.new(s) if key.is_a?(Range)
106   s
107 end
all()

Call the ALL function:

array_op.all # ALL(array)

Usually used like:

dataset.where(1=>array_op.all)
# WHERE (1 = ALL(array))
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
117 def all
118   function(:ALL)
119 end
any()

Call the ANY function:

array_op.any # ANY(array)

Usually used like:

dataset.where(1=>array_op.any)
# WHERE (1 = ANY(array))
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
129 def any
130   function(:ANY)
131 end
cardinality()

Call the cardinality method:

array_op.cardinality # cardinality(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
136 def cardinality
137   function(:cardinality)
138 end
contained_by(other)

Use the contained by (<@) operator:

array_op.contained_by(:a) # (array <@ a)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
150 def contained_by(other)
151   bool_op(CONTAINED_BY, wrap_array(other))
152 end
contains(other)

Use the contains (@>) operator:

array_op.contains(:a) # (array @> a)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
143 def contains(other)
144   bool_op(CONTAINS, wrap_array(other))
145 end
dims()

Call the array_dims method:

array_op.dims # array_dims(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
157 def dims
158   function(:array_dims)
159 end
hstore(arg=(no_arg_given=true; nil))

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)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
173 def hstore(arg=(no_arg_given=true; nil))
174   v = if no_arg_given
175     Sequel.function(:hstore, self)
176   else
177     Sequel.function(:hstore, self, wrap_array(arg))
178   end
179   # simplecov:disable
180   if Sequel.respond_to?(:hstore_op)
181   # simplecov:enable
182     v = Sequel.hstore_op(v)
183   end
184   v
185 end
length(dimension = 1)

Call the array_length method:

array_op.length    # array_length(array, 1)
array_op.length(2) # array_length(array, 2)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
191 def length(dimension = 1)
192   function(:array_length, dimension)
193 end
lower(dimension = 1)

Call the array_lower method:

array_op.lower    # array_lower(array, 1)
array_op.lower(2) # array_lower(array, 2)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
199 def lower(dimension = 1)
200   function(:array_lower, dimension)
201 end
ndims()

Call the array_ndims method:

array_op.ndims # array_ndims(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
164 def ndims
165   function(:array_ndims)
166 end
overlaps(other)

Use the overlaps (&&) operator:

array_op.overlaps(:a) # (array && a)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
228 def overlaps(other)
229   bool_op(OVERLAPS, wrap_array(other))
230 end
pg_array()

Return the receiver.

[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
242 def pg_array
243   self
244 end
position(element, offset = 1)

Call the array_position method:

array_op.position(1)    # array_position(array, 1, 1)
array_op.position(1, 2) # array_position(array, 1, 2)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
207 def position(element, offset = 1)
208   function(:array_position, element, offset)
209 end
positions(element)

Call the array_positions method:

array_op.positions(1) # array_positions(array, 1)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
214 def positions(element)
215   function(:array_positions, element)
216 end
prepend(element)

Call the array_prepend method:

array_op.prepend(1) # array_prepend(1, array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
221 def prepend(element)
222   SQL::Function.new(:array_prepend, element, self)
223 end
push(other)

Use the concatenation (||) operator:

array_op.push(:a) # (array || a)
array_op.concat(:a) # (array || a)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
236 def push(other)
237   array_op(CONCAT, [self, wrap_array(other)])
238 end
remove(element)

Remove the given element from the array:

array_op.remove(1) # array_remove(array, 1)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
249 def remove(element)
250   ArrayOp.new(function(:array_remove, element))
251 end
replace(element, replacement)

Replace the given element in the array with another element:

array_op.replace(1, 2) # array_replace(array, 1, 2)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
257 def replace(element, replacement)
258   ArrayOp.new(function(:array_replace, element, replacement))
259 end
reverse()

Call the array_reverse method:

array_op.reverse # array_reverse(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
264 def reverse
265   function(:array_reverse)
266 end
sample(element)

Call the array_sample method:

array_op.sample(1) # array_sample(array, 1)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
271 def sample(element)
272   function(:array_sample, element)
273 end
shuffle()

Call the array_shuffle method:

array_op.shuffle # array_shuffle
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
278 def shuffle
279   function(:array_shuffle)
280 end
sort(opts=OPTS)

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)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
293 def sort(opts=OPTS)
294   desc = opts[:desc]
295   nulls = opts[:nulls]
296   if desc
297     if nulls == :last
298       function(:array_sort, true, false)
299     else
300       function(:array_sort, true)
301     end
302   elsif nulls == :first
303     function(:array_sort, false, true)
304   else
305     function(:array_sort)
306   end
307 end
to_string(joiner="", null=nil)

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, ':', '*')
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
315 def to_string(joiner="", null=nil)
316   if null.nil?
317     function(:array_to_string, joiner)
318   else
319     function(:array_to_string, joiner, null)
320   end
321 end
trim(number)

Call the trim_array method:

array_op.trim(1) # array_trim(array, 1)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
327 def trim(number)
328   function(:trim_array, number)
329 end
unnest(*args)

Call the unnest method:

array_op.unnest # unnest(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
334 def unnest(*args)
335   function(:unnest, *args.map{|a| wrap_array(a)})
336 end
unshift(other)

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

array_op.unshift(:a) # (a || array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
341 def unshift(other)
342   array_op(CONCAT, [wrap_array(other), self])
343 end