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.
This commit is contained in:
Corry Haines 2012-03-25 20:49:54 -07:00 committed by Daniele Varrazzo
parent 27421f1e41
commit 095cce5605

View File

@ -103,11 +103,10 @@ class DictCursorBase(_cursor):
class DictConnection(_connection): class DictConnection(_connection):
"""A connection that uses `DictCursor` automatically.""" """A connection that uses `DictCursor` automatically."""
def cursor(self, name=None): def cursor(self, *args, **kwargs):
if name is None: if 'cursor_factory' not in kwargs:
return _connection.cursor(self, cursor_factory=DictCursor) kwargs['cursor_factory'] = DictCursor
else: return _connection.cursor(self, *args, **kwargs)
return _connection.cursor(self, name, cursor_factory=DictCursor)
class DictCursor(DictCursorBase): class DictCursor(DictCursorBase):
"""A cursor that keeps a list of column name -> index mappings.""" """A cursor that keeps a list of column name -> index mappings."""
@ -196,11 +195,10 @@ class DictRow(list):
class RealDictConnection(_connection): class RealDictConnection(_connection):
"""A connection that uses `RealDictCursor` automatically.""" """A connection that uses `RealDictCursor` automatically."""
def cursor(self, name=None): def cursor(self, *args, **kwargs):
if name is None: if 'cursor_factory' not in kwargs:
return _connection.cursor(self, cursor_factory=RealDictCursor) kwargs['cursor_factory'] = RealDictCursor
else: return _connection.cursor(self, *args, **kwargs)
return _connection.cursor(self, name, cursor_factory=RealDictCursor)
class RealDictCursor(DictCursorBase): class RealDictCursor(DictCursorBase):
"""A cursor that uses a real dict as the base type for rows. """A cursor that uses a real dict as the base type for rows.
@ -254,6 +252,7 @@ class RealDictRow(dict):
class NamedTupleConnection(_connection): class NamedTupleConnection(_connection):
"""A connection that uses `NamedTupleCursor` automatically.""" """A connection that uses `NamedTupleCursor` automatically."""
def cursor(self, *args, **kwargs): def cursor(self, *args, **kwargs):
if 'cursor_factory' not in kwargs:
kwargs['cursor_factory'] = NamedTupleCursor kwargs['cursor_factory'] = NamedTupleCursor
return _connection.cursor(self, *args, **kwargs) return _connection.cursor(self, *args, **kwargs)