PG::TextDecoder::

CopyRow class

This class decodes one row of arbitrary columns received as COPY data in text 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::TextDecoder::String.

Example with default type map ( TypeMapAllStrings ):

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

deco = PG::TextDecoder::CopyRow.new
conn.copy_data "COPY my_table TO STDOUT", deco do
  while row=conn.get_copy_data
    p row
  end
end

This prints all rows of my_table :

["astring", "7", "f"]
["string2", "42", "t"]

Example with column based type map:

tm = PG::TypeMapByColumn.new( [
  PG::TextDecoder::String.new,
  PG::TextDecoder::Integer.new,
  PG::TextDecoder::Boolean.new] )
deco = PG::TextDecoder::CopyRow.new( type_map: tm )
conn.copy_data "COPY my_table TO STDOUT", 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::TextEncoder::CopyRow for the encoding direction with PG::Connection#put_copy_data .