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
91 def [](key)
92   s = Sequel::SQL::Subscript.new(self, [key])
93   s = ArrayOp.new(s) if key.is_a?(Range)
94   s
95 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
105 def all
106   function(:ALL)
107 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
117 def any
118   function(:ANY)
119 end
cardinality()

Call the cardinality method:

array_op.cardinality # cardinality(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
124 def cardinality
125   function(:cardinality)
126 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
138 def contained_by(other)
139   bool_op(CONTAINED_BY, wrap_array(other))
140 end
contains(other)

Use the contains (@>) operator:

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

Call the array_dims method:

array_op.dims # array_dims(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
145 def dims
146   function(:array_dims)
147 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
154 def hstore(arg=(no_arg_given=true; nil))
155   v = if no_arg_given
156     Sequel.function(:hstore, self)
157   else
158     Sequel.function(:hstore, self, wrap_array(arg))
159   end
160   # :nocov:
161   if Sequel.respond_to?(:hstore_op)
162   # :nocov:
163     v = Sequel.hstore_op(v)
164   end
165   v
166 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
172 def length(dimension = 1)
173   function(:array_length, dimension)
174 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
180 def lower(dimension = 1)
181   function(:array_lower, dimension)
182 end
overlaps(other)

Use the overlaps (&&) operator:

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

Return the receiver.

[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
201 def pg_array
202   self
203 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
195 def push(other)
196   array_op(CONCAT, [self, wrap_array(other)])
197 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
208 def remove(element)
209   ArrayOp.new(function(:array_remove, element))
210 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
216 def replace(element, replacement)
217   ArrayOp.new(function(:array_replace, element, replacement))
218 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
226 def to_string(joiner="", null=nil)
227   if null.nil?
228     function(:array_to_string, joiner)
229   else
230     function(:array_to_string, joiner, null)
231   end
232 end
unnest(*args)

Call the unnest method:

array_op.unnest # unnest(array)
[show source]
    # File lib/sequel/extensions/pg_array_ops.rb
238 def unnest(*args)
239   function(:unnest, *args.map{|a| wrap_array(a)})
240 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
245 def unshift(other)
246   array_op(CONCAT, [wrap_array(other), self])
247 end