From 8446b805333f87e80a7680cfc8939d566fb033df Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 6 Aug 2007 21:52:24 +0000 Subject: [PATCH] - added desc() and asc() directly to CompareMixin --- doc/build/content/sqlexpression.txt | 5 ++--- lib/sqlalchemy/sql.py | 12 +++++++++++- test/sql/select.py | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/build/content/sqlexpression.txt b/doc/build/content/sqlexpression.txt index 761def71d8..702bc50990 100644 --- a/doc/build/content/sqlexpression.txt +++ b/doc/build/content/sqlexpression.txt @@ -539,8 +539,7 @@ We encounter search criterion of "name='jack'". So we apply WHERE criterion sta Next, we encounter that they'd like the results in descending order by full name. We apply ORDER BY, using an extra modifier `desc`: {python} - >>> from sqlalchemy.sql import desc - >>> query = query.order_by(desc(users.c.fullname)) + >>> query = query.order_by(users.c.fullname.desc()) We also come across that they'd like only users who have an address at MSN. A quick way to tack this on is by using an EXISTS clause, which we correlate to the `users` table in the enclosing SELECT: @@ -801,7 +800,7 @@ The `select()` function can take keyword arguments `order_by`, `group_by` (as we {stop}[(1, 2), (2, 2)] >>> s = select([addresses.c.email_address, addresses.c.id]).distinct().\ - ... order_by(desc(addresses.c.email_address), addresses.c.id) + ... order_by(addresses.c.email_address.desc(), addresses.c.id) {opensql}>>> conn.execute(s).fetchall() SELECT DISTINCT addresses.email_address, addresses.id FROM addresses ORDER BY addresses.email_address DESC, addresses.id diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index eef1a90462..06802cf6f6 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -1443,7 +1443,17 @@ class _CompareMixin(ColumnOperators): def label(self, name): """produce a column label, i.e. `` AS ``""" return _Label(name, self, self.type) - + + def desc(self): + """produce a DESC clause, i.e. `` DESC``""" + + return desc(self) + + def asc(self): + """produce a ASC clause, i.e. `` ASC``""" + + return asc(self) + def distinct(self): """produce a DISTINCT clause, i.e. ``DISTINCT ``""" return _UnaryExpression(self, operator="DISTINCT") diff --git a/test/sql/select.py b/test/sql/select.py index a7ce1059e2..2e099b01a2 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -405,6 +405,12 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A table2.select(order_by = [table2.c.otherid, asc(table2.c.othername)]), "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername ASC" ) + + self.runtest( + table2.select(order_by = [table2.c.otherid, table2.c.othername.desc()]), + "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername DESC" + ) + def testgroupby(self): self.runtest( select([table2.c.othername, func.count(table2.c.otherid)], group_by = [table2.c.othername]), -- 2.47.3