New issue 59 by dbres...@gmail.com: CursorWrapper in
zxjdbc/postgresql/base.py needs __iter__ method to support raw queries
http://code.google.com/p/django-jython/issues/detail?id=59
What steps will reproduce the problem?
1. In settings.py, use the following database engine:
'ENGINE': 'doj.backends.zxjdbc.postgresql'
2. Try to iterate over a table in the database (here, "snapshot") using a
raw query:
for s in Snapshot.objects.raw('SELECT * FROM snapshot'):
print s.name
What is the expected output? What do you see instead?
Instead of seeing the 'name' column for each of the rows, an exception is
thrown:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\soft\jython2.5.2\Lib\site-packages\django\db\models\query.py",
line 1406, in __iter__
query = iter(self.query)
File "C:\soft\jython2.5.2\Lib\site-packages\django\db\models\sql\query.py",
line 80, in __iter__
return iter(result)
File "C:\soft\jython2.5.2\Lib\site-packages\django\db\backends\util.py",
line 42, in __iter__
return iter(self.cursor)
TypeError: 'CursorWrapper' object is not iterable
What version of the product are you using? On what operating system?
django-jython-1.2.0b1 on Windows XP
Please provide any additional information below.
Here's the __iter__ method that we added to CursorWrapper in
doj/backends/zxjdbc/postgresql/base.py :
def __iter__(self):
i = self.next()
while i:
yield i
i = self.next()
With this method, iterating over the RawQuerySet that's returned from a
RawQuery works as expected.
Comment #1 on issue 59 by juneau001: CursorWrapper in
zxjdbc/postgresql/base.py needs __iter__ method to support raw queries
http://code.google.com/p/django-jython/issues/detail?id=59
Thanks for the feedback and the code to repair this issue. I will test and
roll this fix in for the next release.
Will this be added any time soon? Seems like an easy fix that's low risk
due to it only effecting raw queries, which without this fix are broken.
Here's a patch that should work for all backends.
diff -r 87f0d585b19b doj/backends/zxjdbc/common.py
--- a/doj/backends/zxjdbc/common.py Tue May 31 07:45:46 2011 -0500
+++ b/doj/backends/zxjdbc/common.py Mon Oct 24 10:08:12 2011 -0700
@@ -111,6 +111,10 @@
def __getattr__(self, attr):
return getattr(self.cursor, attr)
+ def __iter__(self):
+ return iter(self.next, None)
+
+
# Must be called by zxJDBC backends after instantiating a connection
def set_default_isolation_level(connection, innodb_binlog = False):
jdbc_conn = connection.__connection__
Comment #4 on issue 59 by juneau001: CursorWrapper in
zxjdbc/postgresql/base.py needs __iter__ method to support raw queries
http://code.google.com/p/django-jython/issues/detail?id=59
Great! Thanks for the patch. Will start testing today.