Class: Amalgalite::CSVTableImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/amalgalite/csv_table_importer.rb

Overview

A class to deal with importing CSV data into a single table in the database.

Instance Method Summary collapse

Constructor Details

#initialize(csv_path, database, table_name, options = {}) ⇒ CSVTableImporter

Returns a new instance of CSVTableImporter.



13
14
15
16
17
18
19
20
21
# File 'lib/amalgalite/csv_table_importer.rb', line 13

def initialize( csv_path, database, table_name, options = {} )
  @csv_path   = File.expand_path( csv_path )
  @database   = database
  @table_name = table_name
  @table      = @database.schema.tables[@table_name]
  @options    = options.dup
  @encoding   = options.delete("encoding") || "UTF-8"
  validate
end

Instance Method Details

#column_namesObject

The column names of the import table in definiation order



36
37
38
# File 'lib/amalgalite/csv_table_importer.rb', line 36

def column_names
  @table.columns_in_order.collect { |c| c.name }
end

#insert_column_listObject

The columns used for the insertion. This is either #column_names or the value out of @options if that value is an Array



44
45
46
47
48
49
50
# File 'lib/amalgalite/csv_table_importer.rb', line 44

def insert_column_list
  column_list = self.column_names
  if Array === @options[:headers]  then
    column_list = @options[:headers]
  end
  return column_list
end

#insert_sqlObject

The prepared statement SQL that is used for the import



55
56
57
58
59
# File 'lib/amalgalite/csv_table_importer.rb', line 55

def insert_sql
  column_sql = insert_column_list.join(",")
  vars       = insert_column_list.collect { |x| "?" }.join(",")
  return "INSERT INTO #{@table_name}(#{column_sql}) VALUES (#{vars})"
end

#runObject



23
24
25
26
27
28
29
30
31
# File 'lib/amalgalite/csv_table_importer.rb', line 23

def run
  @database.transaction do |db|
    db.prepare( insert_sql ) do |stmt|
      ::CSV.foreach( @csv_path, "r:#{@encoding}", **@options ) do |row|
        stmt.execute( row )
      end
    end
  end
end

#table_listObject



61
62
63
# File 'lib/amalgalite/csv_table_importer.rb', line 61

def table_list
  @database.schema.tables.keys
end

#validateObject

validate that the arguments for initialization are valid and that the #run method will probably execute

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/amalgalite/csv_table_importer.rb', line 69

def validate
  raise ArgumentError, "CSV file #{@csv_path} does not exist" unless File.exist?( @csv_path )
  raise ArgumentError, "CSV file #{@csv_path} is not readable" unless File.readable?( @csv_path )
  raise ArgumentError, "The table '#{@table_name} is not found in the database.  The known tables are #{table_list.sort.join(", ")}" unless @table
end