class Fias::Import::Copy

Constants

BLOCK_SIZE

Attributes

dbf[R]
table_name[R]

Public Class Methods

new(db, table_name, dbf, types = {}) click to toggle source
# File lib/fias/import/copy.rb, line 6
def initialize(db, table_name, dbf, types = {})
  @db = db
  @table_name = table_name.to_sym
  @dbf = dbf
  @encoder = PgDataEncoder::EncodeForCopy.new(
    column_types: map_types(types)
  )
end

Public Instance Methods

copy() click to toggle source
# File lib/fias/import/copy.rb, line 23
def copy
  prepare
  copy_into
end
encode() { || ... } click to toggle source
# File lib/fias/import/copy.rb, line 15
def encode
  @dbf.each do |record|
    line = record.to_a.map { |v| v == '' ? nil : v }
    @encoder.add(line)
    yield if block_given?
  end
end

Private Instance Methods

columns() click to toggle source
# File lib/fias/import/copy.rb, line 38
def columns
  @columns ||= @dbf.columns.map(&:name).map(&:downcase).map(&:to_sym)
end
copy_into() click to toggle source
# File lib/fias/import/copy.rb, line 47
def copy_into
  io = @encoder.get_io

  @db.copy_into(@table_name.to_sym, columns: columns, format: :binary) do
    begin
      io.readpartial(BLOCK_SIZE)
    rescue EOFError => _e
      nil
    end
  end
end
map_types(types) click to toggle source
# File lib/fias/import/copy.rb, line 30
def map_types(types)
  types = types.map do |name, type|
    index = columns.index(name.to_sym)
    [index, type] if index
  end
  Hash[*types.compact.flatten]
end
prepare() click to toggle source
# File lib/fias/import/copy.rb, line 42
def prepare
  @db[@table_name].truncate
  @db.run('SET client_min_messages TO warning;')
end