summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorCorry Haines <tabletcorry@gmail.com>2012-03-25 20:49:54 -0700
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-04-11 17:36:04 +0100
commit095cce5605cc3bf460298c954ab6a90455e2a81a (patch)
treef2dc04270e8d756cec46700c211bb81d27c92a0d /lib
parent27421f1e41dd453c986bf3e746ed8f64abc440cc (diff)
downloadpsycopg2-095cce5605cc3bf460298c954ab6a90455e2a81a.tar.gz
Allow user to override connection factory cursors
Prior to this change, using a extras.connection_factory would not allow any other cursor to be used on that connection. It was set in stone. This change allows all cursor options to pass through and override the connection factory behaviors. This allows a connection_factory to be dropped into existing code with no disruption. This change also standardizes the extras.connection_factories to have the same behavior and all pass through *args and **kwargs.
Diffstat (limited to 'lib')
-rw-r--r--lib/extras.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 2e3974b..b1d4d9e 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -103,11 +103,10 @@ class DictCursorBase(_cursor):
class DictConnection(_connection):
"""A connection that uses `DictCursor` automatically."""
- def cursor(self, name=None):
- if name is None:
- return _connection.cursor(self, cursor_factory=DictCursor)
- else:
- return _connection.cursor(self, name, cursor_factory=DictCursor)
+ def cursor(self, *args, **kwargs):
+ if 'cursor_factory' not in kwargs:
+ kwargs['cursor_factory'] = DictCursor
+ return _connection.cursor(self, *args, **kwargs)
class DictCursor(DictCursorBase):
"""A cursor that keeps a list of column name -> index mappings."""
@@ -196,11 +195,10 @@ class DictRow(list):
class RealDictConnection(_connection):
"""A connection that uses `RealDictCursor` automatically."""
- def cursor(self, name=None):
- if name is None:
- return _connection.cursor(self, cursor_factory=RealDictCursor)
- else:
- return _connection.cursor(self, name, cursor_factory=RealDictCursor)
+ def cursor(self, *args, **kwargs):
+ if 'cursor_factory' not in kwargs:
+ kwargs['cursor_factory'] = RealDictCursor
+ return _connection.cursor(self, *args, **kwargs)
class RealDictCursor(DictCursorBase):
"""A cursor that uses a real dict as the base type for rows.
@@ -254,7 +252,8 @@ class RealDictRow(dict):
class NamedTupleConnection(_connection):
"""A connection that uses `NamedTupleCursor` automatically."""
def cursor(self, *args, **kwargs):
- kwargs['cursor_factory'] = NamedTupleCursor
+ if 'cursor_factory' not in kwargs:
+ kwargs['cursor_factory'] = NamedTupleCursor
return _connection.cursor(self, *args, **kwargs)
class NamedTupleCursor(_cursor):