Commits

Takeshi Komiya  committed f4ab4b1

Add SimpleInspector#dump()

  • Participants
  • Parent commits 38570e1

Comments (0)

Files changed (3)

File src/schema2rst/commands/graph.py

     config = yaml.load(io.open(args[0], encoding='utf-8'))
     engine = inspectors.create_engine(config)
     try:
-        inspector = inspectors.create_for(engine)
+        schema = inspectors.create_for(engine).dump()
 
         doc = RestructuredTextWriter(options.output)
-        generate_doc(doc, inspector, config)
+        generate_doc(doc, schema)
     finally:
         engine.dispose()
 
 
-def generate_doc(doc, inspector, config):
-    doc.header('Schema: %s' % config['db'])
+def generate_doc(doc, schema):
+    doc.header('Schema: %s' % schema['name'])
 
     doc.println(".. graphviz::")
     doc.println("")
     doc.println("   digraph {")
     doc.println("      node [shape = box];")
 
-    for table in inspector.get_tables():
+    for table in schema['tables']:
         if table['fullname']:
             doc.println('      %s [label="%s\\n(%s)"];' %
                         (table['name'], table['name'], table['fullname']))
         else:
             doc.println('      %s;' % table['name'])
 
-        for key in inspector.get_foreign_keys(table['name']):
+        for key in table['foreign_keys']:
             doc.println('      %s -> %s;' %
                         (table['name'], key['referred_table']))
 

File src/schema2rst/commands/rst.py

     config = yaml.load(io.open(args[0], encoding='utf-8'))
     engine = inspectors.create_engine(config)
     try:
-        inspector = inspectors.create_for(engine)
+        schema = inspectors.create_for(engine).dump()
 
         doc = RestructuredTextWriter(options.output)
-        generate_doc(doc, inspector, config)
+        generate_doc(doc, schema)
     finally:
         engine.dispose()
 
 
-def generate_doc(doc, inspector, config):
-    doc.header('Schema: %s' % config['db'])
+def generate_doc(doc, schema):
+    doc.header('Schema: %s' % schema['name'])
 
-    for table in inspector.get_tables():
+    for table in schema['tables']:
         # FIXME: support fullname (table comment)
         if table['fullname']:
             doc.header("%s (%s)" %
                    'PKey', 'Default', 'Comment']
         doc.listtable(headers)
 
-        for c in inspector.get_columns(table['name']):
+        for c in table['columns']:
             columns = [c.get('fullname'), c.get('name'), c.get('type'),
                        (not c.get('nullable')), c.get('primary_key'),
                        c.get('default'), c.get('comment')]
             doc.listtable_column(columns)
 
-        indexes = inspector.get_indexes(table['name'])
-        if indexes:
+        if table['indexes']:
             doc.header('Keys', '^')
-            for index in indexes:
+            for index in table['indexes']:
                 if index['unique']:
                     format = "UNIQUE KEY: %s (%s)"
                 else:

File src/schema2rst/inspectors/base.py

                 column['primary_key'] = False
 
         return columns
+
+    def dump(self):
+        ret = dict(name=self.engine.url.database, tables=[])
+        for table in self.get_tables():
+            table_name = table['name']
+
+            table['columns'] = []
+            for column in self.get_columns(table_name):
+                metadata = dict(fullname=column['fullname'],
+                                name=column['name'],
+                                type=column['type'],
+                                nullable=column['nullable'],
+                                primary_key=column['primary_key'],
+                                default=column['default'],
+                                comment=column['comment'])
+                table['columns'].append(metadata)
+
+            table['indexes'] = []
+            for index in self.get_indexes(table_name):
+                metadata = dict(name=index['name'],
+                                unique=index['unique'],
+                                column_names=index['column_names'])
+                table['indexes'].append(metadata)
+
+            table['foreign_keys'] = []
+            for fkey in self.get_foreign_keys(table_name):
+                metadata = dict(referred_table=fkey['referred_table'])
+                table['foreign_keys'].append(metadata)
+
+            ret['tables'].append(table)
+
+        return ret