123456789_123456789_123456789_123456789_123456789_

Class: PG::BasicTypeRegistry::CoderMapsBundle

Relationships & Source Files
Inherits: Object
Defined in: lib/pg/basic_type_registry.rb

Overview

An instance of this class stores CoderMap instances to be used for text and binary wire formats as well as encoder and decoder directions.

A CoderMapsBundle instance retrieves all type definitions from the PostgreSQL server and matches them with the coder definitions of the global ::PG::BasicTypeRegistry . It provides 4 separate CoderMap instances for the combinations of the two formats and directions.

A CoderMapsBundle instance can be used to initialize an instance of

by passing it instead of the connection object like so:

conn = PG::Connection.new
maps = PG::BasicTypeRegistry::CoderMapsBundle.new(conn)
conn.type_map_for_results = PG::BasicTypeMapForResults.new(maps)

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(connection, registry: nil) ⇒ CoderMapsBundle

[ GitHub ]

  
# File 'lib/pg/basic_type_registry.rb', line 113

def initialize(connection, registry: nil)
	registry ||= DEFAULT_TYPE_REGISTRY

	result = connection.exec(<<-SQL).to_a
		SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput
		FROM pg_type as t
		JOIN pg_proc as ti ON ti.oid = t.typinput
	SQL

	init_maps(registry, result.freeze)
	freeze
end

Instance Attribute Details

#typenames_by_oid (readonly)

[ GitHub ]

  
# File 'lib/pg/basic_type_registry.rb', line 111

attr_reader :typenames_by_oid

Instance Method Details

#each_format(direction)

[ GitHub ]

  
# File 'lib/pg/basic_type_registry.rb', line 142

def each_format(direction)
	@maps.map { |f| f[direction] }
end

#init_maps(registry, result) (private)

[ GitHub ]

  
# File 'lib/pg/basic_type_registry.rb', line 126

private def init_maps(registry, result)
	@maps = [
		[0, :encoder, PG::TextEncoder::Array],
		[0, :decoder, PG::TextDecoder::Array],
		[1, :encoder, PG::BinaryEncoder::Array],
		[1, :decoder, PG::BinaryDecoder::Array],
	].inject([]) do |h, (format, direction, arraycoder)|
		coders = registry.coders_for(format, direction) || {}
		h[format] ||= {}
		h[format][direction] = CoderMap.new(result, coders, format, arraycoder)
		h
	end.each{|h| h.freeze }.freeze

	@typenames_by_oid = result.inject({}){|h, t| h[t['oid'].to_i] = t['typname']; h }.freeze
end

#map_for(format, direction)

[ GitHub ]

  
# File 'lib/pg/basic_type_registry.rb', line 146

def map_for(format, direction)
	@maps[format][direction]
end