Class: PG::BasicTypeRegistry::CoderMap

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/basic_type_mapping.rb

Overview

An instance of this class stores the coders that should be used for a given wire format (text or binary) and type cast direction (encoder or decoder).

Constant Summary collapse

DONT_QUOTE_TYPES =

Hash of text types that don’t require quotation, when used within composite types.

type.name => true
%w[
  int2 int4 int8
  float4 float8
  oid
  bool
  date timestamp timestamptz
].inject({}){|h,e| h[e] = true; h }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, coders_by_name, format, arraycoder) ⇒ CoderMap

Returns a new instance of CoderMap.



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
# File 'lib/pg/basic_type_mapping.rb', line 19

def initialize(result, coders_by_name, format, arraycoder)
  coder_map = {}

  _ranges, nodes = result.partition { |row| row['typinput'] == 'range_in' }
  leaves, nodes = nodes.partition { |row| row['typelem'].to_i == 0 }
  arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }

  # populate the enum types
  _enums, leaves = leaves.partition { |row| row['typinput'] == 'enum_in' }
  # enums.each do |row|
  #  coder_map[row['oid'].to_i] = OID::Enum.new
  # end

  # populate the base types
  leaves.find_all { |row| coders_by_name.key?(row['typname']) }.each do |row|
    coder = coders_by_name[row['typname']].dup
    coder.oid = row['oid'].to_i
    coder.name = row['typname']
    coder.format = format
    coder_map[coder.oid] = coder
  end

  _records_by_oid = result.group_by { |row| row['oid'] }

  # populate composite types
  # nodes.each do |row|
  #  add_oid row, records_by_oid, coder_map
  # end

  if arraycoder
    # populate array types
    arrays.each do |row|
      elements_coder = coder_map[row['typelem'].to_i]
      next unless elements_coder

      coder = arraycoder.new
      coder.oid = row['oid'].to_i
      coder.name = row['typname']
      coder.format = format
      coder.elements_type = elements_coder
      coder.needs_quotation = !DONT_QUOTE_TYPES[elements_coder.name]
      coder_map[coder.oid] = coder
    end
  end

  # populate range types
  # ranges.find_all { |row| coder_map.key? row['rngsubtype'].to_i }.each do |row|
  #  subcoder = coder_map[row['rngsubtype'].to_i]
  #  range = OID::Range.new subcoder
  #  coder_map[row['oid'].to_i] = range
  # end

  @coders = coder_map.values
  @coders_by_name = @coders.inject({}){|h, t| h[t.name] = t; h }
  @coders_by_oid = @coders.inject({}){|h, t| h[t.oid] = t; h }
  @typenames_by_oid = result.inject({}){|h, t| h[t['oid'].to_i] = t['typname']; h }
end

Instance Attribute Details

#codersObject (readonly)

Returns the value of attribute coders.



77
78
79
# File 'lib/pg/basic_type_mapping.rb', line 77

def coders
  @coders
end

#coders_by_nameObject (readonly)

Returns the value of attribute coders_by_name.



79
80
81
# File 'lib/pg/basic_type_mapping.rb', line 79

def coders_by_name
  @coders_by_name
end

#coders_by_oidObject (readonly)

Returns the value of attribute coders_by_oid.



78
79
80
# File 'lib/pg/basic_type_mapping.rb', line 78

def coders_by_oid
  @coders_by_oid
end

#typenames_by_oidObject (readonly)

Returns the value of attribute typenames_by_oid.



80
81
82
# File 'lib/pg/basic_type_mapping.rb', line 80

def typenames_by_oid
  @typenames_by_oid
end

Instance Method Details

#coder_by_name(name) ⇒ Object



82
83
84
# File 'lib/pg/basic_type_mapping.rb', line 82

def coder_by_name(name)
  @coders_by_name[name]
end

#coder_by_oid(oid) ⇒ Object



86
87
88
# File 'lib/pg/basic_type_mapping.rb', line 86

def coder_by_oid(oid)
  @coders_by_oid[oid]
end