Class: RedshiftCsv::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_csv/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, file_path) ⇒ Output

Returns a new instance of Output.



5
6
7
8
# File 'lib/redshift_csv/output.rb', line 5

def initialize(query, file_path)
  @query = query
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



3
4
5
# File 'lib/redshift_csv/output.rb', line 3

def file_path
  @file_path
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/redshift_csv/output.rb', line 3

def query
  @query
end

Instance Method Details

#generate_csvObject



22
23
24
25
26
27
28
29
30
# File 'lib/redshift_csv/output.rb', line 22

def generate_csv
  CSV.open(file_path, "wb") do |csv|
    csv << header

    query_results.each do |result|
      csv << result.values
    end
  end
end

#headerObject



18
19
20
# File 'lib/redshift_csv/output.rb', line 18

def header
  query_results.first.keys
end

#query_resultsObject

def escaped_query

query.gsub("'", "\\\\'")

end



14
15
16
# File 'lib/redshift_csv/output.rb', line 14

def query_results
  @query_results ||= Connection.new.run(query)
end