Sha256: 9685ceb2cbd2dd8cded72d23744d1a79a58c77663576888d603d2ff81dac245d

Contents?: true

Size: 475 Bytes

Versions: 1

Compression:

Stored size: 475 Bytes

Contents

# frozen_string_literal: true

require 'sql_csv/version'

require 'active_record'
require 'csv'

# Create a CSV export from an SQL query.
module SqlCsv
  class Error < StandardError; end

  def self.export(query)
    ActiveRecord::Base.establish_connection
    rows = ActiveRecord::Base.connection.execute(query)
    csv = CSV.new($stdout)
    csv << rows.fields
    rows.each { |row| csv << row.map { |item| item.is_a?(String) ? item : item.to_s } }
    csv.close
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sql_csv-0.2.2 lib/sql_csv.rb