[CalendarServer-changes] [6889] CalendarServer/trunk/twext/enterprise/dal

source_changes at macosforge.org source_changes at macosforge.org
Fri Feb 4 18:16:07 PST 2011


Revision: 6889
          http://trac.macosforge.org/projects/calendarserver/changeset/6889
Author:   glyph at apple.com
Date:     2011-02-04 18:16:05 -0800 (Fri, 04 Feb 2011)
Log Message:
-----------
Full / proper subexpression support, with a few hacks to support previous (non-parenthetical) formatting style in tests.

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

Modified: CalendarServer/trunk/twext/enterprise/dal/syntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/syntax.py	2011-02-05 02:09:24 UTC (rev 6888)
+++ CalendarServer/trunk/twext/enterprise/dal/syntax.py	2011-02-05 02:16:05 UTC (rev 6889)
@@ -69,6 +69,7 @@
     """
 
     modelType = None
+    model = None
 
     def __init__(self, model):
         if not isinstance(model, self.modelType):
@@ -78,7 +79,9 @@
 
 
     def __repr__(self):
-        return '<Syntax for: %r>' % (self.model,)
+        if self.model is not None:
+            return '<Syntax for: %r>' % (self.model,)
+        return super(Syntax, self).__repr__()
 
 
 
@@ -103,8 +106,15 @@
     __le__ = comparison('<=')
     __add__ = comparison("+")
     __sub__ = comparison("-")
+    __div__= comparison("/")
+    __mul__= comparison("*")
 
 
+    def __nonzero__(self):
+        raise ValueError(
+            "SQL expressions should not be tested for truth value in Python.")
+
+
     def In(self, subselect):
         # Can't be Select.__contains__ because __contains__ gets __nonzero__
         # called on its result by the 'in' syntax.
@@ -276,7 +286,7 @@
 
 
 
-class Comparison(object):
+class Comparison(ExpressionSyntax):
 
     def __init__(self, a, op, b):
         self.a = a
@@ -284,9 +294,11 @@
         self.b = b
 
 
-    def __nonzero__(self):
-        raise ValueError(
-            "column comparisons should not be tested for truth value")
+    def _subexpression(self, expr, placeholder, quote, allTables):
+        result = expr.subSQL(placeholder, quote, allTables)
+        if self.op not in ('and', 'or') and isinstance(expr, Comparison):
+            result = _inParens(result)
+        return result
 
 
     def booleanOp(self, operand, other):
@@ -325,12 +337,12 @@
 class ConstantComparison(Comparison):
 
     def allColumns(self):
-        return [self.a]
+        return self.a.allColumns()
 
 
     def subSQL(self, placeholder, quote, allTables):
         sqls = SQLFragment()
-        sqls.append(self.a.subSQL(placeholder, quote, allTables))
+        sqls.append(self._subexpression(self.a, placeholder, quote, allTables))
         sqls.append(SQLFragment(' ' + ' '.join([self.op, placeholder]),
                                  [self.b]))
         return sqls
@@ -343,11 +355,15 @@
     (currently only AND or OR).
     """
 
+    def allColumns(self):
+        return self.a.allColumns() + self.b.allColumns()
+
+
     def subSQL(self, placeholder, quote, allTables):
         stmt = SQLFragment()
-        stmt.append(self.a.subSQL(placeholder, quote, allTables))
+        stmt.append(self._subexpression(self.a, placeholder, quote, allTables))
         stmt.text += ' %s ' % (self.op,)
-        stmt.append(self.b.subSQL(placeholder, quote, allTables))
+        stmt.append(self._subexpression(self.b, placeholder, quote, allTables))
         return stmt
 
 

Modified: CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py	2011-02-05 02:09:24 UTC (rev 6888)
+++ CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py	2011-02-05 02:16:05 UTC (rev 6889)
@@ -317,9 +317,7 @@
             SQLFragment("select ((BAR + BAZ) / ?) * ? from FOO", [3, 7])
         )
 
-    test_multiColumnExpression.todo = 'not implemented yet'
 
-
     def test_len(self):
         """
         Test for the 'Len' function for determining character length of a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110204/2091ff65/attachment-0001.html>


More information about the calendarserver-changes mailing list