Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added to_pgxlsx and to_pgcsv support #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/xport/formatters/axlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ def to_xlsx(&block)
to_file(formatter, &block)
end

def to_pgxlsx
formatter = Xport::Axlsx::Formatter.new(self)
Copy link
Member

@ebeigarts ebeigarts Jun 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be really great if this could be used with any formatter - axlsx, xlsxtream, csv, etc.

module PostgresCopy
    def write_body(formatter, worksheet)
       ....
    end

    def to_csv
       ....
    end
end

and then we could use

class UserExport < Xport::Export
  include Xport::Axlsx
  include Xport::PostgresCopy # can be used for csv and excel

Also test would be very nice addition.

formatter.add_worksheet do |worksheet|
write_header(formatter, worksheet)
connection = object_class.connection
query = connection.unprepared_statement do
scope = object_class.from("(#{objects.to_sql}) AS #{object_class.table_name}")
builder.columns.each_with_index do |column, i|
name = builder.headers[i]
as = human_attribute_name(name)
scope = scope.select("#{column} AS \"#{as}\"")
end
"COPY (#{scope.to_sql}) TO STDOUT"
end
raw_connection = connection.raw_connection
connection.send(:log, query) do
deco = PG::TextDecoder::CopyRow.new
res = raw_connection.copy_data query, deco do
while row = raw_connection.get_copy_data
formatter.add_row worksheet, row
end
end
res.check
end

write_widths(formatter, worksheet)
end
formatter.to_file
end

class Formatter
attr_reader :export, :workbook

Expand Down
25 changes: 25 additions & 0 deletions lib/xport/formatters/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ def to_csv(&block)
to_file(formatter, &block)
end

def to_pgcsv
StringIO.new.tap do |io|
connection = object_class.connection
query = connection.unprepared_statement do
scope = object_class.from("(#{objects.to_sql}) AS #{object_class.table_name}")
builder.columns.each_with_index do |column, i|
name = builder.headers[i]
as = human_attribute_name(name)
scope = scope.select("#{column} AS \"#{as}\"")
end
"COPY (#{scope.to_sql}) TO STDOUT WITH CSV HEADER"
end
raw_connection = connection.raw_connection
connection.send(:log, query) do
res = raw_connection.copy_data query do
while row = raw_connection.get_copy_data
io << row
end
end
res.check
end
io.rewind
end
end

class Formatter
def initialize(export)
@io = StringIO.new
Expand Down