Class: PG::BinaryEncoder::CopyRow

Inherits:
CopyEncoder show all
Defined in:
ext/pg_copy_coder.c

Overview

This class encodes one row of arbitrary columns for transmission as COPY data in binary format. See the COPY command for description of the format.

It is intended to be used in conjunction with PG::Connection#put_copy_data .

The columns are expected as Array of values. The single values are encoded as defined in the assigned #type_map. If no type_map was assigned, all values are converted to strings by PG::BinaryEncoder::String.

Example with default type map ( TypeMapAllStrings ):

conn.exec "create table my_table (a text,b int,c bool)"
enco = PG::BinaryEncoder::CopyRow.new
conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
  conn.put_copy_data ["astring", "\x00\x00\x00\a", "\x00"]
  conn.put_copy_data ["string2", "\x00\x00\x00*", "\x01"]
end

This creates my_table and inserts two rows with binary fields.

The binary format is less portable and less readable than the text format. It is therefore recommended to either manually assign a type encoder for each column per PG::TypeMapByColumn, or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.

Manually assigning a type encoder works per type map like so:

conn.exec "create table my_table (a text,b int,c bool)"
tm = PG::TypeMapByColumn.new( [
  PG::BinaryEncoder::String.new,
  PG::BinaryEncoder::Int4.new,
  PG::BinaryEncoder::Boolean.new] )
enco = PG::BinaryEncoder::CopyRow.new( type_map: tm )
conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
  conn.put_copy_data ["astring", 7, false]
  conn.put_copy_data ["string2", 42, true]
end

See also PG::BinaryDecoder::CopyRow for the decoding direction with PG::Connection#get_copy_data . And see PG::TextEncoder::CopyRow for an encoder of the COPY text format.

Constant Summary

Constants inherited from Coder

Coder::FORMAT_ERROR_MASK, Coder::FORMAT_ERROR_TO_PARTIAL, Coder::FORMAT_ERROR_TO_RAISE, Coder::FORMAT_ERROR_TO_STRING, Coder::TIMESTAMP_APP_LOCAL, Coder::TIMESTAMP_APP_UTC, Coder::TIMESTAMP_DB_LOCAL, Coder::TIMESTAMP_DB_UTC

Instance Attribute Summary

Attributes inherited from Coder

#name

Method Summary

Methods inherited from CopyCoder

#delimiter, #delimiter=, #null_string, #null_string=, #to_h, #type_map, #type_map=

Methods inherited from Coder

#==, #dup, #flags, #flags=, #format, #format=, #initialize, #inspect, #inspect_short, #marshal_dump, #marshal_load, #oid, #oid=, #to_h

Constructor Details

This class inherits a constructor from PG::Coder