@@ -322,21 +322,15 @@ def all(self, sql, parameters=None):
322
322
cursor .execute (sql , parameters )
323
323
return cursor .fetchall ()
324
324
325
- def rows (self , * a , ** kw ):
326
325
327
- # This is for backwards compatibility, see #16. It is stubbed instead
328
- # of aliased to avoid showing up in our docs via sphinx autodoc.
329
-
330
- return self .all (* a , ** kw )
331
-
332
-
333
- def one_or_zero (self , sql , parameters = None ):
334
- """Execute a query and return a single result or :py:class:`None`.
326
+ def one_or_zero (self , sql , parameters = None , zero = None ):
327
+ """Execute a query and return a single result or a default value.
335
328
336
329
:param unicode sql: the SQL statement to execute
337
330
:param parameters: the bind parameters for the SQL statement
338
331
:type parameters: dict or tuple
339
- :returns: a single row or :py:const:`None`
332
+ :returns: a single row or the value of the :py:attr:`zero` argument
333
+ :param zero: the value to return if zero results are found
340
334
:raises: :py:exc:`~postgres.TooFew` or :py:exc:`~postgres.TooMany`
341
335
342
336
Use this for the common case where there should only be one record, but
@@ -349,64 +343,18 @@ def one_or_zero(self, sql, parameters=None):
349
343
No blam yet.
350
344
351
345
"""
352
- return self ._some (sql , parameters , 0 , 1 )
353
-
354
-
355
- def one (self , sql , parameters = None , strict = None ):
356
-
357
- # I'm half-considering dropping this. Now that one_or_zero exists, this
358
- # is really only useful for what should really be called db.first, and
359
- # in that case why aren't you using a LIMIT 1?
360
-
361
- """Execute a query and return a single result.
362
-
363
- :param unicode sql: the SQL statement to execute
364
- :param parameters: the bind parameters for the SQL statement
365
- :type parameters: dict or tuple
366
- :param strict: whether to raise when there isn't exactly one result
367
- :type strict: :py:class:`bool`
368
- :returns: a single row or :py:const:`None`
369
- :raises: :py:exc:`~postgres.TooFew` or :py:exc:`~postgres.TooMany`
370
-
371
- By default, :py:attr:`strict` ends up evaluating to :py:class:`True`,
372
- in which case we raise :py:exc:`postgres.TooFew` or
373
- :py:exc:`postgres.TooMany` if the number of rows returned isn't exactly
374
- one (both are subclasses of :py:exc:`postgres.OutOfBounds`). You can
375
- override this behavior per-call with the :py:attr:`strict` argument
376
- here, or globally by passing :py:attr:`strict_one` to the
377
- :py:class:`~postgres.Postgres` constructor. If you use both, the
378
- :py:attr:`strict` argument here wins. If you pass :py:class:`False`
379
- for :py:attr:`strict`, then we return :py:class:`None` if there are no
380
- results, and the first if there is more than one.
381
-
382
- >>> row = db.one("SELECT * FROM foo WHERE bar='baz'")
383
- >>> print(row["bar"])
384
- baz
385
-
386
- """
387
- if strict not in (True , False , None ):
388
- raise ValueError ("strict must be True, False, or None." )
389
-
390
- if strict is None :
391
- if self .strict_one is None :
392
- strict = True # library default
393
- else :
394
- strict = self .strict_one # user default
395
-
396
- if strict :
397
- out = self ._some (sql , parameters , 1 , 1 )
398
- else :
399
- with self .get_cursor () as cursor :
400
- cursor .execute (sql , parameters )
401
- out = cursor .fetchone ()
346
+ out = self ._some (sql , parameters , 0 , 1 )
347
+ if out is None :
348
+ out = zero
402
349
return out
403
350
404
351
405
352
def _some (self , sql , parameters = None , lo = 0 , hi = 1 ):
406
353
407
354
# This is undocumented (and largely untested) because I think it's a
408
- # rare case where this is wanted directly. It's here to make one and
409
- # one_or_zero DRY. Help yourself to it now that you've found it. :^)
355
+ # rare case where this is wanted directly. It was added to make one and
356
+ # one_or_zero DRY when we had one. Help yourself to it now that you've
357
+ # found it. :^)
410
358
411
359
with self .get_transaction () as txn :
412
360
txn .execute (sql , parameters )
0 commit comments