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
Public Instance
Constants
CONCAT | = | ["(".freeze, " || ".freeze, ")".freeze].freeze | ||
CONTAINED_BY | = | ["(".freeze, " <@ ".freeze, ")".freeze].freeze | ||
CONTAINS | = | ["(".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]
# 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
Call the ALL function:
array_op.all # ALL(array)
Usually used like:
dataset.where(1=>array_op.all) # WHERE (1 = ALL(array))
# File lib/sequel/extensions/pg_array_ops.rb 112 def all 113 function(:ALL) 114 end
Call the ANY function:
array_op.any # ANY(array)
Usually used like:
dataset.where(1=>array_op.any) # WHERE (1 = ANY(array))
# File lib/sequel/extensions/pg_array_ops.rb 124 def any 125 function(:ANY) 126 end
Call the cardinality method:
array_op.cardinality # cardinality(array)
# File lib/sequel/extensions/pg_array_ops.rb 131 def cardinality 132 function(:cardinality) 133 end
Use the contained by (<@) operator:
array_op.contained_by(:a) # (array <@ a)
# File lib/sequel/extensions/pg_array_ops.rb 145 def contained_by(other) 146 bool_op(CONTAINED_BY, wrap_array(other)) 147 end
Use the contains (@>) operator:
array_op.contains(:a) # (array @> a)
# File lib/sequel/extensions/pg_array_ops.rb 138 def contains(other) 139 bool_op(CONTAINS, wrap_array(other)) 140 end
Call the array_dims method:
array_op.dims # array_dims(array)
# File lib/sequel/extensions/pg_array_ops.rb 152 def dims 153 function(:array_dims) 154 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)
# 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
Call the array_length method:
array_op.length # array_length(array, 1) array_op.length(2) # array_length(array, 2)
# File lib/sequel/extensions/pg_array_ops.rb 179 def length(dimension = 1) 180 function(:array_length, dimension) 181 end
Call the array_lower method:
array_op.lower # array_lower(array, 1) array_op.lower(2) # array_lower(array, 2)
# File lib/sequel/extensions/pg_array_ops.rb 187 def lower(dimension = 1) 188 function(:array_lower, dimension) 189 end
Use the overlaps (&&) operator:
array_op.overlaps(:a) # (array && a)
# File lib/sequel/extensions/pg_array_ops.rb 194 def overlaps(other) 195 bool_op(OVERLAPS, wrap_array(other)) 196 end
Return the receiver.
# File lib/sequel/extensions/pg_array_ops.rb 208 def pg_array 209 self 210 end
Use the concatentation (||) operator:
array_op.push(:a) # (array || a) array_op.concat(:a) # (array || a)
# File lib/sequel/extensions/pg_array_ops.rb 202 def push(other) 203 array_op(CONCAT, [self, wrap_array(other)]) 204 end
Remove the given element from the array:
array_op.remove(1) # array_remove(array, 1)
# File lib/sequel/extensions/pg_array_ops.rb 215 def remove(element) 216 ArrayOp.new(function(:array_remove, element)) 217 end
Replace the given element in the array with another element:
array_op.replace(1, 2) # array_replace(array, 1, 2)
# File lib/sequel/extensions/pg_array_ops.rb 223 def replace(element, replacement) 224 ArrayOp.new(function(:array_replace, element, replacement)) 225 end
Call the array_reverse method:
array_op.reverse # array_reverse(array)
# File lib/sequel/extensions/pg_array_ops.rb 230 def reverse 231 function(:array_reverse) 232 end
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)
# 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
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, ':', '*')
# 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
Call the unnest method:
array_op.unnest # unnest(array)
# File lib/sequel/extensions/pg_array_ops.rb 279 def unnest(*args) 280 function(:unnest, *args.map{|a| wrap_array(a)}) 281 end
Use the concatentation (||) operator, reversing the order:
array_op.unshift(:a) # (a || array)
# File lib/sequel/extensions/pg_array_ops.rb 286 def unshift(other) 287 array_op(CONCAT, [wrap_array(other), self]) 288 end