# Re: Is there a way to avoid having the library slurp-read th

**URL:** <https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352>\
**Category:** Ruby\
**Created:** [August 4, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352 "2006-08-04T12:53:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kivle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kivle](https://www.ruby-forum.com/u/kivle)\
**Post date:** [August 4, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352/1 "2006-08-04T12:53:52Z")

</div>

Hi depesz,

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?

Helge E.

---

<div class="post-metadata">

**Author:** ![kivle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kivle](https://www.ruby-forum.com/u/kivle)\
**Post date:** [August 4, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352/2 "2006-08-04T13:06:09Z")

</div>

2006/8/4, Helge E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![kivle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kivle](https://www.ruby-forum.com/u/kivle)\
**Post date:** [August 5, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352/3 "2006-08-05T15:03:04Z")

</div>

> Does anyone know if ActiveRecord might be a solution? Does it have it’s  
> own implementation of a PostgreSQL connector?

I’m on thin ice here, but a quick browsing of the postgresql\_adabter.rb  
in  
AR reads:

if @async  
@connection.async\_exec(sql)  
else  
@connection.exce(sql)  
end

(lines 162…166)

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.

---

<div class="post-metadata">

**Author:** ![kivle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kivle](https://www.ruby-forum.com/u/kivle)\
**Post date:** [August 5, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352/4 "2006-08-05T16:33:10Z")

</div>

On Sat, 5 Aug 2006, Jon Egil S. wrote:

> else  
> @connection.exce(sql)  
> end
> 
> (lines 162…166)
> 
> 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.

indeed - i sent a patch in some time ago to address this - but no joy.

-a

---

<div class="post-metadata">

**Author:** ![kivle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kivle](https://www.ruby-forum.com/u/kivle)\
**Post date:** [August 5, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/re-is-there-a-way-to-avoid-having-the-library-slurp-read-th/101352/5 "2006-08-05T15:15:05Z")

</div>

On 8/5/06, Jon Egil S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> 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.
