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):
"""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):