When iterating over rows in a postgres DB using 'use_cursor', the 
underlying 'cursor_fetch_rows' always names the cursor 'sequel_cursor'. 
 This means that use_cursor calls cannot be embedded within each other 
(say, for doing joins in memory).  Is it possible to have an optimal 
:cursor_name argument passed to 'use_cursor' to prevent this? 

I would change the following in adapters/postgres.rb

def use_cursor(opts=OPTS)
        clone(:cursor=>{:rows_per_fetch=>1000, 
:cursor_name=>"sequel_cursor"}.merge(opts))
end

def cursor_fetch_rows(sql)
        server_opts = {:server=>@opts[:server] || :read_only}
        db.transaction(server_opts) do
          begin
            cursor_name = @opts[:cursor][:cursor_name]
            execute_ddl("DECLARE #{cursor_name} NO SCROLL CURSOR WITHOUT 
HOLD FOR #{sql}", server_opts)
            rows_per_fetch = @opts[:cursor][:rows_per_fetch].to_i
            rows_per_fetch = 1000 if rows_per_fetch <= 0

            fetch_sql = "FETCH FORWARD #{rows_per_fetch} FROM 
#{cursor_name}"
            cols = nil
    # Load columns only in the first fetch, so subsequent fetches are faster
            execute(fetch_sql) do |res|
              cols = fetch_rows_set_cols(res)
              yield_hash_rows(res, cols){|h| yield h}
              return if res.ntuples < rows_per_fetch
            end
            loop do
              execute(fetch_sql) do |res|
                yield_hash_rows(res, cols){|h| yield h}
return if res.ntuples < rows_per_fetch
              end
            end
          ensure
            execute_ddl("CLOSE #{cursor_name}", server_opts)
          end
        end
      end

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to