Class: Sequel::Postgres::PGArray

Inherits:
Array show all
Includes:
SQL::AliasMethods
Defined in:
lib/sequel/extensions/pg_array.rb,
lib/sequel/extensions/pg_array_ops.rb

Overview

Represents a PostgreSQL array column value.

Defined Under Namespace

Modules: DatabaseMethods Classes: Creator, Parser

Constant Summary collapse

ARRAY =
"ARRAY".freeze
DOUBLE_COLON =
'::'.freeze
EMPTY_ARRAY =
"'{}'".freeze
EMPTY_BRACKET =
'[]'.freeze
OPEN_BRACKET =
'['.freeze
CLOSE_BRACKET =
']'.freeze
COMMA =
','.freeze
BACKSLASH =
'\\'.freeze
EMPTY_STRING =
''.freeze
OPEN_BRACE =
'{'.freeze
CLOSE_BRACE =
'}'.freeze
NULL =
'NULL'.freeze
QUOTE =
'"'.freeze
ARRAY_TYPES =

SEQUEL5: Remove

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SQL::AliasMethods

#as

Methods inherited from Array

#case, #pg_array, #pg_json, #pg_jsonb, #pg_row, #sql_array, #sql_expr, #sql_negate, #sql_or, #sql_string_join, #sql_value_list, #~

Constructor Details

#initialize(array, type = nil) ⇒ PGArray

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



558
559
560
561
# File 'lib/sequel/extensions/pg_array.rb', line 558

def initialize(array, type=nil)
  super(array)
  @array_type = type
end

Instance Attribute Details

#array_typeObject

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’



555
556
557
# File 'lib/sequel/extensions/pg_array.rb', line 555

def array_type
  @array_type
end

Class Method Details

.register(db_type, opts = OPTS, &block) ⇒ Object

SEQUEL5: Remove



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
# File 'lib/sequel/extensions/pg_array.rb', line 113

def self.register(db_type, opts=OPTS, &block)
  Sequel::Deprecation.deprecate("Sequel::Postgres::PGArray.register", "Use Database#register_array_type on a Database instance using the pg_array extension") unless opts[:skip_deprecation_warning]

  db_type = db_type.to_s
  type = (opts[:type_symbol] || db_type).to_sym
  type_procs = opts[:type_procs] || PG__TYPES
  mod = opts[:typecast_methods_module] || DatabaseMethods
  typecast_method_map = opts[:typecast_method_map] || ARRAY_TYPES

  if converter = opts[:converter]
    raise Error, "can't provide both a block and :converter option to register" if block
  else
    converter = block
  end

  if soid = opts[:scalar_oid]
    raise Error, "can't provide both a converter and :scalar_oid option to register" if converter 
    converter = type_procs[soid]
  end

  array_type = (opts[:array_type] || db_type).to_s.dup.freeze
  creator = Creator.new(array_type, converter)

  typecast_method_map[db_type] = :"#{type}_array"

  define_array_typecast_method(mod, type, creator, opts.fetch(:scalar_typecast, type))

  if oid = opts[:oid]
    if opts[:skip_deprecation_warning]
      def creator.call(s)
        Sequel::Deprecation.deprecate("Conversion proc for #{type}[] added globally by pg_array or other pg_* extension", "Load the appropriate pg_* extension(s) into the Database instance")
        super
      end
    end
    type_procs[oid] = creator
  end

  nil
end

Instance Method Details

#opObject

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



286
287
288
# File 'lib/sequel/extensions/pg_array_ops.rb', line 286

def op
  ArrayOp.new(self)
end

#sql_literal_append(ds, sql) ⇒ Object

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



566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/sequel/extensions/pg_array.rb', line 566

def sql_literal_append(ds, sql)
  at = array_type
  if empty? && at
    sql << "'{}'"
  else
    sql << "ARRAY"
    _literal_append(sql, ds, to_a)
  end
  if at
    sql << '::' << at.to_s << '[]'
  end
end