[CalendarServer-changes] [9855] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Sep 25 15:00:32 PDT 2012


Revision: 9855
          http://trac.calendarserver.org//changeset/9855
Author:   glyph at apple.com
Date:     2012-09-25 15:00:32 -0700 (Tue, 25 Sep 2012)
Log Message:
-----------
Make syntax.Tuple iterable, and expect it as an OrderBy element, thereby allowing multi-column expressions to be used, as they are in twext.enterprise.record, as OrderBy clauses.

Modified Paths:
--------------
    CalendarServer/trunk/twext/enterprise/dal/syntax.py
    CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py

Property Changed:
----------------
    CalendarServer/trunk/

Modified: CalendarServer/trunk/twext/enterprise/dal/syntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/syntax.py	2012-09-25 21:46:36 UTC (rev 9854)
+++ CalendarServer/trunk/twext/enterprise/dal/syntax.py	2012-09-25 22:00:32 UTC (rev 9855)
@@ -773,7 +773,7 @@
 
 
 class ResultAliasSyntax(ExpressionSyntax):
-    
+
     def __init__(self, expression, alias=None):
         self.expression = expression
         self.alias = alias
@@ -797,7 +797,7 @@
 
 
 class AliasReferenceSyntax(ExpressionSyntax):
-    
+
     def __init__(self, resultAlias):
         self.resultAlias = resultAlias
 
@@ -997,6 +997,10 @@
         self.columns = columns
 
 
+    def __iter__(self):
+        return iter(self.columns)
+
+
     def subSQL(self, queryGenerator, allTables):
         return _inParens(_commaJoined(c.subSQL(queryGenerator, allTables)
                                       for c in self.columns))
@@ -1010,24 +1014,24 @@
     """
     A UNION, INTERSECT, or EXCEPT construct used inside a SELECT.
     """
-    
+
     OPTYPE_ALL = "all"
     OPTYPE_DISTINCT = "distinct"
 
     def __init__(self, selects, optype=None):
         """
-        
+
         @param selects: a single Select or a list of Selects
         @type selects: C{list} or L{Select}
         @param optype: whether to use the ALL, DISTINCT constructs: C{None} use neither, OPTYPE_ALL, or OPTYPE_DISTINCT
         @type optype: C{str}
         """
-        
+
         if isinstance(selects, Select):
             selects = (selects,)
         self.selects = selects
         self.optype = optype
-        
+
         for select in self.selects:
             if not isinstance(select, Select):
                 raise DALError("Must have SELECT statements in a set expression")
@@ -1086,7 +1090,7 @@
         self.From = From
         self.Where = Where
         self.Distinct = Distinct
-        if not isinstance(OrderBy, (list, tuple, type(None))):
+        if not isinstance(OrderBy, (Tuple, list, tuple, type(None))):
             OrderBy = [OrderBy]
         self.OrderBy = OrderBy
         if not isinstance(GroupBy, (list, tuple, type(None))):
@@ -1102,7 +1106,7 @@
             _checkColumnsMatchTables(columns, From.tables())
             columns = _SomeColumns(columns)
         self.columns = columns
-        
+
         self.ForUpdate = ForUpdate
         self.NoWait = NoWait
         self.Ascending = Ascending
@@ -1235,8 +1239,8 @@
             for table in self.From.tables():
                 tables.add(table.model)
             return [TableSyntax(table) for table in tables]
-        
 
+
 def _commaJoined(stmts):
     first = True
     cstatement = SQLFragment()

Modified: CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py	2012-09-25 21:46:36 UTC (rev 9854)
+++ CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py	2012-09-25 22:00:32 UTC (rev 9855)
@@ -37,6 +37,7 @@
 from twext.enterprise.test.test_adbapi2 import resultOf, AssertResultHelper
 from twisted.internet.defer import succeed
 from twisted.trial.unittest import TestCase
+from twext.enterprise.dal.syntax import Tuple
 
 
 
@@ -288,6 +289,21 @@
         )
 
 
+    def test_orderByParens(self):
+        """
+        L{Select}'s L{OrderBy} paraneter, if specified as a L{Tuple}, generates
+        an SQL expression I{without} parentheses, since the standard format
+        does not allow an arbitrary sort expression but rather a list of
+        columns.
+        """
+        self.assertEquals(
+            Select(From=self.schema.FOO,
+                   OrderBy=Tuple([self.schema.FOO.BAR,
+                                  self.schema.FOO.BAZ])).toSQL(),
+            SQLFragment("select * from FOO order by BAR, BAZ")
+        )
+
+
     def test_forUpdate(self):
         """
         L{Select}'s L{ForUpdate} parameter generates a 'for update' clause at
@@ -805,13 +821,13 @@
         )
 
         # Check various error situations
-        
+
         # No count not allowed
         self.assertRaises(DALError, self.schema.FOO.BAR.In, Parameter("names"))
-        
+
         # count=0 not allowed
         self.assertRaises(DALError, Parameter,"names", 0)
-        
+
         # Mismatched count and len(items)
         self.assertRaises(
             DALError,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120925/bdb7b3c8/attachment.html>


More information about the calendarserver-changes mailing list