Class: Baza::SqlQueries::SqliteUpsertDuplicateKey

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SqliteUpsertDuplicateKey

Returns a new instance of SqliteUpsertDuplicateKey.



2
3
4
5
6
7
8
# File 'lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb', line 2

def initialize(args)
  @db = args.fetch(:db)
  @table_name = args.fetch(:table_name)
  @updates = StringCases.stringify_keys(args.fetch(:updates))
  @terms = StringCases.stringify_keys(args.fetch(:terms))
  @return_id = args[:return_id]
end

Instance Method Details

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb', line 10

def execute
  return insert_or_handle_duplicate if @terms.empty?

  @db.transaction do
    @db.query(insert_sql)
    @db.query(update_sql)

    if @return_id
      data = @db.single(@table_name, @terms)
      raise "Couldn't find the updated data" unless data
      return data.fetch(primary_column).to_i
    end
  end
end