Class: Baza::SqlQueries::PostgresUpsertDuplicateKey

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ PostgresUpsertDuplicateKey

Returns a new instance of PostgresUpsertDuplicateKey.



2
3
4
5
6
7
8
# File 'lib/baza/sql_queries/postgres_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
# File 'lib/baza/sql_queries/postgres_upsert_duplicate_key.rb', line 10

def execute
  if @db.commands.version.to_f >= 9.5
    @db.query(on_conflict_sql)
  elsif @terms.empty?
    return insert_and_register_conflict
  else
    @db.query(begin_update_exception_sql)
  end

  @db.last_id if @return_id
end