Commits

t2y  committed 4caeb28 Merge

merged from original

  • Participants
  • Parent commits f615c46, 0271fbc

Comments (0)

Files changed (3)

File src/schema2rst/metadata.py

 class MySQLTable:
     def __init__(self, meta):
         self.meta = meta
+        self.keys = []
 
     def reflect(self, engine):
         self.engine = engine
 
         schema_name = os.path.basename(str(self.engine.url))
+
+        query = """SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
+                   FROM information_schema.table_constraints
+                   WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'""" % \
+                   (schema_name, self.name)
+        rs = self.engine.execute(query)
+        for r in rs.fetchall():
+            key = MySQLConstraint(r[0], self.meta, r[1])
+            key.reflect(self.engine)
+            self.keys.append(key)
+
         query = """SELECT TABLE_COMMENT
                    FROM information_schema.Tables
                    WHERE TABLE_SCHEMA = '%s' AND
     @property
     def columns(self):
         for column in self.meta.columns:
-            column = MySQLColumn(column)
+            column = MySQLColumn(self, column)
             column.reflect(self.engine)
             yield column
 
+    def refkey(self, column):
+        keys = [key for key in self.keys \
+                if column in key.columns and key.type == 'FOREIGN KEY']
+        if keys:
+            return keys[0]
+        else:
+            return None
+
+
+class MySQLConstraint:
+    def __init__(self, name, table, type):
+        self.name = name
+        self.table = table
+        self.type = type
+        self.columns = []
+        self.references = []
+
+    def reflect(self, engine):
+        self.engine = engine
+
+        schema_name = os.path.basename(str(self.engine.url))
+
+        query = """SELECT COLUMN_NAME, REFERENCED_TABLE_NAME,
+                          REFERENCED_COLUMN_NAME
+                   FROM information_schema.key_column_usage
+                   WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s' AND
+                         CONSTRAINT_NAME = '%s'""" % \
+                   (schema_name, self.table.name, self.name)
+        rs = self.engine.execute(query)
+        meta = self.table.metadata
+        for r in rs.fetchall():
+            self.columns.append(self.table.c[r[0]])
+            if r[1]:
+                reftable = meta.tables[r[1]]
+                self.references.append(reftable.c[r[2]])
+
 
 class MySQLColumn:
-    def __init__(self, meta):
+    def __init__(self, table, meta):
+        self.table = table
         self.meta = meta
 
     def reflect(self, engine):
             options.append(self._collation_name)
         if self._extra:
             options.append(self._extra)
+        if self.table.refkey(self.meta):
+            key = self.table.refkey(self.meta)
+            mesg = "Refer: %s" % key.references[0]
+            options.append(mesg)
 
         if self._comment and options:
             return "%s (%s)" % (self.comment, ", ".join(options))

File src/schema2rst/schema2rst.py

         sphinx.listtable(m.headers)
         for c in table.columns:
             sphinx.listtable_column(m.columns(c))
+        if table.keys:
+            sphinx.header(u'Keys', '^')
+            for key in table.keys:
+                if key.type != 'FOREIGN KEY':
+                    keyinfo = "%s (%s): " % (key.name, key.type)
+                    keyinfo += ", ".join(c.name for c in key.columns)
+
+                    sphinx.list_item(keyinfo)
+
 
 if __name__ == '__main__':
     main()

File src/schema2rst/sphinx.py

                 self.out("   * - %s" % column)
             else:
                 self.out("     - %s" % column)
+
+    def list_item(self, item):
+        self.out("* %s" % item)