Class: PgDataEncoder::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_data_encoder/decoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Decoder

Returns a new instance of Decoder.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pg_data_encoder/decoder.rb', line 6

def initialize(options = {})
  @options = options
  @closed = false
  if options[:column_types].kind_of?(Array)
    map = {}
    options[:column_types].each_with_index {|c, i|
      map[i] = c
    }
    options[:column_types] = map
  else
    options[:column_types] ||= {}
  end
  @io = nil
end

Instance Method Details

#eachObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pg_data_encoder/decoder.rb', line 46

def each
  loop do
    result = read_line
    if result
      yield result
    else
      break
    end

    if @closed
      break
    end
  end
end

#read_lineObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pg_data_encoder/decoder.rb', line 21

def read_line
  return nil if @closed
  setup_io if !@io
  row = []
  bytes = @io.read(2)
  #p bytes
  column_count = bytes.unpack("n").first
  if column_count == 65535
    @closed = true
    return nil
  end
  #@io.write([row.size].pack("n"))
  0.upto(column_count - 1).each {|index|
    field = decode_field(@io)
    if field == nil
      row[index] = field 
    elsif @options[:column_types][index]
      row[index] = map_field(field, @options[:column_types][index])
    else
      row[index] = field
    end
  }
  row
end