From: Paul D. <pa...@sn...> - 2003-03-31 16:39:14
|
At 15:07 +0100 3/31/03, Brian Candler wrote: >I was writing some code like this: > > s = db.prepare("select bar from foo where baz=?") > ... >(1) > s.execute(key) do |sth| > result = sth.fetch > end > >Unfortunately, I discovered that it doesn't work like that - the block is >simply ignored. Right. execute takes a block for a database handle, not a statement handle. Example: dbh.execute("SHOW DATABASES") do |sth| sth.fetch do |row| puts row[0] end end > It also doesn't work like this: > >(2) > t = s.execute(key) > result = t.fetch > >After some poking around, it turns out that when you run 'execute' on a >StatementHandle it returns an empty DBI::Row object. You have to do: > >(3) > s.execute(key) > result = s.fetch > >I think it would be nice to support the block format as in (1), which I >think would only need 'yield self if block_given?' in the execute method. > >I think it would also be nice to return self rather than the empty DBI::Row, >so that you can chain execute with other operations: > > result = s.execute(key).fetch > >What does anyone else think? It ought to be a simple change (the attached >patch is untested though, as I need my app to work with the current API >anyway) > >Regards, > >Brian. > >--- dbi.rb.orig Mon Mar 31 15:04:37 2003 >+++ dbi.rb Mon Mar 31 15:05:23 2003 >@@ -747,6 +747,11 @@ > #if @row.nil? > @row = DBI::Row.new(column_names,nil) > #end >+ if block_given? >+ yield self >+ else >+ self >+ end > end > > def finish |