Skip to content

Commit cebe999

Browse files
committed
Expose cursor_factory to all and one_or_zero
1 parent e0f7dd0 commit cebe999

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

postgres/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
from postgres.orm import Model
162162
from psycopg2.extensions import cursor as RegularCursor
163163
from psycopg2.extras import register_composite, CompositeCaster
164-
from psycopg2.extras import NamedTupleCursor
164+
from psycopg2.extras import NamedTupleCursor, RealDictCursor
165165
from psycopg2.pool import ThreadedConnectionPool as ConnectionPool
166166

167167

@@ -340,7 +340,7 @@ def run(self, sql, parameters=None):
340340
txn.execute(sql, parameters)
341341

342342

343-
def all(self, sql, parameters=None):
343+
def all(self, sql, parameters=None, cursor_factory=None):
344344
"""Execute a query and return all results.
345345
346346
:param unicode sql: the SQL statement to execute
@@ -358,15 +358,16 @@ def all(self, sql, parameters=None):
358358
[537, 42]
359359
360360
"""
361-
with self.get_transaction() as txn:
361+
with self.get_transaction(cursor_factory=cursor_factory) as txn:
362362
txn.execute(sql, parameters)
363363
recs = txn.fetchall()
364364
if recs and len(recs[0]) == 1:
365365
recs = [rec[0] for rec in recs]
366366
return recs
367367

368368

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):
370371
"""Execute a query and return a single result or a default value.
371372
372373
:param unicode sql: the SQL statement to execute
@@ -394,22 +395,22 @@ def one_or_zero(self, sql, parameters=None, zero=None):
394395
42
395396
396397
"""
397-
out = self._some(sql, parameters, 0, 1)
398+
out = self._some(sql, parameters, 0, 1, cursor_factory)
398399
if out is None:
399400
out = zero
400401
elif len(out) == 1:
401402
out = out[0]
402403
return out
403404

404405

405-
def _some(self, sql, parameters=None, lo=0, hi=1):
406+
def _some(self, sql, parameters, lo, hi, cursor_factory):
406407

407408
# This is undocumented (and largely untested) because I think it's a
408409
# rare case where this is wanted directly. It was added to make one and
409410
# one_or_zero DRY when we had one. Help yourself to it now that you've
410411
# found it. :^)
411412

412-
with self.get_transaction() as txn:
413+
with self.get_transaction(cursor_factory=cursor_factory) as txn:
413414
txn.execute(sql, parameters)
414415

415416
if txn.rowcount < lo:
@@ -575,6 +576,9 @@ def __enter__(self):
575576
"""
576577
self.conn = self.pool.getconn()
577578
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']
578582
self.cursor = self.conn.cursor(*self.a, **self.kw)
579583
return self.cursor
580584

0 commit comments

Comments
 (0)