Sha256: 3ec6fe954e700d128beda55fdd676495571ce28aa87efabc459c99b83df3754d

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

class Upsert
  # @private
  class Row
    attr_reader :parent
    attr_reader :raw_selector
    attr_reader :selector
    attr_reader :document

    def initialize(parent, raw_selector, raw_document)
      @parent = parent
      @raw_selector = raw_selector
      @selector = raw_selector.inject({}) do |memo, (k, v)|
        memo[parent.quote_ident(k)] = parent.quote_value(v)
        memo
      end
      @document = raw_document.inject({}) do |memo, (k, v)|
        memo[parent.quote_ident(k)] = parent.quote_value(v)
        memo
      end
    end

    def columns
      @columns ||= (selector.keys + document.keys).uniq
    end

    def values_sql_bytesize
      @values_sql_bytesize ||= pairs.inject(0) { |sum, (_, v)| sum + v.to_s.bytesize } + columns.length - 1
    end

    def values_sql
      pairs.map { |_, v| v }.join(',')
    end

    def columns_sql
      pairs.map { |k, _| k }.join(',')
    end

    def where_sql
      selector.map { |k, v| [k, v].join('=') }.join(',')
    end

    def set_sql
      pairs.map { |k, v| [k, v].join('=') }.join(',')
    end

    def pairs
      @pairs ||= columns.map do |k|
        v = if document.has_key?(k)
          # prefer the document so that you can change rows
          document[k]
        else
          selector[k]
        end
        [ k, v ]
      end
    end

    def to_hash
      @to_hash ||= pairs.inject({}) do |memo, (k, v)|
        memo[k] = v
        memo
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
upsert-0.3.0 lib/upsert/row.rb
upsert-0.2.2 lib/upsert/row.rb