Class: PG::BinaryDecoder::CopyRow

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

Overview

This class decodes one row of arbitrary columns received 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#get_copy_data .

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

Example with default type map ( TypeMapAllStrings ):

conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")

deco = PG::BinaryDecoder::CopyRow.new
conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
  while row=conn.get_copy_data
    p row
  end
end

This prints all rows of my_table in binary format:

["astring", "\x00\x00\x00\a", "\x00"]
["string2", "\x00\x00\x00*", "\x01"]

Example with column based type map:

tm = PG::TypeMapByColumn.new( [
  PG::BinaryDecoder::String.new,
  PG::BinaryDecoder::Integer.new,
  PG::BinaryDecoder::Boolean.new] )
deco = PG::BinaryDecoder::CopyRow.new( type_map: tm )
conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
  while row=conn.get_copy_data
    p row
  end
end

This prints the rows with type casted columns:

["astring", 7, false]
["string2", 42, true]

Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults can be used to assign them based on the table OIDs.

See also PG::BinaryEncoder::CopyRow for the encoding direction with PG::Connection#put_copy_data . And see PG::TextDecoder::CopyRow for a decoder 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