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

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


Revision: 6766
          http://trac.macosforge.org/projects/calendarserver/changeset/6766
Author:   glyph at apple.com
Date:     2011-01-19 12:57:30 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
notNull 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:19 UTC (rev 6765)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py	2011-01-19 20:57:30 UTC (rev 6766)
@@ -47,11 +47,16 @@
 
 
 
+
+
 class Constraint(object):
 
-    def __init__(self, type, affectsColumns, name=None):
+    # Values for 'type' attribute:
+    NOT_NULL = 'NOT NULL'
+    UNIQUE = 'UNIQUE'
+
+    def __init__(self, type, affectsColumns):
         self.affectsColumns = affectsColumns
-        self.name = name
         # XXX: possibly different constraint types should have different
         # classes?
         self.type = type
@@ -91,6 +96,14 @@
         return '<Column (%s %r)>' % (self.name, self.type)
 
 
+    def canBeNull(self):
+        for constraint in self.table.constraints:
+            if self in constraint.affectsColumns:
+                if constraint.type is Constraint.NOT_NULL:
+                    return False
+        return True
+
+
     def setDefaultValue(self, value):
         self.default = value
 
@@ -136,7 +149,7 @@
         affectsColumns = []
         for name in columnNames:
             affectsColumns.append(self.columnNamed(name))
-        self.constraints.append(Constraint(constraintType, columnNames))
+        self.constraints.append(Constraint(constraintType, affectsColumns))
 
 
     def insertSchemaRow(self, values):

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:19 UTC (rev 6765)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py	2011-01-19 20:57:30 UTC (rev 6766)
@@ -27,6 +27,7 @@
                           Function)
 
 from txdav.base.datastore.sqlmodel import Schema, Table, SQLType, ProcedureCall
+from txdav.base.datastore.sqlmodel import Constraint
 from txdav.base.datastore.sqlmodel import Sequence
 
 def _fixKeywords():
@@ -198,6 +199,10 @@
                 return self.checkEnd(val)
             else:
                 expected = True
+                def notNull():
+                    self.table.tableConstraint(Constraint.NOT_NULL,
+                                               [theColumn.name])
+
                 if val.match(Keyword, 'PRIMARY'):
                     expect(self, ttype=Keyword, value='KEY')
                     # XXX check to make sure there's no other primary key yet
@@ -206,11 +211,12 @@
                     # XXX add UNIQUE constraint
                     pass
                 elif val.match(Keyword, 'NOT'):
-                    # no longer necessary
+                    # possibly not necessary, as 'NOT NULL' is a single keyword
+                    # in sqlparse as of 0.1.2
                     expect(self, ttype=Keyword, value='NULL')
+                    notNull()
                 elif val.match(Keyword, 'NOT NULL'):
-                    # XXX add NOT NULL constraint
-                    pass
+                    notNull()
                 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:19 UTC (rev 6765)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py	2011-01-19 20:57:30 UTC (rev 6766)
@@ -75,4 +75,19 @@
                           [s.tables[0].columns[0]])
 
 
+    def test_notNull(self):
+        """
+        A column with a NOT NULL constraint in SQL will be parsed as a
+        constraint which returns False from its C{canBeNull()} method.
+        """
+        s = Schema()
+        addSQLToSchema(s,
+                       """
+                       create table alpha (beta integer,
+                                           gamma integer not null);
+                       """)
+        t = s.tableNamed('alpha')
+        self.assertEquals(True, t.columnNamed('beta').canBeNull())
+        self.assertEquals(False, t.columnNamed('gamma').canBeNull())
 
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/1967f30a/attachment-0001.html>


More information about the calendarserver-changes mailing list