From 095cce5605cc3bf460298c954ab6a90455e2a81a Mon Sep 17 00:00:00 2001 From: Corry Haines Date: Sun, 25 Mar 2012 20:49:54 -0700 Subject: [PATCH] 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. --- lib/extras.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 2e3974b6..b1d4d9e4 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):