Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv20695/module
Modified Files:
TEST_PyGreSQL_dbapi20.py pgdb.py
Log Message:
Made cursors support the iteration protocol (this is one of the optional DB API
extensions).
To see the diffs for this commit:
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/TEST_PyGreSQL_dbapi20.py.diff?r1=1.13&r2=1.14
Index: TEST_PyGreSQL_dbapi20.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/TEST_PyGreSQL_dbapi20.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- TEST_PyGreSQL_dbapi20.py 23 Nov 2008 12:19:21 -0000 1.13
+++ TEST_PyGreSQL_dbapi20.py 23 Nov 2008 13:11:21 -0000 1.14
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# $Id: TEST_PyGreSQL_dbapi20.py,v 1.13 2008/11/23 12:19:21 cito Exp $
+# $Id: TEST_PyGreSQL_dbapi20.py,v 1.14 2008/11/23 13:11:21 cito Exp $
import dbapi20
import unittest
@@ -54,6 +54,12 @@
curs.execute("SELECT 1 AS a, 2 AS b")
self.assertEqual(curs.fetchone(), {'a': 1, 'b': 2})
+ def test_cursor_iteration(self):
+ con = self._connect()
+ curs = con.cursor()
+ curs.execute("SELECT 1 union select 2 union select 3")
+ self.assertEqual([r[0] for r in curs], range(1, 4))
+
def test_fetch_2_rows(self):
Decimal = pgdb.decimal_type()
values = ['test', 'test', True, 5, 6L, 5.7,
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pgdb.py.diff?r1=1.52&r2=1.53
Index: pgdb.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pgdb.py,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -r1.52 -r1.53
--- pgdb.py 23 Nov 2008 12:19:21 -0000 1.52
+++ pgdb.py 23 Nov 2008 13:11:21 -0000 1.53
@@ -4,7 +4,7 @@
#
# Written by D'Arcy J.M. Cain
#
-# $Id: pgdb.py,v 1.52 2008/11/23 12:19:21 cito Exp $
+# $Id: pgdb.py,v 1.53 2008/11/23 13:11:21 cito Exp $
#
"""pgdb - DB-API 2.0 compliant module for PygreSQL.
@@ -181,6 +181,10 @@
self.arraysize = 1
self.lastrowid = None
+ def __iter__(self):
+ """Return self to make cursors compatible to the iteration protocol."""
+ return self
+
def _quote(self, val):
"""Quote value depending on its type."""
if isinstance(val, datetime):
@@ -331,6 +335,13 @@
return [row_factory([typecast(*args)
for args in zip(coltypes, row)]) for row in result]
+ def next(self):
+ """Return the next row (support for the iteration protocol)."""
+ res = self.fetchone()
+ if res is None:
+ raise StopIteration
+ return res
+
def nextset():
"""Not supported."""
raise NotSupportedError("nextset() is not supported")
_______________________________________________
PyGreSQL mailing list
[email protected]
http://mailman.vex.net/mailman/listinfo/pygresql