Class: Remi::DataSource::Postgres

Inherits:
Remi::DataSubject show all
Includes:
DataStub, Remi::DataSubject::DataSource, Remi::DataSubject::Postgres
Defined in:
lib/remi/cucumber/data_source.rb,
lib/remi/data_subject/postgres.rb

Instance Attribute Summary

Attributes inherited from Remi::DataSubject

#fields

Instance Method Summary collapse

Methods included from Remi::DataSubject::Postgres

#connection

Methods included from Remi::DataSubject::DataSource

#df, #extract

Methods included from DataStub

#empty_stub_df, #stub_df, #stub_row_array, #stub_values

Methods inherited from Remi::DataSubject

#df, #df=, #field_symbolizer

Constructor Details

#initialize(*args, **kargs, &block) ⇒ Postgres

Returns a new instance of Postgres.



21
22
23
24
# File 'lib/remi/data_subject/postgres.rb', line 21

def initialize(*args, **kargs, &block)
  super
  init_postgres(*args, **kargs, &block)
end

Instance Method Details

#extract!Object

Public: Called to extract data from the source.

Returns data in a format that can be used to create a dataframe.



29
30
31
32
# File 'lib/remi/data_subject/postgres.rb', line 29

def extract!
  @logger.info "Executing query #{@query}"
  @extract = connection.exec @query
end

#to_dataframeObject

Public: Converts extracted data to a dataframe. Currently only supports Daru DataFrames.

Returns a Remi::DataFrame



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/remi/data_subject/postgres.rb', line 38

def to_dataframe
  # Performance for larger sets could be improved by using bulk query (via COPY)
  @logger.info "Converting query to a dataframe"

  hash_array = {}
  extract.each do |row|
    row.each do |field, value|
      (hash_array[field_symbolizer.call(field)] ||= []) << value
    end
  end

  # After converting to DF, clear the PG results to save memory.
  extract.clear

  Remi::DataFrame.create(@remi_df_type, hash_array, order: hash_array.keys)
end