commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7639 - in trunk/gnue-common: doc src/datasources src/datasources


From: johannes
Subject: [gnue] r7639 - in trunk/gnue-common: doc src/datasources src/datasources/drivers/Base src/datasources/drivers/DBSIG2 src/datasources/drivers/sql/interbase src/datasources/drivers/sql/postgres
Date: Wed, 22 Jun 2005 08:01:38 -0500 (CDT)

Author: johannes
Date: 2005-06-22 08:01:37 -0500 (Wed, 22 Jun 2005)
New Revision: 7639

Modified:
   trunk/gnue-common/doc/gnue-schema.dtd
   trunk/gnue-common/src/datasources/GSchema.py
   trunk/gnue-common/src/datasources/drivers/Base/Behavior.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/Behavior.py
   trunk/gnue-common/src/datasources/drivers/sql/interbase/Behavior.py
   trunk/gnue-common/src/datasources/drivers/sql/postgres/Behavior.py
Log:
Added unique-constraint to gnue-schema and behavior


Modified: trunk/gnue-common/doc/gnue-schema.dtd
===================================================================
--- trunk/gnue-common/doc/gnue-schema.dtd       2005-06-22 11:29:20 UTC (rev 
7638)
+++ trunk/gnue-common/doc/gnue-schema.dtd       2005-06-22 13:01:37 UTC (rev 
7639)
@@ -109,7 +109,7 @@
 <!-- ======================= -->
 <!-- Child elements of table -->
 <!-- ======================= -->
-<!ELEMENT constraints (foreignkey*)>
+<!ELEMENT constraints (foreignkey*, unique*)>
 
 <!ELEMENT fields (field*, import-field*)>
 
@@ -139,7 +139,10 @@
 <!ATTLIST foreignkey name %name; #REQUIRED>
 <!ATTLIST foreignkey references %name; #IMPLIED>
 
+<!ELEMENT unique (uqfield*)>
+<!ATTLIST unique name %name; #REQUIRED>
 
+
 <!-- ======================== -->
 <!-- Child elements of fields -->
 <!-- ======================== -->
@@ -189,6 +192,13 @@
 <!ATTLIST fkfield references %name; #REQUIRED>
 
 
+<!-- ======================== -->
+<!-- Child elements of unique -->
+<!-- ======================== -->
+<!ELEMENT uqfield EMPTY>
+<!ATTLIST uqfield name %name; #REQUIRED>
+
+
 <!-- ======================= -->
 <!-- Child elements of index -->
 <!-- ======================= -->
@@ -197,5 +207,5 @@
 
 
 <!-- This DTD was created by gnuedtd
-     Updated: 2005-06-22 13:25:16   -->
+     Updated: 2005-06-22 14:57:42   -->
 

Modified: trunk/gnue-common/src/datasources/GSchema.py
===================================================================
--- trunk/gnue-common/src/datasources/GSchema.py        2005-06-22 11:29:20 UTC 
(rev 7638)
+++ trunk/gnue-common/src/datasources/GSchema.py        2005-06-22 13:01:37 UTC 
(rev 7639)
@@ -127,9 +127,22 @@
   def __init__ (self, parent, **params):
     GSObject.__init__(self, parent, 'GSFKField', **params)
 
+# =============================================================================
 
+class GSUnique (GSObject):
+  def __init__ (self, parent, **params):
+    GSObject.__init__ (self, parent, 'GSUnique', **params)
+
+
 # =============================================================================
 
+class GSUQField (GSObject, GLeafNode):
+  def __init__ (self, parent, **params):
+    GSObject.__init__(self, parent, 'GSUQField', **params)
+
+
+# =============================================================================
+
 class GSIndexes (GUndividedCollection):
   def __init__ (self, parent, **params):
     GUndividedCollection.__init__(self, parent, 'GSIndexes', **params)
@@ -362,6 +375,22 @@
                'Typecast': GTypecast.name } },
          'ParentTags':  ('foreignkey',) },
 
+      'unique':    {
+         'BaseClass': GSUnique,
+         'Attributes': {
+            'name': {
+               'Required': 1,
+               'Typecast': GTypecast.name } },
+         'ParentTags':  ('constraints',) },
+
+      'uqfield':   {
+         'BaseClass': GSUQField,
+         'Attributes': {
+            'name':        {
+               'Required': 1,
+               'Typecast': GTypecast.name } },
+         'ParentTags':  ('unique',) },
+
       'indexes':   {
          'BaseClass': GSIndexes,
          'SingleInstance': 1,

Modified: trunk/gnue-common/src/datasources/drivers/Base/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/Base/Behavior.py  2005-06-22 
11:29:20 UTC (rev 7638)
+++ trunk/gnue-common/src/datasources/drivers/Base/Behavior.py  2005-06-22 
13:01:37 UTC (rev 7639)
@@ -117,6 +117,8 @@
     self._lookups  = self.__getLookups (self._current)
     self._elements = {}
 
+    gDebug (2, self._diff.dumpXML ())
+
     return self._writeSchema_ (self._current, self._new, self._diff, 
simulation)
 
 

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/Behavior.py        
2005-06-22 11:29:20 UTC (rev 7638)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/Behavior.py        
2005-06-22 13:01:37 UTC (rev 7639)
@@ -136,6 +136,12 @@
 
         pre.extend (self._dropConstraint_ (constraint))
 
+    for constraint in table.findChildrenOfType ('GSUnique', False, True):
+      if constraint._action == "remove":
+        csKey = "CONSTRAINT_%s" % constraint.name
+        if csKey in self._lookups:
+          del self._lookups [csKey]
+
     for index in table.findChildrenOfType ('GSIndex', False, True):
       if index._action == "remove":
         ixKey = "INDEX_%s" % index.name
@@ -181,6 +187,10 @@
         self.mergeTriple (result, self._createIndex_ (index))
 
     # build all constraints
+    for constraint in table.findChildrenOfType ('GSUnique', False, True):
+      if constraint._action == 'add':
+        self.mergeTriple (result, self._createConstraint_ (constraint))
+
     for constraint in table.findChildrenOfType ('GSForeignKey', False, True):
       if constraint._action == 'add':
         self.mergeTriple (result, self._createConstraint_ (constraint))
@@ -403,14 +413,21 @@
       elements [csKey] = None
 
     csName  = self._getSafeName_ (constraint.name, "CONSTRAINT")
-    rfName  = self.shortenName (constraint.references)
-    fields  = constraint.findChildrenOfType ('GSFKField', False, True)
 
-    code = u"ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) " \
-            "REFERENCES %s (%s)" \
-           % (table.name, csName, ", ".join ([f.name for f in fields]),
-              rfName, ", ".join ([f.references for f in fields]))
+    if isinstance (constraint, GSchema.GSForeignKey):
+      rfName  = self.shortenName (constraint.references)
+      fields  = constraint.findChildrenOfType ('GSFKField', False, True)
 
+      code = u"ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) " \
+              "REFERENCES %s (%s)" \
+             % (table.name, csName, ", ".join ([f.name for f in fields]),
+                rfName, ", ".join ([f.references for f in fields]))
+
+    elif isinstance (constraint, GSchema.GSUnique):
+      fields = constraint.findChildrenOfType ('GSUQField')
+      code   = u"ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)" \
+               % (table.name, csName, ", ".join ([f.name for f in fields]))
+
     post.append (code)
 
     return result

Modified: trunk/gnue-common/src/datasources/drivers/sql/interbase/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/interbase/Behavior.py 
2005-06-22 11:29:20 UTC (rev 7638)
+++ trunk/gnue-common/src/datasources/drivers/sql/interbase/Behavior.py 
2005-06-22 13:01:37 UTC (rev 7639)
@@ -259,7 +259,8 @@
     cmd = u"SELECT rdb$relation_name, rdb$constraint_name, " \
            "   rdb$constraint_type, rdb$index_name " \
            "FROM rdb$relation_constraints " \
-           "WHERE rdb$constraint_type IN ('PRIMARY KEY', 'FOREIGN KEY') "
+           "WHERE rdb$constraint_type IN " \
+           "      ('PRIMARY KEY', 'FOREIGN KEY', 'UNIQUE')"
 
     result = {}
     cursor = self.__connection.makecursor (cmd)
@@ -276,7 +277,10 @@
         else:
           cons = table.findChildOfType ('GSConstraints') or \
                            GSchema.GSConstraints (table)
-          item = GSchema.GSForeignKey (cons, name = name)
+          if ctype == 'UNIQUE':
+            item = GSchema.GSUnique (cons, name = name)
+          else:
+            item = GSchema.GSForeignKey (cons, name = name)
 
         result ["%s.%s" % (tname, index)] = item
 
@@ -338,6 +342,11 @@
           if isinstance (constraint, GSchema.GSPrimaryKey):
             for field in fields [iname]:
               GSchema.GSPKField (constraint, name = field)
+
+          elif isinstance (constraint, GSchema.GSUnique):
+            for field in fields [iname]:
+              GSchema.GSUQField (constraint, name = field)
+
           else:
             for (field, reffield) in zip (fields [iname], fields [fkey]):
               GSchema.GSFKField (constraint, name = field,

Modified: trunk/gnue-common/src/datasources/drivers/sql/postgres/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/postgres/Behavior.py  
2005-06-22 11:29:20 UTC (rev 7638)
+++ trunk/gnue-common/src/datasources/drivers/sql/postgres/Behavior.py  
2005-06-22 13:01:37 UTC (rev 7639)
@@ -345,35 +345,56 @@
 
   def __readConstraints (self, tables, fields):
 
-    cmd = u"SELECT conname, conrelid, confrelid, conkey, confkey " \
-           "FROM pg_constraint WHERE contype = 'f'"
+    cmd = u"SELECT conname, conrelid, confrelid, conkey, confkey, contype " \
+           "FROM pg_constraint WHERE contype in ('f', 'u')"
 
     cursor = self.__connection.makecursor (cmd)
     try:
-      for (name, relid, fkrel, key, fkey) in cursor.fetchall ():
+      for (name, relid, fkrel, key, fkey, ctype) in cursor.fetchall ():
         table   = tables.get (relid)
-        fktable = tables.get (fkrel)
 
-        # We need both ends of a relation to be a valid constraint
-        if table is None or fktable is None:
-          continue
+        if ctype == 'f':
+          fktable = tables.get (fkrel)
 
-        parent = table.findChildOfType ('GSConstraints')
-        if parent is None:
-          parent = GSchema.GSConstraints (table)
+          # We need both ends of a relation to be a valid constraint
+          if table is None or fktable is None:
+            continue
 
-        constr = GSchema.GSForeignKey (parent, name = name,
-                                               references = fktable.name)
+          parent = table.findChildOfType ('GSConstraints')
+          if parent is None:
+            parent = GSchema.GSConstraints (table)
 
-        kp  = isinstance (key, basestring) and key [1:-1].split (',') or key
-        fkp = isinstance (fkey, basestring) and fkey [1:-1].split (',') or fkey
+          constr = GSchema.GSForeignKey (parent, name = name,
+                                                 references = fktable.name)
 
-        k = [fields ["%s.%s" % (relid, i)].name for i in kp]
-        f = [fields ["%s.%s" % (fkrel, i)].name for i in fkp]
+          kp  = isinstance (key, basestring) and key [1:-1].split (',') or key
+          fkp = isinstance (fkey, basestring) and fkey [1:-1].split(',') or 
fkey
 
-        for (name, refname) in zip (k, f):
-          GSchema.GSFKField (constr, name = name, references = refname)
+          k = [fields ["%s.%s" % (relid, i)].name for i in kp]
+          f = [fields ["%s.%s" % (fkrel, i)].name for i in fkp]
 
+          for (name, refname) in zip (k, f):
+            GSchema.GSFKField (constr, name = name, references = refname)
+
+        # Unique-Constraint
+        elif ctype == 'u':
+          parent = table.findChildOfType ('GSConstraints') or \
+              GSchema.GSConstraints (table)
+          constr = GSchema.GSUnique (parent, name = name)
+          kp = isinstance (key, basestring) and key [1:-1].split (',') or key
+
+          for name in [fields ["%s.%s" % (relid, i)].name for i in kp]:
+            GSchema.GSUQField (constr, name = name)
+
+          # Ok, since we know PostgreSQL automatically creates a unique index
+          # of the same name, we drop that index since it would only confuse a
+          # later diff
+          for ix in table.findChildrenOfType ('GSIndex', False, True):
+            if ix.name == constr.name:
+              parent = ix.getParent ()
+              parent._children.remove (ix)
+              ix.setParent (None)
+
     finally:
       cursor.close ()
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]