Last active
June 14, 2017 18:37
-
-
Save silasjmatson/a783d9c457dd0fc3275dde199a7172ee to your computer and use it in GitHub Desktop.
Ruby Module to stream a SQL query to standard out, and yield each row to a block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DatabaseQueryStreaming | |
def stream_query_rows(sql_query, options="WITH CSV HEADER") | |
conn = ActiveRecord::Base.connection.raw_connection | |
conn.copy_data "COPY (#{sql_query}) TO STDOUT #{options};" do | |
while row = conn.get_copy_data | |
yield row | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment