I’ve looked for things like that, but it doesn’t seem like they’ve been
exposed in the Ruby API. If I try to make a DBI prepared statement, it
will just slurp-read the first time I call execute on it. The native
PostgreSQL-extension doesn’t seem to have any classes for cursors or
prepared statements, and neither does the pure Ruby version.
Does anyone know if ActiveRecord might be a solution? Does it have it’s
own implementation of a PostgreSQL connector?
I’ve looked for things like that, but it doesn’t seem like they’ve been
exposed in the Ruby API. If I try to make a DBI prepared statement, it
will just slurp-read the first time I call execute on it. The native
PostgreSQL-extension doesn’t seem to have any classes for cursors or
prepared statements, and neither does the pure Ruby version.
Hm, I wonder why would you need special API for that?
PGconn.exec should be sufficient:
require ‘postgres’
c = PGconn.new
c.exec(“BEGIN”)
c.exec(“DECLARE my_curs CURSOR FOR SELECT * FROM
generate_series(1,20000)”)
while (rs = c.exec(“FETCH FORWARD 1000 FROM my_curs”)) &&
rs.num_tuples > 0
rs.each { |r| print r[0], “,” }
rs.close
end
c.exec(“CLOSE my_curs”)
c.exec(“COMMIT”)
Does anyone know if ActiveRecord might be a solution? Does it have it’s
own implementation of a PostgreSQL connector?
ActiveRecord just uses ruby-postgres or postgres-pr libs
So, it seems to me that AR would not solve your problem. Maybe one could
patch it with the sub-slurp posted in this thread.
I’ve found that AR exacts such a high price in performance that I
write applications using both AR and a native driver. I use AR for all
the lightweight stuff (for the productivity benefits), and code
against the native adapter for everything heavy, like the query that
is the subject of this thread. Even using AR’s capabilities for
directly executing raw SQL code is measurably slower than using the
underlying adapters. You do lose portability across database engines
with this approach, but high performance in any database generally
requires a lot of engine-specific tunings anyway.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.