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

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


Revision: 6774
          http://trac.macosforge.org/projects/calendarserver/changeset/6774
Author:   glyph at apple.com
Date:     2011-01-19 12:58:54 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
basic joins

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:44 UTC (rev 6773)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlsyntax.py	2011-01-19 20:58:54 UTC (rev 6774)
@@ -68,6 +68,19 @@
 
     modelType = Table
 
+    def join(self, otherTableSyntax, on, type=''):
+        return Join(self, type, otherTableSyntax, on)
+
+
+    def toSQL(self, placeholder, quote):
+        """
+        For use in a 'from' clause.
+        """
+        # XXX maybe there should be a specific method which is only invoked
+        # from the FROM clause, that only tables and joins would implement?
+        return SQLStatement(self.model.name)
+
+
     def __getattr__(self, attr):
         return ColumnSyntax(self.model.columnNamed(attr))
 
@@ -77,6 +90,31 @@
             yield ColumnSyntax(column)
 
 
+
+class Join(object):
+    def __init__(self, firstTable, type, secondTableOrJoin, on):
+        self.firstTable = firstTable
+        self.type = type
+        self.secondTableOrJoin = secondTableOrJoin
+        self.on = on
+
+
+    def toSQL(self, placeholder, quote):
+        stmt = SQLStatement()
+        stmt.append(self.firstTable.toSQL(placeholder, quote))
+        stmt.text += ' '
+        if self.type:
+            stmt.text += self.type
+            stmt.text += ' '
+        stmt.text += 'join '
+        stmt.append(self.secondTableOrJoin.toSQL(placeholder, quote))
+        stmt.text += ' on '
+        stmt.append(self.on.toSQL(placeholder, quote))
+
+        return stmt
+
+
+
 def comparison(comparator):
     def __(self, other):
         if isinstance(other, ColumnSyntax):
@@ -174,7 +212,8 @@
         """
         @return: a 2-tuple of (sql, args).
         """
-        stmt = SQLStatement(quote("select * from ") + self.From.model.name)
+        stmt = SQLStatement(quote("select * from "))
+        stmt.append(self.From.toSQL(placeholder, quote))
         if self.Where is not None:
             wherestmt = self.Where.toSQL(placeholder, quote)
             stmt.text += quote(" where ")
@@ -210,3 +249,7 @@
         if not isinstance(stmt, SQLStatement):
             return NotImplemented
         return not self.__eq__(stmt)
+
+
+    def __repr__(self):
+        return 'SQLStatement' + repr((self.text, self.parameters))

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:44 UTC (rev 6773)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_sqlsyntax.py	2011-01-19 20:58:54 UTC (rev 6774)
@@ -88,7 +88,6 @@
         self.assertRaises(ValueError, sampleComparison)
 
 
-
     def test_compoundWhere(self):
         """
         L{Select.And} and L{Select.Or} will return compound columns.
@@ -100,3 +99,14 @@
             SQLStatement("select * from FOO where BAR < ? or BAR > ?", [2, 5]))
 
 
+    def test_joinClause(self):
+        """
+        A table's .join() method returns a join statement in a SELECT.
+        """
+        self.assertEquals(
+            Select(From=self.schema.FOO.join(
+                self.schema.BOZ, self.schema.FOO.BAR ==
+                self.schema.BOZ.QUX)).toSQL(),
+            SQLStatement("select * from FOO join BOZ on BAR = QUX", [])
+        )
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/934f8ab1/attachment-0001.html>


More information about the calendarserver-changes mailing list