class Sequel::Postgres::ArrayOp
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
- CONTAINED_BY
- CONTAINS
- OVERLAPS
Public Instance Methods
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
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 117 def all 118 function(:ALL) 119 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 129 def any 130 function(:ANY) 131 end
Call the ANY function:
array_op.any # ANY(array)
Usually used like:
dataset.where(1=>array_op.any) # WHERE (1 = ANY(array))
Source
# File lib/sequel/extensions/pg_array_ops.rb 136 def cardinality 137 function(:cardinality) 138 end
Call the cardinality method:
array_op.cardinality # cardinality(array)
Source
# File lib/sequel/extensions/pg_array_ops.rb 150 def contained_by(other) 151 bool_op(CONTAINED_BY, wrap_array(other)) 152 end
Use the contained by (<@) operator:
array_op.contained_by(:a) # (array <@ a)
Source
# File lib/sequel/extensions/pg_array_ops.rb 143 def contains(other) 144 bool_op(CONTAINS, wrap_array(other)) 145 end
Use the contains (@>) operator:
array_op.contains(:a) # (array @> a)
Source
# File lib/sequel/extensions/pg_array_ops.rb 157 def dims 158 function(:array_dims) 159 end
Call the array_dims method:
array_op.dims # array_dims(array)
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
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 191 def length(dimension = 1) 192 function(:array_length, dimension) 193 end
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 199 def lower(dimension = 1) 200 function(:array_lower, dimension) 201 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 164 def ndims 165 function(:array_ndims) 166 end
Call the array_ndims method:
array_op.ndims # array_ndims(array)
Source
# File lib/sequel/extensions/pg_array_ops.rb 228 def overlaps(other) 229 bool_op(OVERLAPS, wrap_array(other)) 230 end
Use the overlaps (&&) operator:
array_op.overlaps(:a) # (array && a)
Source
# File lib/sequel/extensions/pg_array_ops.rb 242 def pg_array 243 self 244 end
Return the receiver.
Source
# File lib/sequel/extensions/pg_array_ops.rb 207 def position(element, offset = 1) 208 function(:array_position, element, offset) 209 end
Call the array_position method:
array_op.position(1) # array_position(array, 1, 1) array_op.position(1, 2) # array_position(array, 1, 2)
Source
# File lib/sequel/extensions/pg_array_ops.rb 214 def positions(element) 215 function(:array_positions, element) 216 end
Call the array_positions method:
array_op.positions(1) # array_positions(array, 1)
Source
# File lib/sequel/extensions/pg_array_ops.rb 221 def prepend(element) 222 SQL::Function.new(:array_prepend, element, self) 223 end
Call the array_prepend method:
array_op.prepend(1) # array_prepend(1, array)
Source
# File lib/sequel/extensions/pg_array_ops.rb 236 def push(other) 237 array_op(CONCAT, [self, wrap_array(other)]) 238 end
Use the concatenation (||) operator:
array_op.push(:a) # (array || a) array_op.concat(:a) # (array || a)
Source
# File lib/sequel/extensions/pg_array_ops.rb 249 def remove(element) 250 ArrayOp.new(function(:array_remove, element)) 251 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 257 def replace(element, replacement) 258 ArrayOp.new(function(:array_replace, element, replacement)) 259 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 264 def reverse 265 function(:array_reverse) 266 end
Call the array_reverse method:
array_op.reverse # array_reverse(array)
Source
# File lib/sequel/extensions/pg_array_ops.rb 271 def sample(element) 272 function(:array_sample, element) 273 end
Call the array_sample method:
array_op.sample(1) # array_sample(array, 1)
Source
# File lib/sequel/extensions/pg_array_ops.rb 278 def shuffle 279 function(:array_shuffle) 280 end
Call the array_shuffle method:
array_op.shuffle # array_shuffle
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
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)
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
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, ':', '*')
Source
# File lib/sequel/extensions/pg_array_ops.rb 327 def trim(number) 328 function(:trim_array, number) 329 end
Call the trim_array method:
array_op.trim(1) # array_trim(array, 1)
Source
# File lib/sequel/extensions/pg_array_ops.rb 334 def unnest(*args) 335 function(:unnest, *args.map{|a| wrap_array(a)}) 336 end
Call the unnest method:
array_op.unnest # unnest(array)
Source
# File lib/sequel/extensions/pg_array_ops.rb 341 def unshift(other) 342 array_op(CONCAT, [wrap_array(other), self]) 343 end
Use the concatenation (||) operator, reversing the order:
array_op.unshift(:a) # (a || array)
Private Instance Methods
Source
# File lib/sequel/extensions/pg_array_ops.rb 349 def array_op(str, args) 350 ArrayOp.new(Sequel::SQL::PlaceholderLiteralString.new(str, args)) 351 end
Return a placeholder literal with the given str and args, wrapped in an ArrayOp, used by operators that return arrays.
Source
# File lib/sequel/extensions/pg_array_ops.rb 355 def bool_op(str, other) 356 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 357 end
Return a placeholder literal with the given str and args, wrapped in a boolean expression, used by operators that return booleans.
Source
# File lib/sequel/extensions/pg_array_ops.rb 361 def function(name, *args) 362 SQL::Function.new(name, self, *args) 363 end
Return a function with the given name, and the receiver as the first argument, with any additional arguments given.
Source
# File lib/sequel/extensions/pg_array_ops.rb 367 def wrap_array(arg) 368 if arg.instance_of?(Array) 369 Sequel.pg_array(arg) 370 else 371 arg 372 end 373 end