Class: Vostok::Import
- Inherits:
-
Object
- Object
- Vostok::Import
- Defined in:
- lib/vostok/import.rb
Instance Attribute Summary collapse
-
#pg_connection ⇒ Object
readonly
Returns the value of attribute pg_connection.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(connection) ⇒ Import
constructor
A new instance of Import.
- #start(table, columns, values, options = @options) ⇒ Object
Constructor Details
#initialize(connection) ⇒ Import
Returns a new instance of Import.
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_connection ⇒ Object (readonly)
Returns the value of attribute pg_connection.
5 6 7 |
# File 'lib/vostok/import.rb', line 5 def pg_connection @pg_connection end |
#table ⇒ Object (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) validate_args(columns, values) @table = table begin values.each_slice([: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 |