Class: Amalgalite::CSVTableImporter
- Inherits:
-
Object
- Object
- Amalgalite::CSVTableImporter
- 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
-
#column_names ⇒ Object
The column names of the import table in definiation order.
-
#initialize(csv_path, database, table_name, options = {}) ⇒ CSVTableImporter
constructor
A new instance of CSVTableImporter.
-
#insert_column_list ⇒ Object
The columns used for the insertion.
-
#insert_sql ⇒ Object
The prepared statement SQL that is used for the import.
- #run ⇒ Object
- #table_list ⇒ Object
-
#validate ⇒ Object
validate that the arguments for initialization are valid and that the #run method will probably execute.
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, = {} ) @csv_path = File.( csv_path ) @database = database @table_name = table_name @table = @database.schema.tables[@table_name] @options = .dup @encoding = .delete("encoding") || "UTF-8" validate end |
Instance Method Details
#column_names ⇒ Object
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_list ⇒ Object
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_sql ⇒ Object
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 |
#run ⇒ Object
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_list ⇒ Object
61 62 63 |
# File 'lib/amalgalite/csv_table_importer.rb', line 61 def table_list @database.schema.tables.keys end |
#validate ⇒ Object
validate that the arguments for initialization are valid and that the #run method will probably execute
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 |