Class: Switches::Backends::Postgres::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/switches/backends/postgres/table.rb

Constant Summary collapse

UPDATE =
"UPDATE %s SET value = $1 WHERE key = $2"
INSERT =
"INSERT INTO %s (key, value) values($2, $1)"
SELECT =
"SELECT value FROM %s WHERE key = $1 LIMIT 1"

Instance Method Summary collapse

Constructor Details

#initialize(name, connection) ⇒ Table

Returns a new instance of Table.



9
10
11
12
# File 'lib/switches/backends/postgres/table.rb', line 9

def initialize(name, connection)
  @name = name
  @connection = connection
end

Instance Method Details

#clearObject



32
33
34
# File 'lib/switches/backends/postgres/table.rb', line 32

def clear
  @connection.execute("TRUNCATE TABLE #{@name}")
end

#find(key) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/switches/backends/postgres/table.rb', line 22

def find(key)
  result = @connection.execute(SELECT % @name, key)

  result.each do |row|
    return row["value"]
  end

  nil
end

#upsert(key, value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/switches/backends/postgres/table.rb', line 14

def upsert(key, value)
  result = @connection.execute(UPDATE % @name, value, key)

  if result.cmd_tuples == 0
    @connection.execute(INSERT % @name, value, key)
  end
end