[PyGreSQL] DictCursor support for 3.8.1

D'Arcy J.M. Cain darcy at PyGreSQL.org
Mon Sep 18 12:02:22 EDT 2006


On Mon, 18 Sep 2006 11:53:00 +0200
Simon Pamies <s.pamies at banality.de> wrote:
> I just downloaded PyGreSQL 3.8.1 and noticed that there is no dict
> cursor support included. Also the close method on the pgdbCursor class
> is missing.
> 
> Patch here:
> http://files.banality.de/public/pgdb.py.diff

Looks fine.  However, your patch as delivered mixes spaces and tabs.  I
have attached your patch with this fixed and otherwise unchanged.  In
general patches should be attached rather than referenced so that they
may be archived in this list.

I also see that you are overwriting the close method with a do-nothing
method.  Was this a mistake?

-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org
-------------- next part --------------
--- pgdb.py	2006-06-02 15:43:17.000000000 +0200
+++ /opt/ZEnv2/Python-2.4.3/lib/python2.4/site-packages/pgdb.py	2006-09-18 09:47:29.000000000 +0200
@@ -27,6 +27,17 @@
 
 	connection.cursor() # open a cursor
 
+	# you can pass in a custom row factory
+	# e.g. a dict_factory
+
+	def dict_factory(caller, row):
+		d = {}
+		for idx, col in enumerate(caller.description):
+			d[col[0]] = row[idx]
+		return d
+
+	cursor.row_factory = lambda cur, row: dict_factory(cur, row)
+
 	cursor.execute(query[, params])
 	# Execute a query, binding params (a dictionary) if they are
 	# passed. The binding syntax is the same as the % operator
@@ -148,6 +159,9 @@
 	def __init__(self, src, cache):
 		self.__cache = cache
 		self.__source = src
+
+		# default row factory returns result unchanged
+		self.row_factory = lambda cur, row: row
 		self.description = None
 		self.rowcount = -1
 		self.arraysize = 1
@@ -239,12 +253,14 @@
 		for r in res:
 			row = []
 			for i in range(len(r)):
-				row.append(self.__cache.typecast(
+						val = self.__cache.typecast(
 						self.description[i][1],
 						r[i]
 					)
-				)
-			result.append(row)
+
+				row.append(val)
+
+			result.append( self.row_factory(self, row) )
 		return result
 
 	def nextset(self):
@@ -256,6 +272,8 @@
 	def setoutputsize(self, size, col = 0):
 		pass
 
+	def close(self):
+		pass
 
 class _quoteitem(dict):
 	def __getitem__(self, key):


More information about the PyGreSQL mailing list