Exemplo n.º 1
0
    def build(self, connection, opts):
        try:
            connection.connect(opts.database)
            tables = connection.tables()
            if opts.table not in tables:
                raise UnknownTableException(opts.table, tables.keys())
            table = tables[opts.table]
            nodes = []
            indent = 0
            if opts.include_driver:
                nodes.append(
                    NameNode(connection.dbms, indent=indent))
                indent += 1
            if opts.include_connection:
                nodes.append(NameNode(str(connection), indent=indent))
                indent += 1
            if opts.include_database and opts.database:
                nodes.append(NameNode(opts.database, indent=indent))
                indent += 1
            nodes.append(NameNode(table.name, indent=indent))
            nodes += bfs(
                table,
                include=to_dict(opts.include),
                exclude=to_dict(opts.exclude),
                indent=indent,
                opts=opts)

            def include_node(item):
                if opts.formatter is GraphvizWriter:
                    if isinstance(item, TableNode):
                        return True
                    if isinstance(item, ColumnNode):
                        return False
                elif opts.include_columns and isinstance(
                        item, ColumnNode):
                    return True
                return not isinstance(item, TableNode)

            return filter(include_node, nodes)
        finally:
            connection.close()
Exemplo n.º 2
0
    def build(self, connection, opts):
        try:
            connection.connect(opts.database)
            tables = connection.tables()
            if opts.table not in tables:
                raise UnknownTableException(opts.table, tables.keys())
            table = tables[opts.table]
            nodes = []
            indent = 0
            if opts.include_driver:
                nodes.append(NameNode(connection.dbms, indent=indent))
                indent += 1
            if opts.include_connection:
                nodes.append(NameNode(str(connection), indent=indent))
                indent += 1
            if opts.include_database and opts.database:
                nodes.append(NameNode(opts.database, indent=indent))
                indent += 1
            nodes.append(NameNode(table.name, indent=indent))
            nodes += bfs(table,
                         include=to_dict(opts.include),
                         exclude=to_dict(opts.exclude),
                         indent=indent,
                         opts=opts)

            def include_node(item):
                if opts.formatter is GraphvizWriter:
                    if isinstance(item, TableNode):
                        return True
                    if isinstance(item, ColumnNode):
                        return False
                elif opts.include_columns and isinstance(item, ColumnNode):
                    return True
                return not isinstance(item, TableNode)

            return filter(include_node, nodes)
        finally:
            connection.close()
Exemplo n.º 3
0
    def test_to_dict(self):
        """Tests the utils.to_dict function"""

        self.assertEqual({}, utils.to_dict(None, {}))
        self.assertEqual({}, utils.to_dict([], {}))

        self.assertEqual(
            {'a': None},
            utils.to_dict(['a.'], {})
        )
        self.assertEqual(
            {'a': None, 'b': None},
            utils.to_dict(['a', 'b'], {})
        )
        self.assertEqual(
            {'a': {'b': None, 'c': None}, 'd': None, 'e': {'f': {'g': None}}},
            utils.to_dict(['a.b', 'a.c', 'd', 'e.f.g'], {})
        )
        self.assertEqual(
            {'a': {'b': '1', 'c': None}, 'd': None, 'e': {'f': {'g': None}}},
            utils.to_dict(['a.b=1', 'a.c', 'd', 'e.f.g'], {})
        )