Class: CsvToSqlite::SQL::Insert

Inherits:
Object
  • Object
show all
Defined in:
lib/sql/insert.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection:, csv_table:, name:) ⇒ Insert

Returns a new instance of Insert.



7
8
9
10
11
# File 'lib/sql/insert.rb', line 7

def initialize connection:, csv_table:, name:
  @connection = connection
  @csv_table = csv_table
  @name = name
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sql/insert.rb', line 13

def run
  columns = @csv_table.headers.sort
  bar = TTY::ProgressBar.new("Inserting data [:bar] :percent", total: @csv_table.by_row.count)
  @csv_table.by_row.each do |row|
    values = columns.map { |column| row[column] }
    question_marks = ("?," * columns.size).chop!
    @connection.execute("INSERT INTO #{@name} (#{columns.join(',')}) VALUES (#{question_marks})", values)
    bar.advance(1)
  end
  bar.finish
end