Sha256: 3d3d69cbcaae1d41e5bf400113fd435fe784d1187cdcb2e9c5ef81d1763d539f

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module Sqldump

  class InsertFormatter

    def initialize(sth, io, options)
      @sth = sth
      @io = io
      @options = options

      setup_column_type_mapping()
    end

    def setup_column_type_mapping
      @column_type_by_name = Hash[@sth.column_names.zip(@sth.column_types)]
    end

    def column_type(column_name)
      @column_type_by_name[column_name]
    end

    def output_list(list)
      @io.print "\n#{indent}" if pretty
      join_string = pretty ? ",\n#{indent}" : ", "
      @io.print list.join(join_string)
      @io.print "\n" if pretty
    end

    def output_column_names
      output_list(@sth.column_names)
    end

    def output_values(row)
      quoted_list = []
      row.each_with_name do |value, column_name|
        quoted_list.push quote(value, column_name)
      end
      output_list(quoted_list)
      #@io.print quoted_list.join(", ")
    end

    def output
      @sth.fetch do |row|
        @io.print("INSERT INTO #{@options.table} (")
        output_column_names()
        @io.print ")"
        @io.print pretty ? "\n" : " "
        @io.print "VALUES ("
        output_values(row)
        @io.print ");\n"
      end
    end

    def quote(value, column_name)
      if value.nil?
        "NULL"
      else
        type = column_type(column_name)
        if type == DBI::Type::Integer or type == DBI::Type::Float or type == DBI::Type::Decimal
          value
        else
          value = value.to_s.gsub(/'/, "''")
          "'#{value}'"
        end
      end
    end

    def indent
      " " * 4
    end

    def pretty
      @options.pretty
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sqldump-0.0.3 lib/sqldump/insert_formatter.rb