module PG::BasicTypeRegistry - pg-0.18.4 Documentation

# module PG::BasicTypeRegistry

### Constants

CODERS_BY_NAME

  The key of this hash maps to the `typname` column from the table. encoder_map is then dynamically built with oids as the key and Type objects as values.

ValidDirections
ValidFormats

### Private Class Methods

alias_type(format, new, old) click to toggle source

Alias the `old` type to the `new` type.

    # File lib/pg/basic_type_mapping.rb, line 147
    def self.alias_type(format, new, old)
            CODERS_BY_NAME[format][:encoder][new] = CODERS_BY_NAME[format][:encoder][old]
            CODERS_BY_NAME[format][:decoder][new] = CODERS_BY_NAME[format][:decoder][old]
    end

register_type(format, name, encoder_class, decoder_class) click to toggle source

Register an OID type named `name` with a typecasting encoder and decoder object in `type`. `name` should correspond to the `typname` column in the `pg_type` table.

    # File lib/pg/basic_type_mapping.rb, line 140
    def self.register_type(format, name, encoder_class, decoder_class)
            CODERS_BY_NAME[format] ||= { encoder: {}, decoder: {} }
            CODERS_BY_NAME[format][:encoder][name] = encoder_class.new(name: name, format: format) if encoder_class
            CODERS_BY_NAME[format][:decoder][name] = decoder_class.new(name: name, format: format) if decoder_class
    end

### Protected Instance Methods

check_format_and_direction(format, direction) click to toggle source

    # File lib/pg/basic_type_mapping.rb, line 126
    def check_format_and_direction(format, direction)
            raise(ArgumentError, "Invalid format value %p" % format) unless ValidFormats[format]
            raise(ArgumentError, "Invalid direction %p" % direction) unless ValidDirections[direction]
    end

### Private Instance Methods

build_coder_maps(connection) click to toggle source

    # File lib/pg/basic_type_mapping.rb, line 97
    def build_coder_maps(connection)
            if supports_ranges?(connection)
                    result = connection.exec <<-SQL
                            SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype
                            FROM pg_type as t
                            LEFT JOIN pg_range as r ON oid = rngtypid
                    SQL
            else
                    result = connection.exec <<-SQL
                            SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput
                            FROM pg_type as t
                    SQL
            end

            [
                    [0, :encoder, PG::TextEncoder::Array],
                    [0, :decoder, PG::TextDecoder::Array],
                    [1, :encoder, nil],
                    [1, :decoder, nil],
            ].inject([]) do |h, (format, direction, arraycoder)|
                    h[format] ||= {}
                    h[format][direction] = CoderMap.new result, CODERS_BY_NAME[format][direction], format, arraycoder
                    h
            end
    end

supports_ranges?(connection) click to toggle source

    # File lib/pg/basic_type_mapping.rb, line 93
    def supports_ranges?(connection)
            connection.server_version >= 90200
    end