@@ -249,7 +249,7 @@ def __str__(self):
249
249
250
250
class BadRecordType (Exception ):
251
251
def __str__ (self ):
252
- return "Bad record_type : {}. Available record_types are: tuple, " \
252
+ return "Bad back_as : {}. Available back_as values are: tuple, " \
253
253
"namedtuple, dict, or None (to use the default)." \
254
254
.format (self .args [0 ])
255
255
@@ -294,7 +294,7 @@ class Postgres(object):
294
294
Use :py:class:`psycopg2.extensions.cursor` for the default
295
295
:py:mod:`psycopg2` behavior, which is to return tuples. Whatever default
296
296
you set here, you can override that default on a per-call basis by passing
297
- :py:attr:`record_type ` or :py:attr:`cursor_factory` to
297
+ :py:attr:`back_as ` or :py:attr:`cursor_factory` to
298
298
:py:meth:`~postgres.Postgres.one`, :py:meth:`~postgres.Postgres.all`, and
299
299
:py:meth:`~postgres.Postgres.get_cursor`.
300
300
@@ -366,15 +366,14 @@ def run(self, sql, parameters=None, *a, **kw):
366
366
cursor .execute (sql , parameters )
367
367
368
368
369
- def one (self , sql , parameters = None , record_type = None , default = None , \
370
- * a , ** kw ):
369
+ def one (self , sql , parameters = None , back_as = None , default = None , * a , ** kw ):
371
370
"""Execute a query and return a single result or a default value.
372
371
373
372
:param string sql: the SQL statement to execute
374
373
:param parameters: the bind parameters for the SQL statement
375
374
:type parameters: dict or tuple
376
- :param record_type : the type of record to return
377
- :type record_type : type or string
375
+ :param back_as : the type of record to return
376
+ :type back_as : type or string
378
377
:param default: the value to return if no results are found
379
378
:param a: passed through to
380
379
:py:meth:`~postgres.Postgres.get_cursor`
@@ -409,14 +408,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
409
408
it's easy to just check the return value in the caller and do your
410
409
extra logic there.
411
410
412
- You can use :py:attr:`record_type ` to override the type associated with
413
- the default :py:attr:`cursor_factory` for your
411
+ You can use :py:attr:`back_as ` to override the type associated with the
412
+ default :py:attr:`cursor_factory` for your
414
413
:py:class:`~postgres.Postgres` instance:
415
414
416
415
>>> db.default_cursor_factory
417
416
<class 'psycopg2.extras.NamedTupleCursor'>
418
417
>>> db.one( "SELECT * FROM foo WHERE bar='buz'"
419
- ... , record_type =dict
418
+ ... , back_as =dict
420
419
... )
421
420
{'bar': 'buz', 'baz': 42}
422
421
@@ -425,11 +424,11 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
425
424
importing it in order to get dictionaries back. If you do need more
426
425
control (maybe you have a custom cursor class), you can pass
427
426
:py:attr:`cursor_factory` explicitly, and that will override any
428
- :py:attr:`record_type `:
427
+ :py:attr:`back_as `:
429
428
430
429
>>> from psycopg2.extensions import cursor
431
430
>>> db.one( "SELECT * FROM foo WHERE bar='buz'"
432
- ... , record_type =dict
431
+ ... , back_as =dict
433
432
... , cursor_factory=cursor
434
433
... )
435
434
('buz', 42)
@@ -450,14 +449,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
450
449
so it should work for both mappings and sequences.
451
450
452
451
>>> db.one( "SELECT sum(baz) FROM foo WHERE bar='nope'"
453
- ... , record_type =dict
452
+ ... , back_as =dict
454
453
... , default=0
455
454
... )
456
455
0
457
456
458
457
"""
459
458
460
- out = self ._some (sql , parameters , 0 , 1 , record_type , * a , ** kw )
459
+ out = self ._some (sql , parameters , 0 , 1 , back_as , * a , ** kw )
461
460
462
461
# dereference
463
462
if out is not None and len (out ) == 1 :
@@ -471,14 +470,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
471
470
return out
472
471
473
472
474
- def all (self , sql , parameters = None , record_type = None , * a , ** kw ):
473
+ def all (self , sql , parameters = None , back_as = None , * a , ** kw ):
475
474
"""Execute a query and return all results.
476
475
477
476
:param string sql: the SQL statement to execute
478
477
:param parameters: the bind parameters for the SQL statement
479
478
:type parameters: dict or tuple
480
- :param record_type : the type of record to return
481
- :type record_type : type or string
479
+ :param back_as : the type of record to return
480
+ :type back_as : type or string
482
481
:param a: passed through to
483
482
:py:meth:`~postgres.Postgres.get_cursor`
484
483
:param kw: passed through to
@@ -489,25 +488,25 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
489
488
>>> db.all("SELECT * FROM foo ORDER BY bar")
490
489
[Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
491
490
492
- You can use :py:attr:`record_type ` to override the type associated with
493
- the default :py:attr:`cursor_factory` for your
491
+ You can use :py:attr:`back_as ` to override the type associated with the
492
+ default :py:attr:`cursor_factory` for your
494
493
:py:class:`~postgres.Postgres` instance:
495
494
496
495
>>> db.default_cursor_factory
497
496
<class 'psycopg2.extras.NamedTupleCursor'>
498
- >>> db.all("SELECT * FROM foo ORDER BY bar", record_type =dict)
497
+ >>> db.all("SELECT * FROM foo ORDER BY bar", back_as =dict)
499
498
[{'bar': 'bit', 'baz': 537}, {'bar': 'buz', 'baz': 42}]
500
499
501
500
That's a convenience so you don't have to go to the trouble of
502
501
remembering where :py:class:`~psycopg2.extras.RealDictCursor` lives and
503
502
importing it in order to get dictionaries back. If you do need more
504
503
control (maybe you have a custom cursor class), you can pass
505
504
:py:attr:`cursor_factory` explicitly, and that will override any
506
- :py:attr:`record_type `:
505
+ :py:attr:`back_as `:
507
506
508
507
>>> from psycopg2.extensions import cursor
509
508
>>> db.all( "SELECT * FROM foo ORDER BY bar"
510
- ... , record_type =dict
509
+ ... , back_as =dict
511
510
... , cursor_factory=cursor
512
511
... )
513
512
[('bit', 537), ('buz', 42)]
@@ -522,11 +521,11 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
522
521
:py:meth:`__len__` and a :py:meth:`values` method) as well those that
523
522
are sequences:
524
523
525
- >>> db.all("SELECT baz FROM foo ORDER BY bar", record_type =dict)
524
+ >>> db.all("SELECT baz FROM foo ORDER BY bar", back_as =dict)
526
525
[537, 42]
527
526
528
527
"""
529
- with self .get_cursor (record_type = record_type , * a , ** kw ) as cursor :
528
+ with self .get_cursor (back_as = back_as , * a , ** kw ) as cursor :
530
529
cursor .execute (sql , parameters )
531
530
recs = cursor .fetchall ()
532
531
if recs and len (recs [0 ]) == 1 : # dereference
@@ -537,14 +536,14 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
537
536
return recs
538
537
539
538
540
- def _some (self , sql , parameters , lo , hi , record_type , * a , ** kw ):
539
+ def _some (self , sql , parameters , lo , hi , back_as , * a , ** kw ):
541
540
542
541
# This is undocumented because I think it's a rare case where this is
543
542
# wanted directly. It was added to make one and one_or_zero DRY when we
544
543
# had those two methods. Help yourself to _some now that you've found
545
544
# it. :^)
546
545
547
- with self .get_cursor (record_type = record_type , * a , ** kw ) as cursor :
546
+ with self .get_cursor (back_as = back_as , * a , ** kw ) as cursor :
548
547
cursor .execute (sql , parameters )
549
548
550
549
if cursor .rowcount < lo :
@@ -678,7 +677,7 @@ class CursorContextManager(object):
678
677
The return value of :py:func:`CursorContextManager.__enter__` is a
679
678
:py:mod:`psycopg2` cursor. Any positional and keyword arguments to our
680
679
constructor are passed through to the cursor constructor. If you pass
681
- :py:attr:`record_type ` as a keyword argument then we'll infer a
680
+ :py:attr:`back_as ` as a keyword argument then we'll infer a
682
681
:py:attr:`cursor_factory` from that, though any explicit
683
682
:py:attr:`cursor_factory` keyword argument will take precedence.
684
683
@@ -722,13 +721,13 @@ def __exit__(self, *exc_info):
722
721
723
722
724
723
def compute_cursor_factory (self , ** kw ):
725
- """Pull :py:attr:`record_type ` out of :py:attr:`kw` and maybe add
724
+ """Pull :py:attr:`back_as ` out of :py:attr:`kw` and maybe add
726
725
:py:attr:`cursor_factory`.
727
726
728
- Valid values for :py:attr:`record_type ` are :py:class:`tuple`,
727
+ Valid values for :py:attr:`back_as ` are :py:class:`tuple`,
729
728
:py:class:`namedtuple`, :py:class:`dict` (or the strings ``tuple``,
730
729
``namedtuple``, and ``dict``), and :py:class:`None`. If the value of
731
- :py:attr:`record_type ` is :py:class:`None`, then we won't insert any
730
+ :py:attr:`back_as ` is :py:class:`None`, then we won't insert any
732
731
:py:attr:`cursor_factory` keyword argument. Otherwise we'll specify a
733
732
:py:attr:`cursor_factory` that will result in records of the specific
734
733
type: :py:class:`psycopg2.extensions.cursor` for :py:class:`tuple`,
@@ -738,17 +737,17 @@ def compute_cursor_factory(self, **kw):
738
737
739
738
"""
740
739
741
- # Pull record_type out of kw.
740
+ # Pull back_as out of kw.
742
741
# ===========================
743
742
# If we leave it in psycopg2 will complain. Our internal calls to
744
743
# get_cursor always have it but external use might not.
745
744
746
- record_type = kw .pop ('record_type ' , None )
745
+ back_as = kw .pop ('back_as ' , None )
747
746
748
747
749
748
if 'cursor_factory' not in kw :
750
749
751
- # Compute cursor_factory from record_type .
750
+ # Compute cursor_factory from back_as .
752
751
# ====================================
753
752
754
753
cursor_factory_registry = { tuple : RegularCursor
@@ -760,10 +759,10 @@ def compute_cursor_factory(self, **kw):
760
759
, None : None
761
760
}
762
761
763
- if record_type not in cursor_factory_registry :
764
- raise BadRecordType (record_type )
762
+ if back_as not in cursor_factory_registry :
763
+ raise BadRecordType (back_as )
765
764
766
- cursor_factory = cursor_factory_registry [record_type ]
765
+ cursor_factory = cursor_factory_registry [back_as ]
767
766
if cursor_factory is not None :
768
767
kw ['cursor_factory' ] = cursor_factory
769
768
0 commit comments