summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-11-19 19:29:18 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2013-11-19 19:29:18 -0500
commit02f21ffcf366da406795334d2fc6908a2f3c9b2f (patch)
treef3f92d882667d7133f9e623335134dd4dc59a5b0
parent63508b82cd5710c660383bcac5fcfd3bb6af83c1 (diff)
downloadsqlalchemy-02f21ffcf366da406795334d2fc6908a2f3c9b2f.tar.gz
- The :class:`.RowProxy` object is now sortable in Python as a regular
tuple is; this is accomplished via ensuring tuple() conversion on both sides within the ``__eq__()`` method as well as the addition of a ``__lt__()`` method. [ticket:2848]
-rw-r--r--doc/build/changelog/changelog_09.rst13
-rw-r--r--doc/build/changelog/migration_09.rst24
-rw-r--r--lib/sqlalchemy/engine/result.py5
-rw-r--r--test/aaa_profiling/test_resultset.py1
-rw-r--r--test/sql/test_query.py13
5 files changed, 55 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst
index 6b7eea2eb..6081951ec 100644
--- a/doc/build/changelog/changelog_09.rst
+++ b/doc/build/changelog/changelog_09.rst
@@ -15,6 +15,19 @@
:version: 0.9.0b2
.. change::
+ :tags: bug, engine
+ :tickets: 2848
+
+ The :class:`.RowProxy` object is now sortable in Python as a regular
+ tuple is; this is accomplished via ensuring tuple() conversion on
+ both sides within the ``__eq__()`` method as well as
+ the addition of a ``__lt__()`` method.
+
+ .. seealso::
+
+ :ref:`migration_2848`
+
+ .. change::
:tags: bug, orm
:tickets: 2833
diff --git a/doc/build/changelog/migration_09.rst b/doc/build/changelog/migration_09.rst
index 72968a8f5..a63c33430 100644
--- a/doc/build/changelog/migration_09.rst
+++ b/doc/build/changelog/migration_09.rst
@@ -287,6 +287,30 @@ The change is illustrated as follows::
:ticket:`2833`
+.. _migration_2848:
+
+``RowProxy`` now has tuple-sorting behavior
+-------------------------------------------
+
+The :class:`.RowProxy` object acts much like a tuple, but up until now
+would not sort as a tuple if a list of them were sorted using ``sorted()``.
+The ``__eq__()`` method now compares both sides as a tuple and also
+an ``__lt__()`` method has been added::
+
+ users.insert().execute(
+ dict(user_id=1, user_name='foo'),
+ dict(user_id=2, user_name='bar'),
+ dict(user_id=3, user_name='def'),
+ )
+
+ rows = users.select().order_by(users.c.user_name).execute().fetchall()
+
+ eq_(rows, [(2, 'bar'), (3, 'def'), (1, 'foo')])
+
+ eq_(sorted(rows), [(1, 'foo'), (2, 'bar'), (3, 'def')])
+
+:ticket:`2848`
+
.. _migration_2751:
Association Proxy SQL Expression Improvements and Fixes
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py
index 0e2316573..93c7d3b6b 100644
--- a/lib/sqlalchemy/engine/result.py
+++ b/lib/sqlalchemy/engine/result.py
@@ -125,8 +125,11 @@ class RowProxy(BaseRowProxy):
__hash__ = None
+ def __lt__(self, other):
+ return tuple(self) < tuple(other)
+
def __eq__(self, other):
- return other is self or other == tuple(self)
+ return other is self or tuple(other) == tuple(self)
def __ne__(self, other):
return not self.__eq__(other)
diff --git a/test/aaa_profiling/test_resultset.py b/test/aaa_profiling/test_resultset.py
index bbd8c4dba..d2f8c2256 100644
--- a/test/aaa_profiling/test_resultset.py
+++ b/test/aaa_profiling/test_resultset.py
@@ -53,6 +53,7 @@ class ResultSetTest(fixtures.TestBase, AssertsExecutionResults):
c1 in row
go()
+
class ExecutionTest(fixtures.TestBase):
def test_minimal_connection_execute(self):
diff --git a/test/sql/test_query.py b/test/sql/test_query.py
index 8e619fe74..8c5e1db4d 100644
--- a/test/sql/test_query.py
+++ b/test/sql/test_query.py
@@ -1090,6 +1090,19 @@ class QueryTest(fixtures.TestBase):
eq_(len(r), 1)
+ def test_sorting_in_python(self):
+ users.insert().execute(
+ dict(user_id=1, user_name='foo'),
+ dict(user_id=2, user_name='bar'),
+ dict(user_id=3, user_name='def'),
+ )
+
+ rows = users.select().order_by(users.c.user_name).execute().fetchall()
+
+ eq_(rows, [(2, 'bar'), (3, 'def'), (1, 'foo')])
+
+ eq_(sorted(rows), [(1, 'foo'), (2, 'bar'), (3, 'def')])
+
def test_column_order_with_simple_query(self):
# should return values in column definition order
users.insert().execute(user_id=1, user_name='foo')