Last active
March 2, 2016 17:12
-
-
Save mikeys/6fcb69bf129a158a621f to your computer and use it in GitHub Desktop.
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
def self.exec_with_cursor(sql, params: [], buffer_size: 10_000) | |
raise ArgumentError, 'Must provide a block' unless block_given? | |
cursor_name = "C#{SecureRandom.hex}" | |
sql = "DECLARE #{cursor_name} CURSOR FOR #{sql}" | |
connection.transaction do | |
connection.exec_params(sql, params) | |
loop do | |
res = connection.exec("FETCH #{buffer_size} IN #{cursor_name}") | |
break if res.ntuples <= 0 | |
yield res | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment