Gang,
I don't know whether this is of interest to anyone else, but I needed it for my stuff and I was hoping it might be suitable for inclusion in adbapi: it defines a new adbapi.ConnectionPool method called 'runQueryMapped' (and corresponding '_runQueryMapped') whose return value is a list of dicts that map column names to values (rather than the list of lists of values that runQuery returns).
Attached are patches for adbapi.py and test_enterprise.py.
Pros:
* shouldn't break any existing code, as it is a completely separate method from runQuery and _runQuery.
* portable, should work with any DBAPI-conforming adaptor, since it builds the dictionaries from the cursor.description, which is part of the DBAPI spec. The reason I needed to do this inside of adbapi is that adbapi doesn't expose cursor.description ... which is fine, as it seems cleaner to keep it inside adbapi anyway.
* unit test included (I've only run the test against SQLite, pyPgSQL, and psycopg, as I don't have the others installed on my machine).
Cons:
* only one I can think of is that it's unnecessary if you are using either reflector, which I'm not, or pyPgSQL's 'PgResultSet' attributes based on the cursor's column names, which I was but am not any more, since that is not portable and not part of the DBAPI spec. While modifying my code to work with other backends (esp. psycopg and sqlite), I came up with this.
If it's decided not to include it, that's okay, I'll just keep patching my Twisted source. It's also fine if you want to name it something else. :)
Cheers, Steve
Lì venerdì, 2004/06/04 alle 02:33, -0400, Stephen Waterbury ha scritto:
Gang,
I don't know whether this is of interest to anyone else, but I needed it for my stuff and I was hoping it might be suitable for inclusion in adbapi: it defines a new adbapi.ConnectionPool method called 'runQueryMapped' (and corresponding '_runQueryMapped') whose return value is a list of dicts that map column names to values (rather than the list of lists of values that runQuery returns).
i think it is a usefull addition.
federico
I'll note that you don't actually have to change adbapi in order to do this. See the "runInteraction" method call.
You would do something like: def mapQuery(curs, *args, **kwargs): curs.execute(*args, **kwargs) result = curs.fetchall() columns = [d[0] for d in curs.description] return [dict(zip(columns, r)) for r in result]
then: dbpool.runInteraction(mapQuery, ...)
James
On Jun 4, 2004, at 4:29 AM, Federico Di Gregorio wrote:
Lì venerdì, 2004/06/04 alle 02:33, -0400, Stephen Waterbury ha scritto:
Gang,
I don't know whether this is of interest to anyone else, but I needed it for my stuff and I was hoping it might be suitable for inclusion in adbapi: it defines a new adbapi.ConnectionPool method called 'runQueryMapped' (and corresponding '_runQueryMapped') whose return value is a list of dicts that map column names to values (rather than the list of lists of values that runQuery returns).
i think it is a usefull addition.
federico
-- Federico Di Gregorio http://people.initd.org/fog Debian GNU/Linux Developer fog@debian.org INIT.D Developer fog@initd.org We are all dust, Saqi, so play the lute We are all wind, Saqi, so bring wine. -- Omar Khayam _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
James Y Knight wrote:
I'll note that you don't actually have to change adbapi in order to do this. See the "runInteraction" method call.
You would do something like: def mapQuery(curs, *args, **kwargs): curs.execute(*args, **kwargs) result = curs.fetchall() columns = [d[0] for d in curs.description] return [dict(zip(columns, r)) for r in result]
then: dbpool.runInteraction(mapQuery, ...)
Interesting -- I wasn't aware of that usage of runInteraction.
Still, that means that anyone who wants to get a mapped query result from adpapi will have to have that construction in their code. I prefer to have it in adbapi, myself, since it is a relatively low-level pattern.
Steve
On Jun 4, 2004, at 12:01 PM, Stephen Waterbury wrote:
Still, that means that anyone who wants to get a mapped query result from adpapi will have to have that construction in their code. I prefer to have it in adbapi, myself, since it is a relatively low-level pattern.
It's not as useful as it could be, however. For example, how can you autogenerate a table from that? The dictionaries don't have an ordering. Here's what I use. (I sent a previous version of this to the list a long time ago, too)
James
James Y Knight wrote:
On Jun 4, 2004, at 12:01 PM, Stephen Waterbury wrote:
Still, that means that anyone who wants to get a mapped query result from adpapi will have to have that construction in their code. I prefer to have it in adbapi, myself, since it is a relatively low-level pattern.
It's not as useful as it could be, however. For example, how can you autogenerate a table from that? The dictionaries don't have an ordering.
1. Ordering is a *totally* orthogonal issue. 2. I don't autogenerate tables. My stuff is not a browser application -- it's a web service. Remember: web stuff is about .01% of Twisted's potential power. ;)
Here's what I use. (I sent a previous version of this to the list a long time ago, too)
Thanks, I'll look at that.
Cheers, Steve