[CalendarServer-changes] [6772] CalendarServer/branches/users/glyph/dal/txdav/base/datastore

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 12:58:33 PST 2011


Revision: 6772
          http://trac.macosforge.org/projects/calendarserver/changeset/6772
Author:   glyph at apple.com
Date:     2011-01-19 12:58:33 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
Add column comparisons, and make both column and constant comparisons raise an exception when tested for a truth value.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 20:58:23 UTC (rev 6771)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 20:58:33 UTC (rev 6772)
@@ -82,11 +82,14 @@
     modelType = Column
 
     def __eq__(self, other):
-        return ConstantComparison(self.model.name, '=', other)
+        if isinstance(other, ColumnSyntax):
+            return ColumnComparison(self, '=', other)
+        else:
+            return ConstantComparison(self, '=', other)
 
 
 
-class ConstantComparison(object):
+class Comparison(object):
 
     def __init__(self, a, op, b):
         self.a = a
@@ -94,11 +97,26 @@
         self.b = b
 
 
+    def __nonzero__(self):
+        raise ValueError(
+            "column comparisons should not be tested for truth value")
+
+
+
+
+class ConstantComparison(Comparison):
+
     def toSQL(self, placeholder, quote):
-        return (' '.join([self.a, self.op, placeholder]), [self.b])
+        return (' '.join([self.a.model.name, self.op, placeholder]), [self.b])
 
 
 
+class ColumnComparison(Comparison):
+    def toSQL(self, placeholder, quote):
+        return (' '.join([self.a.model.name, self.op, self.b.model.name]), [])
+
+
+
 class Select(object):
     """
     'select' statement.

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 20:58:23 UTC (rev 6771)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 20:58:33 UTC (rev 6772)
@@ -29,8 +29,8 @@
     def setUp(self):
         s = Schema(self.id())
         addSQLToSchema(schema=s, schemaData="""
-                       create table FOO (BAR integer);
-                       create table BAZ (QUX integer);
+                       create table FOO (BAR integer, BAZ integer);
+                       create table BOZ (QUX integer);
                        """)
         self.schema = SchemaSyntax(s)
 
@@ -59,10 +59,32 @@
         L{Select} generates a 'select' statement with the specified placeholder
         syntax and quoting function.
         """
-
         self.assertEquals(Select(From=self.schema.FOO,
                                  Where=self.schema.FOO.BAR == 1).toSQL(
                                  placeholder="*",
                                  quote=lambda partial: partial.replace("*", "**")),
                           ("select ** from FOO where BAR = *", [1]))
 
+
+    def test_columnComparison(self):
+        """
+        L{Select} generates a 'select' statement which compares columns.
+        """
+        self.assertEquals(Select(From=self.schema.FOO,
+                                 Where=self.schema.FOO.BAR ==
+                                 self.schema.FOO.BAZ).toSQL(),
+                          ("select * from FOO where BAR = BAZ", []))
+
+
+    def test_comparisonTestErrorPrevention(self):
+        """
+        The comparison object between columns raises an exception when compared
+        for a truth value, so that code will not accidentally test for '==' and
+        always get a true value.
+        """
+        def sampleComparison():
+            if self.schema.FOO.BAR == self.schema.FOO.BAZ:
+                return 'comparison should not succeed'
+        self.assertRaises(ValueError, sampleComparison)
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/93039a04/attachment-0001.html>


More information about the calendarserver-changes mailing list