module Upsert::MergeFunction::Sqlite3

@private

Attributes

quoted_selector_names[R]
quoted_setter_names[R]
quoted_update_names[R]

Public Class Methods

included(klass) click to toggle source
# File lib/upsert/merge_function/sqlite3.rb, line 5
def self.included(klass)
  klass.extend ClassMethods
end
new(*) click to toggle source
Calls superclass method
# File lib/upsert/merge_function/sqlite3.rb, line 19
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

Public Instance Methods

create!() click to toggle source
# File lib/upsert/merge_function/sqlite3.rb, line 26
def create!
  # not necessary
end
execute(row) click to toggle source
# File lib/upsert/merge_function/sqlite3.rb, line 30
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