Skip to content

Instantly share code, notes, and snippets.

@mikeys
Last active March 2, 2016 17:12
Show Gist options
  • Save mikeys/6fcb69bf129a158a621f to your computer and use it in GitHub Desktop.
Save mikeys/6fcb69bf129a158a621f to your computer and use it in GitHub Desktop.
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