@@ -341,20 +341,14 @@ def all(self, sql, parameters=None):
341
341
:type parameters: dict or tuple
342
342
:returns: :py:class:`list` of records or single values
343
343
344
- >>> for value in db.all("SELECT bar FROM foo"):
345
- ... print(value)
346
- ...
347
- buz
348
- bit
344
+ >>> db.all("SELECT * FROM foo ORDER BY bar")
345
+ [Record(bar=u'bit', baz=537), Record(bar=u'buz', baz=42)]
349
346
350
347
If the query results in a single column, we return a list of values
351
348
rather than a list of records of values.
352
349
353
- >>> for value in db.all("SELECT bar FROM foo"):
354
- ... print(value)
355
- ...
356
- buz
357
- bit
350
+ >>> db.all("SELECT bar FROM foo ORDER BY bar")
351
+ ['bit', 'buz']
358
352
359
353
"""
360
354
with self .get_transaction () as txn :
@@ -378,21 +372,25 @@ def one_or_zero(self, sql, parameters=None, zero=None):
378
372
Use this for the common case where there should only be one record, but
379
373
it may not exist yet.
380
374
375
+ >>> db.one_or_zero("SELECT * FROM foo WHERE bar='buz'")
376
+ Record(bar=u'buz', baz=42)
381
377
>>> row = db.one_or_zero("SELECT * FROM foo WHERE bar='blam'")
382
378
>>> if row is None:
383
379
... print("No blam yet.")
384
380
...
385
381
No blam yet.
386
382
387
- If the query returns one result and that result has one field and the
388
- value of that field is a :py:class:`~postgres.orm.Model` subclass, then
389
- we'll unravel and return just that instance for you.
383
+ If the query result has only one column, then we dereference that for
384
+ you.
385
+
386
+ >>> db.one_or_zero("SELECT baz FROM foo WHERE bar='buz'")
387
+ 42
390
388
391
389
"""
392
390
out = self ._some (sql , parameters , 0 , 1 )
393
391
if out is None :
394
392
out = zero
395
- elif len (out ) == 1 and isinstance ( out [ 0 ], Model ) :
393
+ elif len (out ) == 1 :
396
394
out = out [0 ]
397
395
return out
398
396
0 commit comments