Class: Vostok::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/vostok/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Import

Returns a new instance of Import.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/vostok/import.rb', line 8

def initialize(connection)
  raise ArgumentError, 'Connection can not be null' unless connection
  raise ArgumentError, 'Connection must be a Hash or a PG::Connection' unless (connection.is_a?(::Hash) || connection.is_a?(PG::Connection))
  if connection.is_a? ::Hash
    @pg_connection = PG::Connection.new(connection)
  else
    @pg_connection = connection
    @connection_external = true
  end
  @options = {batch_size: 1000}
end

Instance Attribute Details

#pg_connectionObject (readonly)

Returns the value of attribute pg_connection.



5
6
7
# File 'lib/vostok/import.rb', line 5

def pg_connection
  @pg_connection
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/vostok/import.rb', line 5

def table
  @table
end

Instance Method Details

#start(table, columns, values, options = @options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vostok/import.rb', line 20

def start(table, columns, values, options = @options)
  validate_args(columns, values)
  @table = table
  begin
    values.each_slice(options[:batch_size]) do |slice|
      sql = generate_sql(table, columns, slice)
      @pg_connection.exec(sql)
    end
    values.length
  ensure
    @pg_connection.close unless @connection_external
  end
end