Class: Baza::SqlQueries::NonAtomicUpsert

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ NonAtomicUpsert

Returns a new instance of NonAtomicUpsert.



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

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

Instance Method Details

#executeObject



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

def execute
  row = @db.single(@table_name, @terms)

  if @buffer
    obj = @buffer
  else
    obj = @db
  end

  if row
    obj.update(@table_name, @updates, @terms)
  else
    obj.insert(@table_name, @terms.merge(@updates))
  end
end