Class: Athena::Formats::PGSQL

Inherits:
Base
  • Object
show all
Defined in:
lib/athena/formats/sql.rb

Direct Known Subclasses

PgSQL

Instance Attribute Summary

Attributes inherited from Base

#config, #output, #record_element

Instance Method Summary collapse

Methods inherited from Base

#convert, #deferred?, directions, format, has_direction?, init, #init, #raw?, #run

Instance Method Details

#parse(input, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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/i
        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