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

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 13:00:09 PST 2011


Revision: 6781
          http://trac.macosforge.org/projects/calendarserver/changeset/6781
Author:   glyph at apple.com
Date:     2011-01-19 13:00:08 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
Test for table/column mismatch in a query.

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:59:58 UTC (rev 6780)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 21:00:08 UTC (rev 6781)
@@ -21,6 +21,14 @@
 
 from txdav.base.datastore.sqlmodel import Schema, Table, Column
 
+
+class TableMismatch(Exception):
+    """
+    A table in a statement did not match with a column.
+    """
+
+
+
 class Syntax(object):
     """
     Base class for syntactic convenience.
@@ -91,7 +99,15 @@
             yield ColumnSyntax(column)
 
 
+    def tables(self):
+        return [self]
 
+
+    def __contains__(self, columnSyntax):
+        return (columnSyntax.model in self.model.columns)
+
+
+
 class Join(object):
     def __init__(self, firstTable, type, secondTableOrJoin, on):
         self.firstTable = firstTable
@@ -207,6 +223,7 @@
 
 
 
+
 ALL_COLUMNS = _AllColumns()
 
 class Select(object):
@@ -218,6 +235,12 @@
         if columns is None:
             columns = ALL_COLUMNS
         else:
+            for column in columns:
+                for table in From.tables():
+                    if column in table:
+                        break
+                else:
+                    raise TableMismatch()
             columns = SQLStatement(', '.join([c.model.name for c in columns]))
         self.columns = columns
         self.From = From

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:59:58 UTC (rev 6780)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 21:00:08 UTC (rev 6781)
@@ -14,10 +14,14 @@
 # limitations under the License.
 ##
 
+"""
+Tests for L{txdav.base.datastore.sqlsyntax}
+"""
 
 from txdav.base.datastore.sqlmodel import Schema
 from txdav.base.datastore.sqlparser import addSQLToSchema
-from txdav.base.datastore.sqlsyntax import SchemaSyntax, Select, SQLStatement
+from txdav.base.datastore.sqlsyntax import (
+    SchemaSyntax, Select, SQLStatement, TableMismatch)
 
 from twisted.trial.unittest import TestCase
 
@@ -136,3 +140,12 @@
             SQLStatement("select BAZ, BAR from FOO")
         )
 
+
+    def test_tableMismatch(self):
+        """
+        When a column in the 'columns' argument does not match the table from
+        the 'From' argument, L{Select} raises a L{TableMismatch}.
+        """
+        self.assertRaises(TableMismatch, Select, [self.schema.BOZ.QUX],
+                          From=self.schema.FOO)
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/d66673f9/attachment-0001.html>


More information about the calendarserver-changes mailing list