Sha256: 8e6184fdafc5486ca438755898e8254bf17b03ad82c98094700cd533aa375674

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module MassInsert
  module Adapters
    class AbstractAdapter < SimpleDelegator
      attr_accessor :values, :options

      def initialize(values, options)
        super(options.fetch(:class_name))
        @values  = values
        @options = options
      end

      def to_sql
        "#{insert_sql} #{values_sql};"
      end

      private

      def columns
        @columns ||= begin
          columns = column_names
          columns.delete(primary_key) unless options[:primary_key]
          columns.map(&:to_sym)
        end
      end

      def quoted_columns
        columns.map do |name|
          connection.quote_column_name(name)
        end
      end

      def insert_sql
        "INSERT INTO #{quoted_table_name} #{columns_sql} VALUES"
      end

      def columns_sql
        "(#{quoted_columns.join(',')})"
      end

      def values_sql
        "(#{array_of_attributes_sql.join('),(')})"
      end

      def array_of_attributes_sql
        values.map do |attrs|
          columns.map do |name|
            value = column_value(attrs, name)
            connection.quote(value)
          end.join(',')
        end
      end

      def column_value(attrs, column)
        attrs.fetch(column.to_sym) do
          attrs.fetch(column.to_s, nil)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mass_insert-0.2.7 lib/mass_insert/adapters/abstract_adapter.rb
mass_insert-0.2.6 lib/mass_insert/adapters/abstract_adapter.rb