Permalink
Browse files

Make db use objects for PG connections

  • Loading branch information...
1 parent a69d8e1 commit 79714c5a7132df5f83f342a08c3cb22e912027f1 Carlos Diaz-Padron committed Sep 6, 2013
Showing with 17 additions and 3 deletions.
  1. +17 −3 app/models/db.rb
View
@@ -14,6 +14,20 @@ def self.decode(object)
class PostgresDB < AbstractDB
+ def intialize
+ @connection = PG::Connection::new(
+ ENV.fetch("POSTGRES_HOST", "localhost"),
+ ENV.fetch("POSTGRES_PORT", 5432),
+ :dbname => ENV.fetch("POSTGRES_DATABASE", "hurls"),
+ :user => ENV.fetch("POSTGRES_USER", "postgres"),
+ :password => ENV.fetch("POSTGRES_PASSWORD", "postgres")
+ )
+ end
+
+ def connection
+ @connection
+ end
+
def self.connection
@@connection ||= PG::Connection::new(
ENV.fetch("POSTGRES_HOST", "localhost"),
@@ -29,7 +43,7 @@ def self.select_query(scope)
end
def self.find(scope, id)
- connection.exec(select_query(scope), [id], 1) do |result|
+ self.new.connection.exec(select_query(scope), [id], 1) do |result|
decode(result.getvalue(0, 0)) if result.num_tuples >= 1
end
end
@@ -39,11 +53,11 @@ def self.insert_query(scope)
end
def self.save(scope, id, content)
- connection.exec(insert_query(scope), [id, {:value => encode(content), :format => 1}])
+ self.new.connection.exec(insert_query(scope), [id, {:value => encode(content), :format => 1}])
end
def self.count(scope)
- connection.exec("SELECT COUNT(*) FROM %s" % connection.escape_string(scope.to_s)) do |result|
+ self.new.connection.exec("SELECT COUNT(*) FROM %s" % connection.escape_string(scope.to_s)) do |result|
result.getvalue(0, 0)
end
end

0 comments on commit 79714c5

Please sign in to comment.