summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-08-06 21:52:24 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-08-06 21:52:24 +0000
commit8446b805333f87e80a7680cfc8939d566fb033df (patch)
tree25ddfa7fe3a7b5f468b468a4874ce537e62e184f
parent2dde45881b07df9233461c9a19b647f9b7dc816c (diff)
downloadsqlalchemy-8446b805333f87e80a7680cfc8939d566fb033df.tar.gz
- added desc() and asc() directly to CompareMixin
-rw-r--r--doc/build/content/sqlexpression.txt5
-rw-r--r--lib/sqlalchemy/sql.py12
-rw-r--r--test/sql/select.py6
3 files changed, 19 insertions, 4 deletions
diff --git a/doc/build/content/sqlexpression.txt b/doc/build/content/sqlexpression.txt
index 761def71d..702bc5099 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 eef1a9046..06802cf6f 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. ``<columnname> AS <name>``"""
return _Label(name, self, self.type)
-
+
+ def desc(self):
+ """produce a DESC clause, i.e. ``<columnname> DESC``"""
+
+ return desc(self)
+
+ def asc(self):
+ """produce a ASC clause, i.e. ``<columnname> ASC``"""
+
+ return asc(self)
+
def distinct(self):
"""produce a DISTINCT clause, i.e. ``DISTINCT <columnname>``"""
return _UnaryExpression(self, operator="DISTINCT")
diff --git a/test/sql/select.py b/test/sql/select.py
index a7ce1059e..2e099b01a 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]),