Sha256: f954cce003dbb0d66393efbc63a53589f062921092f22328b9e1c0de47aa9511

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

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

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

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

      private

      def columns
        @columns ||= begin
          columns = column_names
          columns.delete(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 = attrs[name.to_sym] || attrs[name.to_s]
            connection.quote(value, columns_hash[name.to_s])
          end.join(',')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mass_insert-0.2.2 lib/mass_insert/adapters/abstract_adapter.rb
mass_insert-0.2.1 lib/mass_insert/adapters/abstract_adapter.rb
mass_insert-0.2.0 lib/mass_insert/adapters/abstract_adapter.rb