Author: test
Date: 2005-11-02 21:37:43 +0000 (Wed, 02 Nov 2005)
New Revision: 1197
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/sresults.py
SQLObject/trunk/sqlobject/tests/test_select.py
Log:
Added .getOne() method to SelectResults
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2005-11-02 20:51:34 UTC (rev 1196)
+++ SQLObject/trunk/docs/News.txt 2005-11-02 21:37:43 UTC (rev 1197)
@@ -43,6 +43,13 @@
This will run ``process_request(os.environ)``. The return
value will be preserved.
+* Added method ``.getOne([default])`` to ``SelectResults`` (these are
+ the objects returned by ``.select()`` and ``.selectBy()``). This
+ returns a single object, when the query is expected to return only
+ one object. The single argument is the value to return when zero
+ results are found (more than one result is always an error). If no
+ default is given, it is an error if no such object exists.
+
SQLObject 0.7.1
===============
Modified: SQLObject/trunk/sqlobject/sresults.py
===================================================================
--- SQLObject/trunk/sqlobject/sresults.py 2005-11-02 20:51:34 UTC (rev 1196)
+++ SQLObject/trunk/sqlobject/sresults.py 2005-11-02 21:37:43 UTC (rev 1197)
@@ -1,5 +1,6 @@
import sqlbuilder
import dbconnection
+import main
StringType = type('')
@@ -227,4 +228,29 @@
def max(self, attribute):
return self.accumulateOne("MAX", attribute)
+ def getOne(self, default=sqlbuilder.NoDefault):
+ """
+ If a query is expected to only return a single value,
+ using ``.getOne()`` will return just that value.
+
+ If not results are found, ``SQLObjectNotFound`` will be
+ raised, unless you pass in a default value (like
+ ``.getOne(None)``).
+
+ If more than one result is returned,
+ ``SQLObjectIntegrityError`` will be raised.
+ """
+ results = list(self)
+ if not results:
+ if default is sqlbuilder.NoDefault:
+ raise main.SQLObjectNotFound(
+ "No results matched the query for %s"
+ % self.sourceClass.__name__)
+ return default
+ if len(results) > 1:
+ raise main.SQLObjectIntegrityError(
+ "More than one result returned from query: %s"
+ % results)
+ return results[0]
+
__all__ = ['SelectResults']
Modified: SQLObject/trunk/sqlobject/tests/test_select.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_select.py 2005-11-02 20:51:34 UTC (rev 1196)
+++ SQLObject/trunk/sqlobject/tests/test_select.py 2005-11-02 21:37:43 UTC (rev 1197)
@@ -1,6 +1,8 @@
from __future__ import generators # for enumerate
from sqlobject import *
+from sqlobject.main import SQLObjectIntegrityError
from sqlobject.tests.dbtest import *
+from py.test import raises
try:
enumerate
@@ -71,6 +73,18 @@
pass
assert count == len(names)
+def test_select_getOne():
+ setupClass(IterTest)
+ a = IterTest(name='a')
+ b = IterTest(name='b')
+ assert IterTest.selectBy(name='a').getOne() == a
+ assert IterTest.select(IterTest.q.name=='b').getOne() == b
+ assert IterTest.selectBy(name='c').getOne(None) is None
+ raises(SQLObjectNotFound, 'IterTest.selectBy(name="c").getOne()')
+ b2 = IterTest(name='b')
+ raises(SQLObjectIntegrityError, 'IterTest.selectBy(name="b").getOne()')
+ raises(SQLObjectIntegrityError, 'IterTest.selectBy(name="b").getOne(None)')
+
|