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
 98 def [](key)
 99   s = Sequel::SQL::Subscript.new(self, [key])
100   s = ArrayOp.new(s) if key.is_a?(Range)
101   s
102 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
112 def all
113   function(:ALL)
114 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
124 def any
125   function(:ANY)
126 end
cardinality()

Call the cardinality method:

array_op.cardinality # cardinality(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
131 def cardinality
132   function(:cardinality)
133 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
145 def contained_by(other)
146   bool_op(CONTAINED_BY, wrap_array(other))
147 end
contains(other)

Use the contains (@>) operator:

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

Call the array_dims method:

array_op.dims # array_dims(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
152 def dims
153   function(:array_dims)
154 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
161 def hstore(arg=(no_arg_given=true; nil))
162   v = if no_arg_given
163     Sequel.function(:hstore, self)
164   else
165     Sequel.function(:hstore, self, wrap_array(arg))
166   end
167   # :nocov:
168   if Sequel.respond_to?(:hstore_op)
169   # :nocov:
170     v = Sequel.hstore_op(v)
171   end
172   v
173 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
179 def length(dimension = 1)
180   function(:array_length, dimension)
181 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
187 def lower(dimension = 1)
188   function(:array_lower, dimension)
189 end
overlaps(other)

Use the overlaps (&&) operator:

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

Return the receiver.

[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
208 def pg_array
209   self
210 end
push(other)

Use the concatentation (||) operator:

array_op.push(:a) # (array || a)
array_op.concat(:a) # (array || a)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
202 def push(other)
203   array_op(CONCAT, [self, wrap_array(other)])
204 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
215 def remove(element)
216   ArrayOp.new(function(:array_remove, element))
217 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
223 def replace(element, replacement)
224   ArrayOp.new(function(:array_replace, element, replacement))
225 end
reverse()

Call the array_reverse method:

array_op.reverse # array_reverse(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
230 def reverse
231   function(:array_reverse)
232 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
245 def sort(opts=OPTS)
246   desc = opts[:desc]
247   nulls = opts[:nulls]
248   if desc
249     if nulls == :last
250       function(:array_sort, true, false)
251     else
252       function(:array_sort, true)
253     end
254   elsif nulls == :first
255     function(:array_sort, false, true)
256   else
257     function(:array_sort)
258   end
259 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
267 def to_string(joiner="", null=nil)
268   if null.nil?
269     function(:array_to_string, joiner)
270   else
271     function(:array_to_string, joiner, null)
272   end
273 end
unnest(*args)

Call the unnest method:

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

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

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