161
161
from postgres .orm import Model
162
162
from psycopg2 .extensions import cursor as RegularCursor
163
163
from psycopg2 .extras import register_composite , CompositeCaster
164
- from psycopg2 .extras import NamedTupleCursor
164
+ from psycopg2 .extras import NamedTupleCursor , RealDictCursor
165
165
from psycopg2 .pool import ThreadedConnectionPool as ConnectionPool
166
166
167
167
@@ -340,7 +340,7 @@ def run(self, sql, parameters=None):
340
340
txn .execute (sql , parameters )
341
341
342
342
343
- def all (self , sql , parameters = None ):
343
+ def all (self , sql , parameters = None , cursor_factory = None ):
344
344
"""Execute a query and return all results.
345
345
346
346
:param unicode sql: the SQL statement to execute
@@ -358,15 +358,16 @@ def all(self, sql, parameters=None):
358
358
[537, 42]
359
359
360
360
"""
361
- with self .get_transaction () as txn :
361
+ with self .get_transaction (cursor_factory = cursor_factory ) as txn :
362
362
txn .execute (sql , parameters )
363
363
recs = txn .fetchall ()
364
364
if recs and len (recs [0 ]) == 1 :
365
365
recs = [rec [0 ] for rec in recs ]
366
366
return recs
367
367
368
368
369
- def one_or_zero (self , sql , parameters = None , zero = None ):
369
+ def one_or_zero (self , sql , parameters = None , zero = None , \
370
+ cursor_factory = None ):
370
371
"""Execute a query and return a single result or a default value.
371
372
372
373
:param unicode sql: the SQL statement to execute
@@ -394,22 +395,22 @@ def one_or_zero(self, sql, parameters=None, zero=None):
394
395
42
395
396
396
397
"""
397
- out = self ._some (sql , parameters , 0 , 1 )
398
+ out = self ._some (sql , parameters , 0 , 1 , cursor_factory )
398
399
if out is None :
399
400
out = zero
400
401
elif len (out ) == 1 :
401
402
out = out [0 ]
402
403
return out
403
404
404
405
405
- def _some (self , sql , parameters = None , lo = 0 , hi = 1 ):
406
+ def _some (self , sql , parameters , lo , hi , cursor_factory ):
406
407
407
408
# This is undocumented (and largely untested) because I think it's a
408
409
# rare case where this is wanted directly. It was added to make one and
409
410
# one_or_zero DRY when we had one. Help yourself to it now that you've
410
411
# found it. :^)
411
412
412
- with self .get_transaction () as txn :
413
+ with self .get_transaction (cursor_factory = cursor_factory ) as txn :
413
414
txn .execute (sql , parameters )
414
415
415
416
if txn .rowcount < lo :
@@ -575,6 +576,9 @@ def __enter__(self):
575
576
"""
576
577
self .conn = self .pool .getconn ()
577
578
self .conn .autocommit = False
579
+ if 'cursor_factory' in self .kw :
580
+ if self .kw ['cursor_factory' ] is None :
581
+ del self .kw ['cursor_factory' ]
578
582
self .cursor = self .conn .cursor (* self .a , ** self .kw )
579
583
return self .cursor
580
584
0 commit comments