class RedshiftPG::Connection

Constants

PG_INCLUDE_KEYS
SQL_TO_PG_KEY_MAP

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/redshift_pg/connection.rb, line 13
def initialize(config)
  @config = config
  @statement_timeout = config['statement_timeout']
end

Public Instance Methods

fetch_all_hash(query) click to toggle source
# 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_connection() click to toggle source
# File lib/redshift_pg/connection.rb, line 27
def pg_connection
  @pg_connection ||= connect!
end
reconnect_on_failure() { || ... } click to toggle source
# 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

Private Instance Methods

connect!() click to toggle source
# File lib/redshift_pg/connection.rb, line 40
def connect!
  ::PG.connect(pg_config).tap do |conn|
    conn.exec("SET statement_timeout to #{@statement_timeout}") if @statement_timeout
  end
end
pg_config() click to toggle source
# File lib/redshift_pg/connection.rb, line 46
def pg_config
  kvs = @config.map do |k, v|
    converted_key = SQL_TO_PG_KEY_MAP.fetch(k, k)
    next unless PG_INCLUDE_KEYS.include?(converted_key)
    [converted_key, v]
  end.compact
  Hash[kvs]
end