Exemplo n.º 1
0
    def test_add_segment_updates_dependencies(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node3 = SqlCTENode("test3", "Fill me!")
        node.add_dependency_node(node1)
        node.add_dependency_node(node3)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)
        oldGraph.add_node(node3)

        newNode = PlaceholderNode("test",
                                  "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1",
                                   "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertTrue(
            newNode2 in oldGraph.get_node_by_name("test").get_dependencies())
        self.assertTrue(
            node3 in oldGraph.get_node_by_name("test").get_dependencies())
Exemplo n.º 2
0
    def split_into_cte_queries(self):

        tokens = sqlparse.parse(self.text)[0].tokens
        if not any([x.ttype is sqlparse.tokens.Keyword and "with" in x.value for x in tokens]):
            self.nodes.append(SqlCTENode(self.query_name, self.text))
            return
        ii = 0
        while ii < len(tokens):
            if type(tokens[ii]) == sqlparse.sql.IdentifierList:
                ctes = tokens[ii].get_identifiers()
                for cte in ctes:
                    cte_name = cte.value
                    cte_text = str(cte).replace(cte.value + " as", "").strip()[1:-1]
                    self.nodes.append(SqlCTENode(cte_name, cte_text))
                break

            if type(tokens[ii]) == type(tokens[ii]) == sqlparse.sql.Identifier:
                cte_name = tokens[ii].value
                cte_text = str(tokens[ii]).replace(tokens[ii].value + " as", "").strip()[1:-1]
                self.nodes.append(SqlCTENode(cte_name, cte_text))
                break

            if tokens[ii].ttype is sqlparse.tokens.DML:
                raise
            ii += 1

        final_query_text = "".join([str(x) for x in tokens[ii + 1:]])
        self.nodes.append(SqlCTENode(self.query_name, final_query_text))
Exemplo n.º 3
0
    def test_add_replace_segment_updates_dependencies(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node.add_dependency_node(node1)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)

        newNode = PlaceholderNode("test",
                                  "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1",
                                   "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertEqual(
            oldGraph.get_node_by_name("test").get_dependencies(), [newNode2])
Exemplo n.º 4
0
    def test_add_replace_segment_adds_new_node(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node.add_dependency_node(node1)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)

        newNode = PlaceholderNode("test",
                                  "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1",
                                   "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertTrue(oldGraph.get_node_by_name("test2") is newNode2)
Exemplo n.º 5
0
    def test_add_with_replacement_replaces_proper_nodes(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        oldGraph.add_node(node)
        newNode = SqlCTENode("test", "Im the text content of the test node")
        newGraph = QueryGraph()
        newGraph.add_node(newNode)

        oldGraph.add_query(newGraph, replace=True)

        self.assertTrue(newNode is oldGraph.node_lookup["test"])
Exemplo n.º 6
0
    def test_value_replace_updates_text(self):
        oldGraph = QueryGraph()
        oldGraph.add_node(
            SqlCTENode("test", "Im the text content of the test node"))

        newGraph = QueryGraph()
        newGraph.add_node(SqlCTENode("test", "im the new content"))

        oldGraph.value_replace(newGraph)

        self.assertEqual(
            oldGraph.get_node_by_name("test").get_text(), "im the new content")
Exemplo n.º 7
0
    def test_value_replace_doesnt_change_structure(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        oldGraph.add_node(node)
        newNode = SqlCTENode("test2", "Im the text content of the test node")
        newGraph = QueryGraph()
        newGraph.add_node(newNode)

        oldGraph.value_replace(newGraph)

        self.assertTrue(node is oldGraph.node_lookup["test"])
        self.assertEqual(len(oldGraph.node_lookup), 1)
Exemplo n.º 8
0
    def test_node_list_contains_all_nodes(self):
        graph = QueryGraph()
        node = SqlCTENode("test", "test")
        node1 = SqlCTENode("test1", "test")
        node2 = SqlCTENode("test2", "test")
        node3 = SqlCTENode("test3", "test")
        graph.add_node(node)
        graph.add_node(node1)
        graph.add_node(node2)
        graph.add_node(node3)

        for n in [node, node2, node3, node1]:
            self.assertEquals([n], graph.get_ordered_node_list_from_node(n))
Exemplo n.º 9
0
    def _parse_node_contents(self, node: SqlCTENode, node_list: List[SqlCTENode]):
        state = "start"
        tokens = sqlparse.parse(node.get_text())[0].tokens
        tokens = [x for x in tokens if not (x.ttype is sqlparse.tokens.Whitespace)]
        for token in tokens:

            if type(token) is sqlparse.sql.Comment:
                node.set_docstring(str(token).replace("/*", "").replace("*/", "").replace("--", "").strip())

            if token.ttype is sqlparse.tokens.DML:
                state = "select"
                continue

            if state == "select" and token.ttype is not sqlparse.tokens.Punctuation:
                if token.value.lower() == "from":
                    state = "from"
                    continue

                if type(token) is sqlparse.sql.IdentifierList:
                    for item in token.get_identifiers():
                        node.add_column(self.column_factory.create_column(item))
                    continue
                if token.ttype is not sqlparse.tokens.Punctuation and token.ttype is not sqlparse.tokens.Whitespace and str(token.value) != "\n":
                    column = self.column_factory.create_column(str(token))
                    print("Here")
                    node.add_column(column)

            if state == "from":

                if type(token) is sqlparse.sql.Where or token.value.lower() == "group":
                    break

                if type(token) is sqlparse.sql.Identifier:
                    tableName = ""
                    if token.has_alias():
                        tableName = str(token).replace(token.get_name(), "").strip()
                    else:
                        tableName = token.get_name()

                    dependency = [x for x in node_list if x.get_name() == tableName]
                    if len(dependency) == 1:
                        node.add_dependency_node(dependency[0])

        if node.get_dependencies() == []:
            node = create_table_node(node)
        node_list.append(node)
        return node_list
Exemplo n.º 10
0
    def test_creates_new_versions_of_nodes(self):
        node = SqlCTENode("test", "TEST")

        factory = QueryGraphFactory()
        query_graph = factory.create_graph_from_node_list([node])
        new_query_graph = factory.create_runnable_graph_from_node(node)

        self.assertFalse(
            query_graph.get_node_by_name("test") is
            new_query_graph.get_node_by_name("test"))
Exemplo n.º 11
0
    def test_node_list_contains_all_nodes(self):
        graph = QueryGraph()
        node = SqlCTENode("test", "test")
        node1 = SqlCTENode("test1", "test")
        node2 = SqlCTENode("test2", "test")
        node3 = SqlCTENode("test3", "test")

        node.add_dependency_node(node1)
        node1.add_dependency_node(node2)
        node2.add_dependency_node(node3)
        graph.add_node(node)
        graph.add_node(node1)
        graph.add_node(node2)
        graph.add_node(node3)

        self.assertEquals([node3, node2, node1, node], graph.get_ordered_node_list_from_node(node))
        self.assertEquals([node3, node2, node1], graph.get_ordered_node_list_from_node(node1))
Exemplo n.º 12
0
    def test_add_doesnt_replace_placeholder_nodes(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        oldGraph.add_node(node)
        newNode = PlaceholderNode("test",
                                  "Im the text content of the test node")
        newGraph = QueryGraph()
        newGraph.add_node(newNode)

        oldGraph.add_query(newGraph)

        self.assertTrue(node is oldGraph.node_lookup["test"])
Exemplo n.º 13
0
    def test_add_segment_updates_dependencies(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node3 = SqlCTENode("test3", "Fill me!")
        node.add_dependency_node(node1)
        node.add_dependency_node(node3)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)
        oldGraph.add_node(node3)

        newNode = PlaceholderNode("test", "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1", "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertTrue(newNode2 in oldGraph.get_node_by_name("test").get_dependencies())
        self.assertTrue(node3 in oldGraph.get_node_by_name("test").get_dependencies())
Exemplo n.º 14
0
    def test_value_replace_updates_docstring(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node.set_docstring("im a doc string")
        oldGraph.add_node(node)
        newNode = SqlCTENode("test", "Im the text content of the test node")
        newNode.set_docstring("im a new doc string")
        newGraph = QueryGraph()
        newGraph.add_node(newNode)

        oldGraph.value_replace(newGraph)

        self.assertEqual(
            oldGraph.get_node_by_name("test").get_docstring(),
            "im a new doc string")
Exemplo n.º 15
0
    def test_truncated_placeholder_keeps_dependencies(self):

        graph = QueryGraph()

        node = SqlCTENode("test", "test")
        node1 = SqlCTENode("test1", "test")
        node2 = SqlCTENode("test2", "test")
        node3 = SqlCTENode("test3", "test")

        node.add_dependency_node(node1)
        node1.add_dependency_node(node2)
        node2.add_dependency_node(node3)
        graph.add_node(node)
        graph.add_node(node1)
        graph.add_node(node2)
        graph.add_node(node3)

        factory = QueryGraphFactory()
        testGraph = factory.create_truncated_runnable_graph_from_node(node, ["test2"])

        self.assertEqual(type(testGraph.get_node_by_name("test2")), PlaceholderNode)
        self.assertEqual(testGraph.get_node_by_name("test2").get_dependencies(), node2.get_dependencies())
        self.assertEqual(len(testGraph.node_lookup), 3)
Exemplo n.º 16
0
    def test_value_replace_updates_docstring(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node.set_docstring("im a doc string")
        oldGraph.add_node(node)
        newNode = SqlCTENode("test", "Im the text content of the test node")
        newNode.set_docstring("im a new doc string")
        newGraph = QueryGraph()
        newGraph.add_node(newNode)

        oldGraph.value_replace(newGraph)

        self.assertEqual(oldGraph.get_node_by_name("test").get_docstring(),"im a new doc string")
Exemplo n.º 17
0
    def test_add_replace_segment_adds_new_node(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node.add_dependency_node(node1)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)

        newNode = PlaceholderNode("test", "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1", "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertTrue(oldGraph.get_node_by_name("test2") is newNode2)
Exemplo n.º 18
0
    def test_add_replace_segment_updates_dependencies(self):
        oldGraph = QueryGraph()
        node = SqlCTENode("test", "Im the text content of the test node")
        node1 = SqlCTENode("test1", "Im the text content of the test node")
        node.add_dependency_node(node1)
        oldGraph.add_node(node)
        oldGraph.add_node(node1)

        newNode = PlaceholderNode("test", "Im the text content of the test node")
        newNode2 = SqlCTENode("test2", "Im the text content of the test node")
        newNode1 = PlaceholderNode("test1", "Im the text content of the test node")
        newNode.add_dependency_node(newNode2)
        newNode2.add_dependency_node(newNode1)

        newGraph = QueryGraph()

        newGraph.add_node(newNode)
        newGraph.add_node(newNode2)
        newGraph.add_node(newNode1)

        oldGraph.add_query(newGraph)

        self.assertEqual(oldGraph.get_node_by_name("test").get_dependencies(), [newNode2])
Exemplo n.º 19
0
def create_table_node(from_node : SqlCTENode):
    new_node = SqlTableNode(from_node.get_name(), from_node.get_text())
    new_node.columns = from_node.columns
    new_node.doc_string = from_node.doc_string
    return new_node
Exemplo n.º 20
0
    def test_truncated_placeholder_keeps_dependencies(self):

        graph = QueryGraph()

        node = SqlCTENode("test", "test")
        node1 = SqlCTENode("test1", "test")
        node2 = SqlCTENode("test2", "test")
        node3 = SqlCTENode("test3", "test")

        node.add_dependency_node(node1)
        node1.add_dependency_node(node2)
        node2.add_dependency_node(node3)
        graph.add_node(node)
        graph.add_node(node1)
        graph.add_node(node2)
        graph.add_node(node3)

        factory = QueryGraphFactory()
        testGraph = factory.create_truncated_runnable_graph_from_node(
            node, ["test2"])

        self.assertEqual(type(testGraph.get_node_by_name("test2")),
                         PlaceholderNode)
        self.assertEqual(
            testGraph.get_node_by_name("test2").get_dependencies(),
            node2.get_dependencies())
        self.assertEqual(len(testGraph.node_lookup), 3)
Exemplo n.º 21
0
    def test_node_list_contains_all_nodes(self):
        graph = QueryGraph()
        node = SqlCTENode("test", "test")
        node1 = SqlCTENode("test1", "test")
        node2 = SqlCTENode("test2", "test")
        node3 = SqlCTENode("test3", "test")

        node.add_dependency_node(node1)
        node1.add_dependency_node(node2)
        node2.add_dependency_node(node3)
        graph.add_node(node)
        graph.add_node(node1)
        graph.add_node(node2)
        graph.add_node(node3)

        self.assertEquals([node3, node2, node1, node],
                          graph.get_ordered_node_list_from_node(node))
        self.assertEquals([node3, node2, node1],
                          graph.get_ordered_node_list_from_node(node1))
Exemplo n.º 22
0
 def test_add_node(self):
     graph = QueryGraph()
     node = SqlCTENode("test", "test")
     graph.add_node(node)
     self.assertEqual(graph.get_node_by_name("test"), node)