Class: Baza::Driver::Pg::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/driver/pg/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Commands

Returns a new instance of Commands.



2
3
4
# File 'lib/baza/driver/pg/commands.rb', line 2

def initialize(args)
  @db = args.fetch(:db)
end

Instance Method Details

#last_idObject



28
29
30
# File 'lib/baza/driver/pg/commands.rb', line 28

def last_id
  @db.query("SELECT LASTVAL() AS id").fetch.fetch(:id).to_i
end

#upsert(table_name, updates, terms, args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/baza/driver/pg/commands.rb', line 17

def upsert(table_name, updates, terms, args = {})
  @last_insert_table_name = table_name.to_s

  Baza::SqlQueries::NonAtomicUpsert.new({
    db: @db,
    table_name: table_name,
    terms: terms,
    updates: updates
  }.merge(args)).execute
end

#upsert_duplicate_key(table_name, updates, terms, args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/baza/driver/pg/commands.rb', line 6

def upsert_duplicate_key(table_name, updates, terms, args = {})
  @last_insert_table_name = table_name.to_s

  Baza::SqlQueries::PostgresUpsertDuplicateKey.new({
    db: @db,
    table_name: table_name,
    updates: updates,
    terms: terms
  }.merge(args)).execute
end

#versionObject



32
33
34
# File 'lib/baza/driver/pg/commands.rb', line 32

def version
  @version ||= @db.query("SELECT VERSION() AS version").fetch.fetch(:version).match(/\APostgreSQL ([\d\.]+)/)[1]
end