コード例 #1
0
def optimize(syntax_tree, sql):
    if syntax_tree.operator == 'SELECT':
        condition = syntax_tree.condition
        sql = condition.split('&')
        relation = []
        for i in range(len(sql)):
            if search(sql[i]) is not None:
                relation.append(search(sql[i]))
        syntax_tree = optimize(syntax_tree.left_child, sql)
    elif syntax_tree.operator == 'PROJECTION':
        syntax_tree.left_child = optimize(syntax_tree.left_child, sql)
    elif syntax_tree.operator == 'JOIN':
        first_tree = SyntaxTree()
        first_tree.operator = 'SELECT'
        first_tree.condition = sql[0]
        first_tree.left_child = syntax_tree.left_child
        syntax_tree.left_child = first_tree
        if len(sql) == 1:
            return syntax_tree
        second_tree = SyntaxTree()
        second_tree.operator = 'SELECT'
        second_tree.condition = sql[1]
        second_tree.right_child = syntax_tree.right_child
        syntax_tree.right_child = second_tree
    return syntax_tree
コード例 #2
0
def parse(sql_statement):
    sql = sql_statement.split()
    execute_tree = SyntaxTree()
    index = 0
    while True:
        if index >= len(sql):
            break
        #处理一元运算符
        elif sql[index] == 'SELECT' or sql[index] == 'PROJECTION':
            execute_tree.operator = sql[index]
            index += 2  # 从[开始到]里面的全部记录下来
            condition = ''
            while sql[index] != ']':
                condition += sql[index]
                condition += ' '
                index += 1
            index += 1
            execute_tree.condition = condition
        #处理二元运算符
        elif sql[index] == 'JOIN':
            # 连接操作需要创建子树,所以分开写
            execute_tree.operator = sql[index]
            execute_tree.left_child = SyntaxTree()
            execute_tree.left_child.attribute = sql[index - 1]
            execute_tree.right_child = SyntaxTree()
            execute_tree.right_child.attribute = sql[index + 1]
            index += 1
        #处理子查询,进行递归
        elif sql[index] == '(':
            index += 1
            statement = ''
            while index < len(sql) and sql[index] != ')':
                statement += sql[index]
                statement += ' '
                index += 1
            index += 1
            execute_tree.left_child = parse(statement)
        else:
            index += 1

    return execute_tree
コード例 #3
0
def parse(sql_statement):
    sql = sql_statement.split()
    execute_tree = SyntaxTree()

    index = 0
    while True:
        if index >= len(sql):
            break
        elif sql[index] == 'SELECT' or sql[index] == 'PROJECTION':
            execute_tree.operator = sql[index]
            index += 2  # 从[开始到]里面的全部记录下来
            condition = ''
            while sql[index] != ']':
                condition += sql[index]
                condition += ' '
                index += 1
            index += 1
            execute_tree.condition = condition
        elif sql[index] == 'JOIN':
            # 连接操作需要创建子树,所以分开写
            execute_tree.operator = sql[index]
            execute_tree.left_child = SyntaxTree()
            execute_tree.left_child.attribute = sql[index - 1]
            execute_tree.right_child = SyntaxTree()
            execute_tree.right_child.attribute = sql[index + 1]
            index += 1
        elif sql[index] == '(':
            # 每次遇到这个说明需要创建一棵子树,由于题目中的查询都是只有一个分支,所以可以直接进入下一个子树中
            index += 1
            statement = ''
            while index < len(sql) and sql[index] != ')':
                statement += sql[index]
                statement += ' '
                index += 1
            index += 1
            execute_tree.left_child = parse(statement)
        else:
            index += 1

    return execute_tree