Class Sequel::Postgres::PGArray
In: lib/sequel/extensions/pg_array_ops.rb
lib/sequel/extensions/pg_array.rb
Parent: Object

Represents a PostgreSQL array column value.

Methods

new   op   register   sql_literal_append  

Included Modules

Sequel::SQL::AliasMethods

Classes and Modules

Module Sequel::Postgres::PGArray::DatabaseMethods
Class Sequel::Postgres::PGArray::Creator
Class Sequel::Postgres::PGArray::JSONCreator
Class Sequel::Postgres::PGArray::Parser

Constants

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 = {}   Global hash of database array type name strings to symbols (e.g. ‘double precision’ => :float), used by the schema parsing for array types registered globally.

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

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

[Source]

     # File lib/sequel/extensions/pg_array.rb, line 482
482:       def initialize(array, type=nil)
483:         super(array)
484:         @array_type = type
485:       end

Registers an array type that the extension should handle. Makes a Database instance that has been extended with DatabaseMethods recognize the array type given and set up the appropriate typecasting. Also sets up automatic typecasting for the native postgres adapter, so that on retrieval, the values are automatically converted to PGArray instances. The db_type argument should be the exact database type used (as returned by the PostgreSQL format_type database function). Accepts the following options:

:array_type :The type to automatically cast the array to when literalizing the array. Usually the same as db_type.
:converter :A callable object (e.g. Proc), that is called with each element of the array (usually a string), and should return the appropriate typecasted object.
:oid :The PostgreSQL OID for the array type. This is used by the Sequel postgres adapter to set up automatic type conversion on retrieval from the database.
:parser :Can be set to :json to use the faster JSON-based parser. Note that the JSON-based parser can only correctly handle integers values correctly. It doesn‘t handle full precision for numeric types, and doesn‘t handle NaN/Infinity values for floating point types.
:scalar_oid :Should be the PostgreSQL OID for the scalar version of this array type. If given, automatically sets the :converter option by looking for scalar conversion proc.
:scalar_typecast :Should be a symbol indicating the typecast method that should be called on each element of the array, when a plain array is passed into a database typecast method. For example, for an array of integers, this could be set to :integer, so that the typecast_value_integer method is called on all of the array elements. Defaults to :type_symbol option.
:type_procs :A hash mapping oids to conversion procs, used for looking up the :scalar_oid and value and setting the :oid value. Defaults to the global Sequel::Postgres::PG_TYPES.
:type_symbol :The base of the schema type symbol for this type. For example, if you provide :integer, Sequel will recognize this type as :integer_array during schema parsing. Defaults to the db_type argument.
:typecast_method_map :The map in which to place the database type string to type symbol mapping. Defaults to ARRAY_TYPES.
:typecast_methods_module :If given, a module object to add the typecasting method to. Defaults to DatabaseMethods.

If a block is given, it is treated as the :converter option.

[Source]

     # File lib/sequel/extensions/pg_array.rb, line 147
147:       def self.register(db_type, opts=OPTS, &block)
148:         db_type = db_type.to_s
149:         type = (opts[:type_symbol] || db_type).to_sym
150:         type_procs = opts[:type_procs] || PG_TYPES
151:         mod = opts[:typecast_methods_module] || DatabaseMethods
152:         typecast_method_map = opts[:typecast_method_map] || ARRAY_TYPES
153: 
154:         if converter = opts[:converter]
155:           raise Error, "can't provide both a block and :converter option to register" if block
156:         else
157:           converter = block
158:         end
159: 
160:         if soid = opts[:scalar_oid]
161:           raise Error, "can't provide both a converter and :scalar_oid option to register" if converter 
162:           converter = type_procs[soid]
163:         end
164: 
165:         array_type = (opts[:array_type] || db_type).to_s.dup.freeze
166:         creator = (opts[:parser] == :json ? JSONCreator : Creator).new(array_type, converter)
167: 
168:         typecast_method_map[db_type] = "#{type}_array""#{type}_array"
169: 
170:         define_array_typecast_method(mod, type, creator, opts.fetch(:scalar_typecast, type))
171: 
172:         if oid = opts[:oid]
173:           type_procs[oid] = creator
174:         end
175: 
176:         nil
177:       end

Public Instance methods

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

[Source]

     # File lib/sequel/extensions/pg_array_ops.rb, line 282
282:         def op
283:           ArrayOp.new(self)
284:         end

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

[Source]

     # File lib/sequel/extensions/pg_array.rb, line 490
490:       def sql_literal_append(ds, sql)
491:         at = array_type
492:         if empty? && at
493:           sql << EMPTY_ARRAY
494:         else
495:           sql << ARRAY
496:           _literal_append(sql, ds, to_a)
497:         end
498:         if at
499:           sql << DOUBLE_COLON << at.to_s << EMPTY_BRACKET
500:         end
501:       end

[Validate]