Class: PgDataEncoder::EncodeForCopy

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EncodeForCopy

Returns a new instance of EncodeForCopy.



6
7
8
9
10
11
12
# File 'lib/pg_data_encoder/encode_for_copy.rb', line 6

def initialize(options = {})
  @options = options
  @closed = false
  options[:column_types] ||= {}
  @io = nil
  @buffer = TempBuffer.new
end

Instance Method Details

#add(row) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pg_data_encoder/encode_for_copy.rb', line 14

def add(row)
  setup_io if !@io
  @io.write([row.size].pack("n"))
  row.each_with_index {|col, index|
    encode_field(@buffer, col, index)
    if @buffer.size > 0
      #@buffer.rewind
      @io.write(@buffer.read)
      @buffer.reopen
    end
  }
end

#closeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/pg_data_encoder/encode_for_copy.rb', line 27

def close
  @closed = true
  if @buffer.size > 0
    #@buffer.rewind
    @io.write(@buffer.read)
    @buffer.reopen
  end
  @io.write([-1].pack("n")) rescue raise Exception.new("No rows have been added to the encoder!")
  @io.rewind
end

#get_ioObject



38
39
40
41
42
43
# File 'lib/pg_data_encoder/encode_for_copy.rb', line 38

def get_io
  if !@closed
    close
  end
  @io
end

#removeObject



45
46
47
48
49
50
# File 'lib/pg_data_encoder/encode_for_copy.rb', line 45

def remove
  if @io.kind_of?(Tempfile)
    @io.close
    @io.unlink
  end
end