module ArJdbc::PostgreSQL::OIDTypes

@private

Constants

TypeMap

Public Instance Methods

disable_extension(name) click to toggle source

@override

Calls superclass method
# File lib/arjdbc/postgresql/oid_types.rb, line 34
def disable_extension(name)
  result = super(name)
  @extensions = nil
  reload_type_map
  result
end
dup() click to toggle source
Calls superclass method
# File lib/arjdbc/postgresql/oid_types.rb, line 84
def dup
  dup = super # make sure @mapping is not shared
  dup.instance_variable_set(:@mapping, @mapping.dup)
  dup
end
enable_extension(name) click to toggle source

@override

Calls superclass method
# File lib/arjdbc/postgresql/oid_types.rb, line 26
def enable_extension(name)
  result = super(name)
  @extensions = nil
  reload_type_map
  result
end
extensions() click to toggle source

@override

Calls superclass method
# File lib/arjdbc/postgresql/oid_types.rb, line 42
def extensions
  @extensions ||= super
end
get_oid_type(oid, fmod, column_name) click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 52
def get_oid_type(oid, fmod, column_name)
  type_map.fetch(oid, fmod) {
    warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
    type_map[oid] = OID::Identity.new
  }
end
lookup_cast_type(sql_type) click to toggle source

@override

Calls superclass method
# File lib/arjdbc/postgresql/oid_types.rb, line 47
def lookup_cast_type(sql_type)
  oid = execute("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA")
  super oid.first['oid'].to_i
end
reload_type_map() click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 106
def reload_type_map
  if ( @type_map ||= nil )
    @type_map.clear
    initialize_type_map(@type_map)
  end
end
type_map() click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 91
def type_map
  # NOTE: our type_map is lazy (on AR < 4.2)
  # ... since it's only used for `adapter.accessor`
  @type_map ||= begin
    if type_map = @@type_map_cache[ type_cache_key ]
      type_map.dup
    else
      type_map = TypeMap.new
      initialize_type_map(type_map)
      cache_type_map(type_map)
      type_map
    end
  end
end

Private Instance Methods

add_oid(row, records_by_oid, type_map) click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 125
def add_oid(row, records_by_oid, type_map)
  return type_map if type_map.key? row['type_elem'].to_i

  if OID.registered_type? typname = row['typname']
    # this composite type is explicitly registered
    vector = OID::NAMES[ typname ]
  else
    # use the default for composite types
    unless type_map.key? typelem = row['typelem'].to_i
      add_oid records_by_oid[ row['typelem'] ], records_by_oid, type_map
    end

    vector = OID::Vector.new row['typdelim'], type_map[typelem]
  end

  type_map[ row['oid'].to_i ] = vector
  type_map
end
cache_type_map(type_map) click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 115
def cache_type_map(type_map)
  @@type_map_cache_lock.synchronize do
    @@type_map_cache[ type_cache_key ] = type_map
  end
end
initialize_type_map(type_map) click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 144
def initialize_type_map(type_map)
  result = execute('SELECT oid, typname, typelem, typdelim, typinput FROM pg_type', 'SCHEMA')
  leaves, nodes = result.partition { |row| row['typelem'].to_s == '0' }
  # populate the leaf nodes
  leaves.find_all { |row| OID.registered_type? row['typname'] }.each do |row|
    type_map[ row['oid'].to_i ] = OID::NAMES[ row['typname'] ]
  end

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

  arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }

  # populate composite types
  nodes.each { |row| add_oid row, records_by_oid, type_map }

  # populate array types
  arrays.find_all { |row| type_map.key? row['typelem'].to_i }.each do |row|
    array = OID::Array.new  type_map[ row['typelem'].to_i ]
    type_map[ row['oid'].to_i ] = array
  end
end
load_additional_types(type_map, oids = nil) click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 237
def load_additional_types(type_map, oids = nil)
  if supports_ranges?
    query = <<-SQL
      SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
      FROM pg_type as t
      LEFT JOIN pg_range as r ON oid = rngtypid
    SQL
  else
    query = <<-SQL
      SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, t.typtype, t.typbasetype
      FROM pg_type as t
    SQL
  end

  initializer = OID::TypeMapInitializer.new(type_map)

  if oids
    query << ( "WHERE t.oid::integer IN (%s)" % oids.join(", ") )
  else
    # query_conditions_for_initial_load only available since AR > 4.2.1
    if initializer.respond_to?(:query_conditions_for_initial_load)
      query << initializer.query_conditions_for_initial_load(type_map)
    end
  end

  records = execute(query, 'SCHEMA')
  initializer.run(records)
end
type_cache_key() click to toggle source
# File lib/arjdbc/postgresql/oid_types.rb, line 121
def type_cache_key
  config.hash + ( 7 * extensions.hash )
end