Class: CSVPP::SqliteImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/csvpp/sqlite_importer.rb

Overview

Imports data into an Sqlite database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format:, db_path:) ⇒ SqliteImporter

Returns a new instance of SqliteImporter.

Parameters:

  • format (Format)
  • db_path (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/csvpp/sqlite_importer.rb', line 12

def initialize(format:, db_path:)
  @format = format
  @db = Sequel.sqlite(db_path)

  @db.drop_table? :data
  @db.create_table :data do
    Int :line_number

    format.var_names.each do |var|
      type = format.type(var)

      if type.start_with?('array')
        type = 'String'
      else
        type.capitalize
      end

      send(type, var)
    end
  end

  @data = @db[:data]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/csvpp/sqlite_importer.rb', line 8

def data
  @data
end

#dbObject (readonly)

Returns the value of attribute db.



8
9
10
# File 'lib/csvpp/sqlite_importer.rb', line 8

def db
  @db
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/csvpp/sqlite_importer.rb', line 8

def format
  @format
end

Instance Method Details

#import(input) ⇒ Object

Parameters:

  • input (String)

    input string



37
38
39
40
41
42
43
44
45
# File 'lib/csvpp/sqlite_importer.rb', line 37

def import(input)
  Parser.parse_str(
    input: input,
    format: format,
    convert_type: false
  ) do |attr|
    data.insert(attr)
  end
end