class Athena::Formats::PGSQL

Private Instance Methods

init_in(*) click to toggle source
Calls superclass method Athena::Formats::Base#init_in
# File lib/athena/formats/sql.rb, line 113
def init_in(*)
  @__record_element_ok__ = [String, nil]
  super
end
parse(input, &block) click to toggle source
# File lib/athena/formats/sql.rb, line 77
def parse(input, &block)
  columns, table, num = Hash.new { |h, k| h[k] = [] }, nil, 0

  input.each { |line|
    case line = line.chomp
      when /\ACOPY\s+(\S+)\s+\((.+?)\)\s+FROM\s+stdin;\z/
        columns[table = $1] = $2.split(/\s*,\s*/)
      when /\A\\.\z/
        table = nil
      else
        next unless table

        cols = columns[table]
        next if cols.empty?

        Athena::Record.new(nil, block) { |record|
          line.split(/\t/).each_with_index { |value, index|
            column = cols[index] or next

            if column == record_element
              record.instance_variable_set(:@id, value)
            end

            record.update(column, value, config[column])
          }
        }

        num += 1
    end
  }

  num
end