class Amalgalite::CSVTableImporter

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

Public Class Methods

new( csv_path, database, table_name, options = {} ) click to toggle source
# 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
  validate
end

Public Instance Methods

column_names() click to toggle source

The column names of the import table in definiation order

# File lib/amalgalite/csv_table_importer.rb, line 35
def column_names
  @table.columns_in_order.collect { |c| c.name }
end
insert_column_list() click to toggle source

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

# File lib/amalgalite/csv_table_importer.rb, line 43
def insert_column_list
  column_list = self.column_names
  if Array === @options[:headers]  then
    column_list = @options[:headers]
  end
  return column_list
end
insert_sql() click to toggle source

The prepared statement SQL that is used for the import

# File lib/amalgalite/csv_table_importer.rb, line 54
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
run() click to toggle source
# File lib/amalgalite/csv_table_importer.rb, line 22
def run
  @database.transaction do |db|
    db.prepare( insert_sql ) do |stmt|
      ::CSV.foreach( @csv_path, @options ) do |row|
        stmt.execute( row )
      end
    end
  end
end
table_list() click to toggle source
# File lib/amalgalite/csv_table_importer.rb, line 60
def table_list
  @database.schema.tables.keys
end
validate() click to toggle source

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

# File lib/amalgalite/csv_table_importer.rb, line 68
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