Class: FakePipe::Postgres::CopyBlock

Inherits:
TextBlock
  • Object
show all
Defined in:
lib/fake_pipe/postgres/copy_block.rb

Overview

Finds COPY… text blocks inside of ‘pg_dumps`

Constant Summary collapse

DELIMITER =
"\t"
COLUMN_SPLITTER =
/,\s*/

Instance Attribute Summary

Attributes inherited from TextBlock

#delegate, #start_match, #table

Instance Method Summary collapse

Methods inherited from TextBlock

#end_text?, #initialize, #match_start_text, #start_text?

Constructor Details

This class inherits a constructor from FakePipe::TextBlock

Instance Method Details

#on_start_text(match, line) ⇒ Hash<Integer,String>

Returns Index for column ordinal and column name: { 1 => column_name }.

Returns:

  • (Hash<Integer,String>)

    Index for column ordinal and column name: { 1 => column_name }



16
17
18
19
20
# File 'lib/fake_pipe/postgres/copy_block.rb', line 16

def on_start_text(match, line)
  @table = match[:table]
  @columns = match[:columns].split(COLUMN_SPLITTER)
  @column_idx = Hash[@columns.map.with_index { |name, i| [i, name] }]
end

#parse(line) ⇒ String

Postgres COPY format is NOT CSV. > www.postgresql.org/docs/9.1/static/sql-copy.html

Returns:

  • (String)

    maybe mutated by ‘delegate.on_cell`



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fake_pipe/postgres/copy_block.rb', line 26

def parse(line)
  row = line.split(DELIMITER)
  faked = row.map.with_index do |cell, i|
    if cell.blank? || cell == '\N'
      # Don't acknowledge null cells
      cell
    else
      delegate.on_cell(table: @table, column: @column_idx[i], cell: cell)
    end
  end
  faked.join(DELIMITER)
end