summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-06-20 22:28:54 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-06-20 22:28:54 +0000
commita1fe6e2c7607740c46eaab89e2bcd70aaccee71e (patch)
tree6ba2a60d454295779989c9dc4101b91a6ed2d375 /lib/sqlalchemy
parent48ebb102eaeb828fc320b16dce17883723ed5904 (diff)
downloadsqlalchemy-a1fe6e2c7607740c46eaab89e2bcd70aaccee71e.tar.gz
- forwards-compatibility with 0.4: added one(), first(), and
all() to Query - added selectone_by() to assignmapper
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/ext/assignmapper.py2
-rw-r--r--lib/sqlalchemy/orm/query.py45
2 files changed, 44 insertions, 3 deletions
diff --git a/lib/sqlalchemy/ext/assignmapper.py b/lib/sqlalchemy/ext/assignmapper.py
index b124de8f6..4708afd8d 100644
--- a/lib/sqlalchemy/ext/assignmapper.py
+++ b/lib/sqlalchemy/ext/assignmapper.py
@@ -43,7 +43,7 @@ def assign_mapper(ctx, class_, *args, **kwargs):
m = mapper(class_, extension=extension, *args, **kwargs)
class_.mapper = m
class_.query = classmethod(lambda cls: Query(class_, session=ctx.current))
- for name in ['get', 'filter', 'filter_by', 'select', 'select_by', 'selectfirst', 'selectfirst_by', 'selectone', 'get_by', 'join_to', 'join_via', 'count', 'count_by', 'options', 'instances']:
+ for name in ['get', 'filter', 'filter_by', 'select', 'select_by', 'selectfirst', 'selectfirst_by', 'selectone', 'selectone_by', 'get_by', 'join_to', 'join_via', 'count', 'count_by', 'options', 'instances']:
monkeypatch_query_method(ctx, class_, name)
for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update']:
monkeypatch_objectstore_method(ctx, class_, name)
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 2266f9fa8..c29933ef9 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -828,15 +828,56 @@ class Query(object):
return list(self)
- def scalar(self):
+ def one(self):
+ """Return the first result of this ``Query``, raising an exception if more than one row exists.
+
+ This results in an execution of the underlying query.
+
+ this method is for forwards-compatibility with 0.4.
+ """
+
+ if self._col is None or self._func is None:
+ ret = list(self[0:2])
+
+ if len(ret) == 1:
+ return ret[0]
+ elif len(ret) == 0:
+ raise exceptions.InvalidRequestError('No rows returned for one()')
+ else:
+ raise exceptions.InvalidRequestError('Multiple rows returned for one()')
+ else:
+ return self._col_aggregate(self._col, self._func)
+
+ def first(self):
"""Return the first result of this ``Query``.
This results in an execution of the underlying query.
+
+ this method is for forwards-compatibility with 0.4.
"""
+
if self._col is None or self._func is None:
- return self[0]
+ ret = list(self[0:1])
+ if len(ret) > 0:
+ return ret[0]
+ else:
+ return None
else:
return self._col_aggregate(self._col, self._func)
+
+ def all(self):
+ return self.list()
+
+ def scalar(self):
+ """Return the first result of this ``Query``.
+
+ This results in an execution of the underlying query.
+
+ this method will be deprecated in 0.4; first() is added for
+ forwards-compatibility.
+ """
+
+ return self.first()
def __iter__(self):
return iter(self.select_whereclause())