Sha256: e60fc06aa1c035e01c7de115d31988881d55c2cf5fd677bc3d4fe844358e6622

Contents?: true

Size: 507 Bytes

Versions: 1

Compression:

Stored size: 507 Bytes

Contents

require 'activerecord-import'

class Atomically::QueryService
  def initialize(klass)
    @klass = klass
  end

  def create_or_plus(columns, data, update_columns)
    @klass.import(columns, data, on_duplicate_key_update: on_duplicate_key_plus_sql(update_columns))
  end

  private

  def on_duplicate_key_plus_sql( columns)
    columns.lazy
           .map(&@klass.connection.method(:quote_column_name))
           .map{|s| "#{s} = #{s} + VALUES(#{s})" }
           .force
           .join(', ')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
atomically-1.0.0 lib/atomically/query_service.rb