class RedshiftCsvFile

Reads CSV file generated by Redshift UNLOAD statement (with option ADDQUOTES ESCAPE). UNLOAD escapes data by '' (backslash character), we cannot use standard CSV class.

Constants

UNESCAPE_MAP

Public Class Methods

new(f) click to toggle source
f

IO

# File lib/redshift_csv_file.rb, line 9
def initialize(f)
  @f = f
  @s = ScanBuffer.new(@f)
end

Public Instance Methods

each()
Alias for: each_row
each_row() { |row| ... } click to toggle source
# File lib/redshift_csv_file.rb, line 14
def each_row
  s = @s
  while row = parse_row(@s)
    yield row
  end
end
Also aliased as: each
read_row() click to toggle source
# File lib/redshift_csv_file.rb, line 23
def read_row
  return nil if @s.eof?
  parse_row(@s)
end

Private Instance Methods

parse_row(s) click to toggle source
# File lib/redshift_csv_file.rb, line 30
def parse_row(s)
  s.next_row or return nil
  row = []
  begin
    first = false
    column = s.scan_column
    unless column
      raise MalformedCSVException, "CSV parse error: unterminated column or row at line #{s.lineno}"
    end
    row.push unescape_column(column)
  end while s.read_separator
  unless s.read_eol
    raise MalformedCSVException, "CSV parse error: missing column separator at line #{s.lineno}"
  end
  row
end
unescape_column(col) click to toggle source
# File lib/redshift_csv_file.rb, line 53
def unescape_column(col)
  charmap = UNESCAPE_MAP
  col[1...-1].gsub(/\\./m) {|s| charmap[s] || s[1,1] }
end