[sqlalchemy] Making ResultProxy test as False when there are no rows

2011-10-14 Thread Lloyd Kvam
Would it make sense to add a __nonzero__ method to ResultProxy that
was tied to the rowcount?

def __nonzero__(self):
return bool(self.rowcount)

This would allow code like

if results:
process(results)

I was surprised when this did not work.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Making ResultProxy test as False when there are no rows

2011-10-14 Thread Michael Bayer

On Oct 14, 2011, at 10:38 AM, Lloyd Kvam wrote:

 Would it make sense to add a __nonzero__ method to ResultProxy that
 was tied to the rowcount?
 
 def __nonzero__(self):
return bool(self.rowcount)
 
 This would allow code like
 
 if results:
process(results)
 
 I was surprised when this did not work.

this is because .rowcount is not related to the count of rows in a result set.  
Some DBAPI's may do this, but this is not the intent of .rowcount and is not 
supported on most.  .rowcount is only intended to describe the number of rows 
matched by an UPDATE or DELETE statement.

The only way to know if there are any rows in a result set are to fetch them.   
The cursor itself, based on backend, doesn't even know there's a row until it 
sends a message to the database to get the first row.

Therefore you have to use fetchone() to check for a row.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.