Sha256: b2dffd28d7dca218f74689e570f6fc64ca304b9c6fa49c9b48e22a2e5ea9223e

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

class CukeTableDataConverter

  def initialize(table_name, table)
    @table_name = table_name
    @table = table

    @column_names = extract_column_names(table)
    @datatypes = extract_column_datatypes(table)
  end

  def extract_column_names(table)
    table.column_names.collect { |column| column_name(column) }
  end

  def extract_column_datatypes(table)
    datatypes = Hash.new
    table.column_names.each do |column|
      datatype = column_datatype(column)
      datatypes[column_name(column)] = datatype
    end
    datatypes
  end

  def get_create_table_sql
    query = "CREATE TABLE #{@table_name} ("
    query += @column_names.collect { |column| "#{column} #{@datatypes[column]}" }.join ", "
    query += ");"
    query
  end

  def get_insert_sql
    sql = []
    @table.hashes.each do |hash|
      query = "INSERT INTO #{@table_name} ("
      query += @column_names.join ", "
      query += ") VALUES ("
      query += @table.column_names.collect { |column| as_sql_literal(hash[column]) }.join ", "
      query += ");"
      sql << query
    end
    sql
  end

  def as_sql_literal(value)
    if value == '<null>'
      'NULL'
    elsif (value =~ /[^0-9]/ || value =~ /^0./)
      "'#{value}'"
    else
      value
    end
  end

  def column_name(column_heading)
    /^(\w+)/.match(column_heading)[1]
  end

  def column_datatype(column_heading)
    column_heading =~ /\[([^\]]+)\]/ ? $1 : 'varchar(100)'
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sqldump-0.0.4 features/support/cuke_table_data_converter.rb
sqldump-0.0.3 features/support/cuke_table_data_converter.rb
sqldump-0.0.2 features/support/cuke_table_data_converter.rb
sqldump-0.0.1 features/support/cuke_table_data_converter.rb