class Sequel::Postgres::PGArray

Represents a PostgreSQL array column value.

:nocov:

Attributes

array_type[RW]

The type of this array. May be nil if no type was given. If a type is provided, the array is automatically casted to this type when literalizing. This type is the underlying type, not the array type itself, so for an int4[] database type, it should be :int4 or 'int4'

Public Class Methods

new(array, type=nil) click to toggle source

Set the array to delegate to, and a database type.

Calls superclass method
    # File lib/sequel/extensions/pg_array.rb
458 def initialize(array, type=nil)
459   super(array)
460   @array_type = type
461 end

Public Instance Methods

op() click to toggle source

Wrap the PGArray instance in an ArrayOp, allowing you to easily use the PostgreSQL array functions and operators with literal arrays.

    # File lib/sequel/extensions/pg_array_ops.rb
294 def op
295   ArrayOp.new(self)
296 end
sequel_auto_param_type(ds) click to toggle source

Allow automatic parameterization of the receiver if all elements can be can be automatically parameterized.

    # File lib/sequel/extensions/pg_array.rb
481 def sequel_auto_param_type(ds)
482   if array_type && all?{|x| nil == x || ds.send(:auto_param_type, x)}
483     "::#{array_type}[]"
484   end
485 end
sql_literal_append(ds, sql) click to toggle source

Append the array SQL to the given sql string. If the receiver has a type, add a cast to the database array type.

    # File lib/sequel/extensions/pg_array.rb
466 def sql_literal_append(ds, sql)
467   at = array_type
468   if empty? && at
469     sql << "'{}'"
470   else
471     sql << "ARRAY"
472     _literal_append(sql, ds, to_a)
473   end
474   if at
475     sql << '::' << at.to_s << '[]'
476   end
477 end

Private Instance Methods

_literal_append(sql, ds, array) click to toggle source

Recursive method that handles multi-dimensional arrays, surrounding each with [] and interspersing entries with ,.

    # File lib/sequel/extensions/pg_array.rb
492 def _literal_append(sql, ds, array)
493   sql << '['
494   comma = false
495   commas = ','
496   array.each do |i|
497     sql << commas if comma
498     if i.is_a?(Array)
499       _literal_append(sql, ds, i)
500     else
501       ds.literal_append(sql, i)
502     end
503     comma = true
504   end
505   sql << ']'
506 end