/usr/lib/ruby/vendor_ruby/sequel/extensions/pg_array_ops.rb is in ruby-sequel 4.15.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | # The pg_array_ops extension adds support to Sequel's DSL to make
# it easier to call PostgreSQL array functions and operators.
#
# To load the extension:
#
# Sequel.extension :pg_array_ops
#
# The most common usage is passing an expression to Sequel.pg_array_op:
#
# ia = Sequel.pg_array_op(:int_array_column)
#
# If you have also loaded the pg_array extension, you can use
# Sequel.pg_array as well:
#
# ia = Sequel.pg_array(:int_array_column)
#
# Also, on most Sequel expression objects, you can call the pg_array
# method:
#
# ia = Sequel.expr(:int_array_column).pg_array
#
# If you have loaded the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc],
# or you have loaded the core_refinements extension
# and have activated refinements for the file, you can also use Symbol#pg_array:
#
# ia = :int_array_column.pg_array
#
# This creates a Sequel::Postgres::ArrayOp object that can be used
# for easier querying:
#
# ia[1] # int_array_column[1]
# ia[1][2] # int_array_column[1][2]
#
# ia.contains(:other_int_array_column) # @>
# ia.contained_by(:other_int_array_column) # <@
# ia.overlaps(:other_int_array_column) # &&
# ia.concat(:other_int_array_column) # ||
#
# ia.push(1) # int_array_column || 1
# ia.unshift(1) # 1 || int_array_column
#
# ia.any # ANY(int_array_column)
# ia.all # ALL(int_array_column)
# ia.cardinality # cardinality(int_array_column)
# ia.dims # array_dims(int_array_column)
# ia.hstore # hstore(int_array_column)
# ia.hstore(:a) # hstore(int_array_column, a)
# ia.length # array_length(int_array_column, 1)
# ia.length(2) # array_length(int_array_column, 2)
# ia.lower # array_lower(int_array_column, 1)
# ia.lower(2) # array_lower(int_array_column, 2)
# ia.join # array_to_string(int_array_column, '', NULL)
# ia.join(':') # array_to_string(int_array_column, ':', NULL)
# ia.join(':', ' ') # array_to_string(int_array_column, ':', ' ')
# ia.unnest # unnest(int_array_column)
# ia.unnest(:b) # unnest(int_array_column, b)
#
# See the PostgreSQL array function and operator documentation for more
# details on what these functions and operators do.
#
# If you are also using the pg_array extension, you should load it before
# loading this extension. Doing so will allow you to use PGArray#op to get
# an ArrayOp, allowing you to perform array operations on array literals.
#
# In order for #hstore to automatically wrap the returned value correctly in
# an HStoreOp, you need to load the pg_hstore_ops extension.
#
module Sequel
module Postgres
# 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
class ArrayOp < Sequel::SQL::Wrapper
CONCAT = ["(".freeze, " || ".freeze, ")".freeze].freeze
CONTAINS = ["(".freeze, " @> ".freeze, ")".freeze].freeze
CONTAINED_BY = ["(".freeze, " <@ ".freeze, ")".freeze].freeze
OVERLAPS = ["(".freeze, " && ".freeze, ")".freeze].freeze
# Access a member of the array, returns an SQL::Subscript instance:
#
# array_op[1] # array[1]
def [](key)
s = Sequel::SQL::Subscript.new(self, [key])
s = ArrayOp.new(s) if key.is_a?(Range)
s
end
# Call the ALL function:
#
# array_op.all # ALL(array)
#
# Usually used like:
#
# dataset.where(1=>array_op.all)
# # WHERE (1 = ALL(array))
def all
function(:ALL)
end
# Call the ANY function:
#
# array_op.all # ANY(array)
#
# Usually used like:
#
# dataset.where(1=>array_op.any)
# # WHERE (1 = ANY(array))
def any
function(:ANY)
end
# Call the cardinality method:
#
# array_op.cardinality # cardinality(array)
def cardinality
function(:cardinality)
end
# Use the contains (@>) operator:
#
# array_op.contains(:a) # (array @> a)
def contains(other)
bool_op(CONTAINS, wrap_array(other))
end
# Use the contained by (<@) operator:
#
# array_op.contained_by(:a) # (array <@ a)
def contained_by(other)
bool_op(CONTAINED_BY, wrap_array(other))
end
# Call the array_dims method:
#
# array_op.dims # array_dims(array)
def dims
function(:array_dims)
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)
def hstore(arg=(no_arg_given=true; nil))
v = if no_arg_given
Sequel.function(:hstore, self)
else
Sequel.function(:hstore, self, wrap_array(arg))
end
if Sequel.respond_to?(:hstore_op)
v = Sequel.hstore_op(v)
end
v
end
# Call the array_length method:
#
# array_op.length # array_length(array, 1)
# array_op.length(2) # array_length(array, 2)
def length(dimension = 1)
function(:array_length, dimension)
end
# Call the array_lower method:
#
# array_op.lower # array_lower(array, 1)
# array_op.lower(2) # array_lower(array, 2)
def lower(dimension = 1)
function(:array_lower, dimension)
end
# Use the overlaps (&&) operator:
#
# array_op.overlaps(:a) # (array && a)
def overlaps(other)
bool_op(OVERLAPS, wrap_array(other))
end
# Use the concatentation (||) operator:
#
# array_op.push(:a) # (array || a)
# array_op.concat(:a) # (array || a)
def push(other)
array_op(CONCAT, [self, wrap_array(other)])
end
alias concat push
# Return the receiver.
def pg_array
self
end
# Remove the given element from the array:
#
# array_op.remove(1) # array_remove(array, 1)
def remove(element)
ArrayOp.new(function(:array_remove, element))
end
# Replace the given element in the array with another
# element:
#
# array_op.replace(1, 2) # array_replace(array, 1, 2)
def replace(element, replacement)
ArrayOp.new(function(:array_replace, element, replacement))
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, ':', '*')
def to_string(joiner="", null=nil)
function(:array_to_string, joiner, null)
end
alias join to_string
# Call the unnest method:
#
# array_op.unnest # unnest(array)
def unnest(*args)
function(:unnest, *args.map{|a| wrap_array(a)})
end
# Use the concatentation (||) operator, reversing the order:
#
# array_op.unshift(:a) # (a || array)
def unshift(other)
array_op(CONCAT, [wrap_array(other), self])
end
private
# Return a placeholder literal with the given str and args, wrapped
# in an ArrayOp, used by operators that return arrays.
def array_op(str, args)
ArrayOp.new(Sequel::SQL::PlaceholderLiteralString.new(str, args))
end
# Return a placeholder literal with the given str and args, wrapped
# in a boolean expression, used by operators that return booleans.
def bool_op(str, other)
Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other]))
end
# Return a function with the given name, and the receiver as the first
# argument, with any additional arguments given.
def function(name, *args)
SQL::Function.new(name, self, *args)
end
# Automatically wrap argument in a PGArray if it is a plain Array.
# Requires that the pg_array extension has been loaded to work.
def wrap_array(arg)
if arg.instance_of?(Array)
Sequel.pg_array(arg)
else
arg
end
end
end
module ArrayOpMethods
# Wrap the receiver in an ArrayOp so you can easily use the PostgreSQL
# array functions and operators with it.
def pg_array
ArrayOp.new(self)
end
end
if defined?(PGArray)
class PGArray
# Wrap the PGArray instance in an ArrayOp, allowing you to easily use
# the PostgreSQL array functions and operators with literal arrays.
def op
ArrayOp.new(self)
end
end
end
end
module SQL::Builders
# Return the object wrapped in an Postgres::ArrayOp.
def pg_array_op(v)
case v
when Postgres::ArrayOp
v
else
Postgres::ArrayOp.new(v)
end
end
end
class SQL::GenericExpression
include Sequel::Postgres::ArrayOpMethods
end
class LiteralString
include Sequel::Postgres::ArrayOpMethods
end
end
# :nocov:
if Sequel.core_extensions?
class Symbol
include Sequel::Postgres::ArrayOpMethods
end
end
if defined?(Sequel::CoreRefinements)
module Sequel::CoreRefinements
refine Symbol do
include Sequel::Postgres::ArrayOpMethods
end
end
end
# :nocov:
|