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

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 12:57:41 PST 2011


Revision: 6767
          http://trac.macosforge.org/projects/calendarserver/changeset/6767
Author:   glyph at apple.com
Date:     2011-01-19 12:57:41 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
uniqueness constraint

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py	2011-01-19 20:57:30 UTC (rev 6766)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py	2011-01-19 20:57:41 UTC (rev 6767)
@@ -47,8 +47,6 @@
 
 
 
-
-
 class Constraint(object):
 
     # Values for 'type' attribute:
@@ -163,7 +161,17 @@
         self.descriptiveComment = comment
 
 
+    def uniques(self):
+        """
+        @return: an iterable of C{set}s of C{Column}s which are unique within
+            this table.
+        """
+        for constraint in self.constraints:
+            if constraint.type is Constraint.UNIQUE:
+                yield set(constraint.affectsColumns)
 
+
+
 class Sequence(object):
     """
     A sequence object.

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py	2011-01-19 20:57:30 UTC (rev 6766)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py	2011-01-19 20:57:41 UTC (rev 6767)
@@ -164,7 +164,11 @@
             expect(self, cls=Parenthesis)
             self.primaryKey = 'MULTI-VALUE-KEY'
         elif constraintType.match(Keyword, 'UNIQUE'):
-            expect(self, cls=Parenthesis)
+            parens = iterSignificant(expect(self, cls=Parenthesis))
+            expect(parens, ttype=Punctuation, value="(")
+            idname = expect(parens, cls=Identifier).get_name()
+            expect(parens, ttype=Punctuation, value=")")
+            self.table.tableConstraint(Constraint.UNIQUE, [idname])
         else:
             raise ViolatedExpectation('PRIMARY or UNIQUE', constraintType)
         return self.checkEnd(self.next())
@@ -199,8 +203,8 @@
                 return self.checkEnd(val)
             else:
                 expected = True
-                def notNull():
-                    self.table.tableConstraint(Constraint.NOT_NULL,
+                def oneConstraint(t):
+                    self.table.tableConstraint(t,
                                                [theColumn.name])
 
                 if val.match(Keyword, 'PRIMARY'):
@@ -209,14 +213,14 @@
                     self.table.primaryKey = theColumn
                 elif val.match(Keyword, 'UNIQUE'):
                     # XXX add UNIQUE constraint
-                    pass
+                    oneConstraint(Constraint.UNIQUE)
                 elif val.match(Keyword, 'NOT'):
                     # possibly not necessary, as 'NOT NULL' is a single keyword
                     # in sqlparse as of 0.1.2
                     expect(self, ttype=Keyword, value='NULL')
-                    notNull()
+                    oneConstraint(Constraint.NOT_NULL)
                 elif val.match(Keyword, 'NOT NULL'):
-                    notNull()
+                    oneConstraint(Constraint.NOT_NULL)
                 elif val.match(Keyword, 'DEFAULT'):
                     theDefault = self.next()
                     if isinstance(theDefault, Function):

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py	2011-01-19 20:57:30 UTC (rev 6766)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py	2011-01-19 20:57:41 UTC (rev 6767)
@@ -14,6 +14,11 @@
 # limitations under the License.
 ##
 
+"""
+Tests for parsing an SQL schema, which cover L{txdav.base.datastore.sqlmodel}
+and L{txdav.base.datastore.sqlparser}.
+"""
+
 from txdav.base.datastore.sqlmodel import Schema
 
 from txdav.base.datastore.sqlparser import addSQLToSchema
@@ -59,15 +64,13 @@
         versa.
         """
         s = Schema()
-        addSQLToSchema(
-            s,
-            """
-            create sequence thingy;
-            create table thetable (
-                thecolumn integer default nextval('thingy')
-            );
-            """
-        )
+        addSQLToSchema(s,
+                       """
+                       create sequence thingy;
+                       create table thetable (
+                           thecolumn integer default nextval('thingy')
+                       );
+                       """)
         self.assertEquals(len(s.sequences), 1)
         self.assertEquals(s.sequences[0].name, "thingy")
         self.assertEquals(s.tables[0].columns[0].default, s.sequences[0])
@@ -91,3 +94,19 @@
         self.assertEquals(False, t.columnNamed('gamma').canBeNull())
 
 
+    def test_unique(self):
+        """
+        A column with a UNIQUE constraint in SQL will result in the table
+        listing that column as a unique set.
+        """
+        for identicalSchema in [
+                "create table sample (example integer unique);",
+                "create table sample (example integer, unique(example));"]:
+            s = Schema()
+            addSQLToSchema(s, identicalSchema)
+            table = s.tableNamed('sample')
+            column = table.columnNamed('example')
+            self.assertEquals(list(table.uniques()), [set([column])])
+
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/c9481ed9/attachment-0001.html>


More information about the calendarserver-changes mailing list