Class: CsvToSqlite::SQL::CreateTable

Inherits:
Object
  • Object
show all
Defined in:
lib/sql/create_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(name:, csv_table:, connection:) ⇒ CreateTable

Returns a new instance of CreateTable.



8
9
10
11
12
# File 'lib/sql/create_table.rb', line 8

def initialize name:, csv_table:, connection:
  @connection = connection
  @name = name
  @csv_table = csv_table
end

Instance Method Details

#columnsObject



28
29
30
31
32
33
34
# File 'lib/sql/create_table.rb', line 28

def columns
  @column_generator = CsvToSqlite::SQL::Column.new(csv_table: @csv_table)
  columns_sql = @csv_table.headers.map do |column|
    @column_generator.sql_for column
  end
  columns_sql.join.chop
end

#create_statementObject



24
25
26
# File 'lib/sql/create_table.rb', line 24

def create_statement
  "CREATE TABLE IF NOT EXISTS #{@name}"
end

#runObject



14
15
16
17
18
# File 'lib/sql/create_table.rb', line 14

def run
  puts "========== Creating table #{@name} =========="
  @connection.execute sql
  puts "=================================================="
end

#sqlObject



20
21
22
# File 'lib/sql/create_table.rb', line 20

def sql
  "#{create_statement} ( #{columns} );"
end