Class: PG::BasicTypeMapBasedOnResult

### Home Classes Methods

### In Files

- lib/pg/basic_type_mapping.rb

### Parent

PG::TypeMapByOid

### Methods

- ::new

### Included Modules

- PG::BasicTypeRegistry

### Files

- LICENSE
- Manifest.txt
- POSTGRES
- README-OS_X.rdoc
- README-Windows.rdoc
- README.ja.rdoc
- README.rdoc
- errorcodes.txt

### Class/Module Index

Quicksearch

- PG
- PG::BasicTypeMapBasedOnResult
- PG::BasicTypeMapForQueries
- PG::BasicTypeMapForResults
- PG::BasicTypeMapForResults::WarningTypeMap
- PG::BasicTypeRegistry
- PG::BasicTypeRegistry::CoderMap
- PG::Coder
- PG::Coder::BinaryFormatting
- PG::CompositeCoder
- PG::Connection
- PG::Connection
- PG::Constants
- PG::CopyCoder
- PG::Error
- PG::NotAllCopyDataRetrieved
- PG::Result
- PG::Result
- PG::TextDecoder
- PG::TextDecoder::Date
- PG::TextDecoder::TimestampWithTimeZone
- PG::TextDecoder::TimestampWithoutTimeZone
- PG::TextEncoder
- PG::TextEncoder::Date
- PG::TextEncoder::TimestampWithTimeZone
- PG::TextEncoder::TimestampWithoutTimeZone
- PG::TypeMapByColumn
- Object
- Object::PGconn
- Object::PGresult

No matching classes.

# PG::BasicTypeMapBasedOnResult

Simple set of rules for type casting common PostgreSQL types from Ruby to PostgreSQL.

OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the PostgreSQL's pg_type table in PG::BasicTypeMapBasedOnResult.new .

This class works equal to PG::BasicTypeMapForResults, but does not define decoders for the given result OIDs, but encoders. So it can be used to type cast field values based on the type OID retrieved by a separate SQL query.

PG::TypeMapByOid#build_column_map(result) can be used to generate a result independent PG::TypeMapByColumn type map, which can subsequently be used to cast query bind parameters or put_copy_data fields.

Example:

    conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[])" )

    # Retrieve table OIDs per empty result set.
    res = conn.exec( "SELECT * FROM copytable LIMIT 0" )
    tm = basic_type_mapping.build_column_map( res )
    row_encoder = PG::TextEncoder::CopyRow.new type_map: tm

    conn.copy_data( "COPY copytable FROM STDIN", row_encoder ) do |res|
      conn.put_copy_data ['a', 123, [5,4,3]]
    end

### Public Class Methods

new(connection) click to toggle source

    # File lib/pg/basic_type_mapping.rb, line 302
    def initialize(connection)
            @coder_maps = build_coder_maps(connection)

            # Populate TypeMapByOid hash with encoders
            @coder_maps.map{|f| f[:encoder].coders }.flatten.each do |coder|
                    add_coder(coder)
            end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.