Class: RedshiftPG::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_pg/connection.rb

Constant Summary collapse

SQL_TO_PG_KEY_MAP =
{
  'username' => 'user',
  'database' => 'dbname'
}
PG_INCLUDE_KEYS =
%w(host dbname port user password)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
# File 'lib/redshift_pg/connection.rb', line 13

def initialize(config)
  @config = config
  @statement_timeout = config['statement_timeout']
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/redshift_pg/connection.rb', line 4

def config
  @config
end

Instance Method Details

#fetch_all_hash(query) ⇒ Object



31
32
33
34
35
36
# File 'lib/redshift_pg/connection.rb', line 31

def fetch_all_hash(query)
  # execute a query and return the entire result as an array of hashes keyed by column names
  reconnect_on_failure do
    pg_connection.exec(query).to_a
  end
end

#pg_connectionObject



27
28
29
# File 'lib/redshift_pg/connection.rb', line 27

def pg_connection
  @pg_connection ||= connect!
end

#reconnect_on_failure(&block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/redshift_pg/connection.rb', line 18

def reconnect_on_failure(&block)
  begin
    return yield
  rescue PG::UnableToSend, PG::ConnectionBad
    pg_connection.reset
    return yield   # retry once
  end
end