]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added desc() and asc() directly to CompareMixin
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 6 Aug 2007 21:52:24 +0000 (21:52 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 6 Aug 2007 21:52:24 +0000 (21:52 +0000)
doc/build/content/sqlexpression.txt
lib/sqlalchemy/sql.py
test/sql/select.py

index 761def71d8a18f86f2fde2fa082186c27ea287a9..702bc50990dea24f2634737210d6af313cd71496 100644 (file)
@@ -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
index eef1a90462758e96d552d9709d86615a4f9726a2..06802cf6f6bfb262da9ad9801b6be2d5562364bc 100644 (file)
@@ -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")
index a7ce1059e2a9ac57b66ab3f18d5b7d9526a86ad7..2e099b01a2d55010f74c2aec5174ad9e2be34b4e 100644 (file)
@@ -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]),