@@ -209,7 +209,8 @@ class Postgres(object):
209
209
210
210
:param unicode url: A ``postgres://`` URL or a `PostgreSQL connection string <http://www.postgresql.org/docs/current/static/libpq-connect.html>`_
211
211
:param int minconn: The minimum size of the connection pool
212
- :param int maxconn: The minimum size of the connection pool
212
+ :param int maxconn: The maximum size of the connection pool
213
+ :param cursor_factory: Defaults to :py:class:`~psycopg2.extras.RealDictCursor`
213
214
:param strict_one: The default :py:attr:`strict` parameter for :py:meth:`~postgres.Postgres.one`
214
215
:type strict_one: :py:class:`bool`
215
216
@@ -223,6 +224,11 @@ class Postgres(object):
223
224
:py:class:`~postgres.Postgres` instance is that it runs everything through
224
225
its connection pool.
225
226
227
+ Check the :py:mod:`psycopg2` `docs
228
+ <http://initd.org/psycopg/docs/extras.html#connection-and-cursor-subclasses>`_
229
+ for additional :py:attr:`cursor_factories`, such as
230
+ :py:class:`NamedTupleCursor`.
231
+
226
232
The names in our simple API, :py:meth:`~postgres.Postgres.run`,
227
233
:py:meth:`~postgres.Postgres.one`, and :py:meth:`~postgres.Postgres.rows`,
228
234
were chosen to be short and memorable, and to not conflict with the DB-API
@@ -242,10 +248,13 @@ class Postgres(object):
242
248
243
249
"""
244
250
245
- def __init__ (self , url , minconn = 1 , maxconn = 10 , strict_one = None ):
251
+ def __init__ (self , url , minconn = 1 , maxconn = 10 , \
252
+ cursor_factory = RealDictCursor , strict_one = None ):
246
253
if url .startswith ("postgres://" ):
247
254
dsn = url_to_dsn (url )
248
255
256
+ Connection .cursor_factory = cursor_factory
257
+
249
258
self .pool = ConnectionPool ( minconn = minconn
250
259
, maxconn = maxconn
251
260
, dsn = dsn
@@ -400,18 +409,20 @@ class Connection(psycopg2.extensions.connection):
400
409
401
410
- We set :py:attr:`autocommit` to :py:const:`True`.
402
411
- We set the client encoding to ``UTF-8``.
403
- - We use :py:class:`psycopg2.extras.RealDictCursor `.
412
+ - We use :py:attr:`self.cursor_factory `.
404
413
405
414
"""
406
415
416
+ cursor_factory = None # set this before using this object
417
+
407
418
def __init__ (self , * a , ** kw ):
408
419
psycopg2 .extensions .connection .__init__ (self , * a , ** kw )
409
420
self .set_client_encoding ('UTF-8' )
410
421
self .autocommit = True
411
422
412
423
def cursor (self , * a , ** kw ):
413
424
if 'cursor_factory' not in kw :
414
- kw ['cursor_factory' ] = RealDictCursor
425
+ kw ['cursor_factory' ] = self . cursor_factory
415
426
return psycopg2 .extensions .connection .cursor (self , * a , ** kw )
416
427
417
428
0 commit comments