class PG::BasicTypeMapBasedOnResult


Binary COPY is faster than text format but less portable and less readable and pg offers fewer en-/decoders of database types.
database types using binary copy and value format.
This inserts a single row into copytable with type casts from ruby to
end
conn.put_copy_data [‘a’, 123, “xffx00”.b, Time.now]
conn.copy_data( “COPY copytable FROM STDIN WITH (FORMAT binary)”, row_encoder ) do |res|
row_encoder = PG::BinaryEncoder::CopyRow.new type_map: tm
tm = btm.build_column_map( res )
# Build a PG::TypeMapByColumn with encoders suitable for copytable.
btm = PG::BasicTypeMapBasedOnResult.new(conn)
# Build a type map for common ruby to database type encoders.
res = conn.exec_params( “SELECT * FROM copytable LIMIT 0”, [], 1 )
# Retrieve table OIDs per empty result set in binary format.
conn.exec( “CREATE TEMP TABLE copytable (t TEXT, i INT, blob bytea, created_at timestamp)” )
Very similar with binary format:
database types using text format.
This inserts a single row into copytable with type casts from ruby to
end
conn.put_copy_data [‘a’, 123, [5,4,3]]
conn.copy_data( “COPY copytable FROM STDIN”, row_encoder ) do |res|
row_encoder = PG::TextEncoder::CopyRow.new type_map: tm
tm = btm.build_column_map( res )
# Build a PG::TypeMapByColumn with encoders suitable for copytable.
btm = PG::BasicTypeMapBasedOnResult.new(conn)
# Build a type map for common ruby to database type encoders.
res = conn.exec( “SELECT * FROM copytable LIMIT 0” )
# Retrieve table OIDs per empty result set.
conn.exec( “CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[])” )
Example:
or #put_copy_data fields.
PG::TypeMapByColumn type map, which can subsequently be used to cast query bind parameters
PG::TypeMapByOid#build_column_map(result) can be used to generate a result independent
the type OID retrieved by a separate SQL query.
the given result OIDs, but encoders. So it can be used to type cast field values based on
This class works equal to PG::BasicTypeMapForResults, but does not define decoders for
PostgreSQL’s pg_type table in PG::BasicTypeMapBasedOnResult.new .
OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
to PostgreSQL.
Simple set of rules for type casting common PostgreSQL types from Ruby

def initialize(connection_or_coder_maps, registry: nil)

def initialize(connection_or_coder_maps, registry: nil)
	@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)
	# Populate TypeMapByOid hash with encoders
	@coder_maps.each_format(:encoder).flat_map{|f| f.coders }.each do |coder|
		add_coder(coder)
	end
end