Sha256: 60974414d0cbc0341fe33a5624a86e1fe1a60b8e942d70c6a366fbaa812ae1f6

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

class Upsert
  class MergeFunction
    # @private
    module Sqlite3
      def self.included(klass)
        klass.extend ClassMethods
      end

      module ClassMethods
        def clear(*)
          # not necessary
        end
      end

      attr_reader :quoted_setter_names
      attr_reader :quoted_update_names
      attr_reader :quoted_selector_names

      def initialize(*)
        super
        @quoted_setter_names = setter_keys.map { |k| connection.quote_ident k }
        @quoted_update_names = setter_keys.select { |k| k !~ CREATED_COL_REGEX }.map { |k| connection.quote_ident k }
        @quoted_selector_names = selector_keys.map { |k| connection.quote_ident k }
      end

      def create!
        # not necessary
      end

      def execute(row)
        bind_setter_values = row.setter.values.map { |v| connection.bind_value v }
        bind_selector_values = row.selector.values.map { |v| connection.bind_value v }
        bind_update_values = row.setter.select{ |k,v| k !~ CREATED_COL_REGEX }.map { |k,v| connection.bind_value v }

        insert_or_ignore_sql = %{INSERT OR IGNORE INTO #{quoted_table_name} (#{quoted_setter_names.join(',')}) VALUES (#{Array.new(bind_setter_values.length, '?').join(',')})}
        connection.execute insert_or_ignore_sql, bind_setter_values

        update_sql = %{UPDATE #{quoted_table_name} SET #{quoted_update_names.map { |qk| "#{qk}=?" }.join(',')} WHERE #{quoted_selector_names.map { |qk| "#{qk}=?" }.join(' AND ')}}
        connection.execute update_sql, (bind_update_values + bind_selector_values)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
upsert-2.9.10-java lib/upsert/merge_function/sqlite3.rb
upsert-2.9.10 lib/upsert/merge_function/sqlite3.rb
upsert-2.9.9-universal-java-11 lib/upsert/merge_function/sqlite3.rb
upsert-2.9.9 lib/upsert/merge_function/sqlite3.rb