Sha256: 9e20df3f6771d0b08873d5e4577001e8d8e20ef1a2ad252eaec84b8bedc05cc5

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

require "metacrunch/db"

module Metacrunch
  class Db::Writer

    def initialize(database_connection_or_url, dataset_proc, options = {})
      @use_upsert          = options.delete(:use_upsert)          || false
      @id_key              = options.delete(:id_key)              || :id
      @isolation_level     = options.delete(:isolation_level)     || :repeatable
      @transaction_retries = options.delete(:transaction_retries) || 5

      @db = if database_connection_or_url.is_a?(String)
        Sequel.connect(database_connection_or_url, options)
      else
        database_connection_or_url
      end

      @dataset = dataset_proc.call(@db)
    end

    def write(data)
      if data.is_a?(Array)
        @db.transaction(isolation: @isolation_level, num_retries: @transaction_retries) do
          data.each{|d| insert_or_upsert(d) }
        end
      else
        insert_or_upsert(data)
      end
    end

    def close
      @db.disconnect
    end

  private

    def insert_or_upsert(data)
      @use_upsert ? upsert(data) : insert(data)
    end

    def insert(data)
      @dataset.insert(data) if data
    end

    def upsert(data)
      if data
        rec = @dataset.where(@id_key => data[@id_key])
        if 1 != rec.update(data)
          insert(data)
        end
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
metacrunch-3.1.4 lib/metacrunch/db/writer.rb
metacrunch-3.1.3 lib/metacrunch/db/writer.rb
metacrunch-3.1.2 lib/metacrunch/db/writer.rb
metacrunch-3.1.1 lib/metacrunch/db/writer.rb
metacrunch-3.1.0 lib/metacrunch/db/writer.rb