Class Sequel::Postgres::ArrayOp
In: lib/sequel/extensions/pg_array_ops.rb
Parent: 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

Methods

[]   all   any   cardinality   concat   contained_by   contains   dims   hstore   join   length   lower   overlaps   pg_array   push   remove   replace   to_string   unnest   unshift  

Constants

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

Public Instance methods

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

  array_op[1] # array[1]

[Source]

    # File lib/sequel/extensions/pg_array_ops.rb, line 87
87:       def [](key)
88:         s = Sequel::SQL::Subscript.new(self, [key])
89:         s = ArrayOp.new(s) if key.is_a?(Range)
90:         s
91:       end

Call the ALL function:

  array_op.all # ALL(array)

Usually used like:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 101
101:       def all
102:         function(:ALL)
103:       end

Call the ANY function:

  array_op.all # ANY(array)

Usually used like:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 113
113:       def any
114:         function(:ANY)
115:       end

Call the cardinality method:

  array_op.cardinality # cardinality(array)

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 120
120:       def cardinality
121:         function(:cardinality)
122:       end
concat(other)

Alias for push

Use the contained by (<@) operator:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 134
134:       def contained_by(other)
135:         bool_op(CONTAINED_BY, wrap_array(other))
136:       end

Use the contains (@>) operator:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 127
127:       def contains(other)
128:         bool_op(CONTAINS, wrap_array(other))
129:       end

Call the array_dims method:

  array_op.dims # array_dims(array)

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 141
141:       def dims
142:         function(:array_dims)
143:       end

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)

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 150
150:       def hstore(arg=(no_arg_given=true; nil))
151:         v = if no_arg_given
152:           Sequel.function(:hstore, self)
153:         else
154:           Sequel.function(:hstore, self, wrap_array(arg))
155:         end
156:         if Sequel.respond_to?(:hstore_op)
157:           v = Sequel.hstore_op(v)
158:         end
159:         v
160:       end
join(joiner="", null=nil)

Alias for to_string

Call the array_length method:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 166
166:       def length(dimension = 1)
167:         function(:array_length, dimension)
168:       end

Call the array_lower method:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 174
174:       def lower(dimension = 1)
175:         function(:array_lower, dimension)
176:       end

Use the overlaps (&&) operator:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 181
181:       def overlaps(other)
182:         bool_op(OVERLAPS, wrap_array(other))
183:       end

Return the receiver.

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 195
195:       def pg_array
196:         self
197:       end

Use the concatentation (||) operator:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 189
189:       def push(other)
190:         array_op(CONCAT, [self, wrap_array(other)])
191:       end

Remove the given element from the array:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 202
202:       def remove(element)
203:         ArrayOp.new(function(:array_remove, element))
204:       end

Replace the given element in the array with another element:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 210
210:       def replace(element, replacement)
211:         ArrayOp.new(function(:array_replace, element, replacement))
212:       end

Call the array_to_string method:

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 220
220:       def to_string(joiner="", null=nil)
221:         function(:array_to_string, joiner, null)
222:       end

Call the unnest method:

  array_op.unnest # unnest(array)

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 228
228:       def unnest(*args)
229:         function(:unnest, *args.map{|a| wrap_array(a)})
230:       end

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

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 235
235:       def unshift(other)
236:         array_op(CONCAT, [wrap_array(other), self])
237:       end

[Validate]