### Home Classes Methods

### In Files

- lib/amalgalite/csv_table_importer.rb

### Parent

Object

### Methods

- ::new
- #column_names
- #insert_column_list
- #insert_sql
- #run
- #table_list
- #validate

### Files

- HISTORY.rdoc
- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Amalgalite
- Amalgalite::Aggregate
- Amalgalite::Blob
- Amalgalite::Blob::Error
- Amalgalite::Boolean
- Amalgalite::BusyHandler
- Amalgalite::BusyTimeout
- Amalgalite::CSVTableImporter
- Amalgalite::Column
- Amalgalite::Database
- Amalgalite::Database::AggregateError
- Amalgalite::Database::BusyHandlerError
- Amalgalite::Database::FunctionError
- Amalgalite::Database::InvalidModeError
- Amalgalite::Database::ProgressHandlerError
- Amalgalite::Database::TransactionBehavior
- Amalgalite::Error
- Amalgalite::Function
- Amalgalite::Index
- Amalgalite::MemoryDatabase
- Amalgalite::Packer
- Amalgalite::Paths
- Amalgalite::ProfileSampler
- Amalgalite::ProfileTap
- Amalgalite::ProgressHandler
- Amalgalite::Requires
- Amalgalite::SQLite3
- Amalgalite::SQLite3::Constants
- Amalgalite::SQLite3::Constants::Config
- Amalgalite::SQLite3::Constants::DBStatus
- Amalgalite::SQLite3::Constants::DataType
- Amalgalite::SQLite3::Constants::Helpers
- Amalgalite::SQLite3::Constants::Open
- Amalgalite::SQLite3::Constants::ResultCode
- Amalgalite::SQLite3::Constants::StatementStatus
- Amalgalite::SQLite3::Constants::Status
- Amalgalite::SQLite3::Database
- Amalgalite::SQLite3::Database::DBStatus
- Amalgalite::SQLite3::Database::Function
- Amalgalite::SQLite3::Database::Stat
- Amalgalite::SQLite3::Stat
- Amalgalite::SQLite3::Status
- Amalgalite::SQLite3::Version
- Amalgalite::Schema
- Amalgalite::Statement
- Amalgalite::Table
- Amalgalite::Taps
- Amalgalite::Taps::IO
- Amalgalite::Taps::Stderr
- Amalgalite::Taps::Stdout
- Amalgalite::Taps::StringIO
- Amalgalite::TraceTap
- Amalgalite::TypeMap
- Amalgalite::TypeMaps
- Amalgalite::TypeMaps::DefaultMap
- Amalgalite::TypeMaps::StorageMap
- Amalgalite::TypeMaps::TextMap
- Amalgalite::Version
- Amalgalite::View
- Kernel
- Object

No matching classes.

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.