Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #543 from jhoffner/10-17/csv_importer_updates
Browse files Browse the repository at this point in the history
Updates to CSV Importer
  • Loading branch information
jhoffner authored Feb 16, 2018
2 parents 9a4b6a6 + b734445 commit c622870
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions frameworks/ruby/sql/csv_importer.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
require 'csv'
require 'chronic'
require 'open-uri'

# data importer utility
class CsvImporter
attr_reader :fields, :csv, :table, :limit, :random

def initialize(file, table, fields: {}, limit: 500, random: false)
@csv = CSV.read(file)
# make it possible to load CSV files from either web or file system
@csv = if (file =~ /^https?:\//i) == 0
CSV.parse(open(file).read)
else
CSV.read(file)
end

@table = table
@fields = fields
@limit = limit
Expand All @@ -17,8 +24,22 @@ def create_schema
importer = self
DB.create_table @table do
importer.csv.first.each do |field|
if importer.fields[field]
column field, importer.fields[field]
if schema_type = importer.fields[field]
case schema_type
when :primary_key
primary_key field
when Array
case schema_type.first
when :primary_key
primary_key field, schema_type.last || {}
when :foreign_key
foreign_key field, schema_type[1], schema_type[2] || {}
end
else
column field, schema_type
end
elsif field == 'id'
primary_key :id
else
String field
end
Expand Down

0 comments on commit c622870

Please sign in to comment.