示例#1
0
文件: main.py 项目: tyh24689/SQLolita
def execute_alert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    if node.op == "ADD":
        if node.attr_list.attr_name in names:
            print "Error: The attr's name already exists."
            return
        data_dict.dict[node.table_name] += [node.attr_list]
        for idx in range(len(data)): data[idx].append("NULL")
    elif node.op == "DROP":
        attr_name = node.attr_list[0]
        if attr_name not in names:
            print "Error: The attr's name does not exist."
            return
        old_list = data_dict.dict[node.table_name]
        data_dict.dict[node.table_name] = [attr for attr in old_list if attr.attr_name != attr_name]
        idx_remove = names.index(attr_name)
        for idx in range(len(data)): del data[idx][idx_remove]
        index_dict.drop_index(node.table_name, attr_name)
    table.data_list = data
    table.write_back()
    data_dict.write_back()
    print "Alert table successful."
示例#2
0
def execute_insert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    table = TableFile(data_dict, node.table_name, node.value_list)
    if not table.insert(index_dict):
        print "Error: Types are not matched or index duplicated"
        return
    index_dict.load_index()
示例#3
0
文件: main.py 项目: tyh24689/SQLolita
def execute_insert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    table = TableFile(data_dict, node.table_name, node.value_list)
    if not table.insert(index_dict):
        print "Error: Types are not matched or index duplicated"
        return
    index_dict.load_index()
示例#4
0
def execute_create_table(node):
    # if not __check_power(node.type, [node.table_name]):
    #     return
    # 如果该表已经存在,则返回None
    if __check_table([node.table_name]):
        print("Error: This table already exists.")
        return

    data_dict = DataDict(node.table_name)
    data_dict.dict[node.table_name] = node.attr_list
    data_dict.write_back()
    table = TableFile(data_dict, node.table_name)
    table.init_table()
示例#5
0
def execute_insert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    table = TableFile(data_dict, node.table_name, node.value_list)
    print("node.table_name: ", node.table_name)
    if not table.insert(index_dict):
        print("Error: Types are not matched or Key error or index duplicated")
        return
    index_dict.load_index()  # 加载索引
示例#6
0
文件: main.py 项目: tyh24689/SQLolita
def execute_delete(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    old_len = len(data)
    try:
        table.data_list = [line for line in data if not check_where(node.where_list, names, line)]
    except Exception, e:
        print "Error: %s." % e
        # print traceback.format_exc()
        return
示例#7
0
def execute_print_table(node):
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    data = TableFile(data_dict, node.table_name).load_data()
    print_table(names, data)
示例#8
0
def execute_print_table(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    names = data_dict.table_attr_names()
    data = TableFile(data_dict, node.table_name).load_data()
    print_table(names, data)
示例#9
0
文件: main.py 项目: tyh24689/SQLolita
def execute_update(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    updated_lines = 0
    try:
        for idx in range(len(data)):
            if check_where(node.where_list, names, data[idx]):
                updated_lines += 1
                set_value(data[idx], names, node.set_list)
    except Exception, e:
        print "Error: %s." % e
        # print traceback.format_exc()
        return
示例#10
0
def execute_update(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    updated_lines = 0
    try:
        for idx in range(len(data)):
            if check_where(node.where_list, names, data[idx]):
                updated_lines += 1
                set_value(data[idx], names, node.set_list)
    except Exception, e:
        print "Error: %s." % e
        # print traceback.format_exc()
        return
示例#11
0
def execute_delete(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    old_len = len(data)
    try:
        table.data_list = [
            line for line in data
            if not check_where(node.where_list, names, line)
        ]
    except Exception, e:
        print "Error: %s." % e
        # print traceback.format_exc()
        return
示例#12
0
def execute_alert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    names = data_dict.table_attr_names()
    table = TableFile(data_dict, node.table_name)
    table_data = table.load_data()
    if node.op == "ADD":
        if node.attr_list.attr_name in names:
            print("Error: The attr's name already exists.")
            return
        data_dict.dict[node.table_name] += [node.attr_list]
        for line in table_data:
            line.append("NULL")
        # for idx in range(len(table_data)): table_data[idx].append("NULL")
    elif node.op == "DROP":
        attr_name = node.attr_list[0]
        # for it in node.attr_list:
        #     print(it, type(it))
        if attr_name not in names:
            print("Error: The attr's name does not exist.")
            return
        old_list = data_dict.dict[node.table_name]
        data_dict.dict[node.table_name] = [
            attr for attr in old_list if attr.attr_name != attr_name
        ]
        index_remove = names.index(attr_name)
        for line in table_data:
            line.pop(index_remove)
        # 当某个属性drop是,如果其有索引,则也要相应的drop
        index_dict.drop_index(node.table_name, attr_name)
    # print("table.data_list: ")
    # for it in table.data_list:
    #     print(it)
    # table.data_list = table_data
    table.write_back()
    data_dict.write_back()
    print("Alert table successful.")
示例#13
0
def execute_select(node):
    if not __check_power(node.type, node.from_list):
        return
    __dur()
    for table_name in node.from_list:
        if not data_dict.has_table(table_name):
            print "Error: The table '%s' does not exist." % table_name
            return
    part_name = []
    full_name = []
    table_data = []
    for table_name in node.from_list:
        names = data_dict.table_attr_names(table_name)
        part_name += names
        full_name += [table_name + '.' + attr_name for attr_name in names]
        table_data += [TableFile(data_dict, table_name).load_data()]

    name_dict = {}
    for idx in range(len(full_name)):
        name_dict[full_name[idx]] = idx
        name_dict[part_name[idx]] = idx

    if node.select_list[0] == "*":
        node.select_list = full_name
    try:
        select_col_nums = [
            name_dict[str(attr_name)] for attr_name in node.select_list
        ]
        if node.where_list and len(
                node.from_list) == 2 and __can_use_index_joint(
                    node.from_list[0], node.where_list):
            # print "This query used index."
            res = query.joint_by_index(table_data, node.from_list[0],
                                       node.where_list.left.attr_name,
                                       index_dict)
        else:
            res = query.joint(table_data)
        if node.where_list and __can_use_index_select(node.from_list[0],
                                                      node.where_list):
            # print "This query used index."
            val = node.where_list.right.value
            num = index_dict.query(node.from_list[0],
                                   node.where_list.left.attr_name, [val])[0]
            res = [res[num]] if num is not None else []
        res = [
            line for line in res
            if check_where(node.where_list, part_name, line, full_name)
        ]
        res = query.projection(res, select_col_nums)
        print_table(node.select_list, res)
        __dur("Select")
    except Exception, e:
        print "Error: %s." % e
示例#14
0
def execute_delete(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    names = data_dict.table_attr_names()
    table = TableFile(data_dict, node.table_name)
    table_data = table.load_data()
    old_len = len(table_data)
    print("where_list: ", node.where_list, type(node.where_list))
    # for where_item in node.where_list:
    #     print("where_item", where_item)
    try:
        table.data_list = []
        for line in table_data:
            is_true = check_where(node.where_list, names, line)
            print("is_true: ", is_true) if VISIBLE is True else None
            if not is_true:
                table.data_list.append(line)
    except Exception as ex:
        print("Error: %s." % ex)
        return
    new_len = len(table.data_list)
    table.write_back()
    index_dict.load_index()  # 表中记录删除后,索引文件也要相应的发生改变
    print("%d line(s) are deleted." % (old_len - new_len))
示例#15
0
def execute_alert(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    if node.op == "ADD":
        if node.attr_list.attr_name in names:
            print "Error: The attr's name already exists."
            return
        data_dict.dict[node.table_name] += [node.attr_list]
        for idx in range(len(data)):
            data[idx].append("NULL")
    elif node.op == "DROP":
        attr_name = node.attr_list[0]
        if attr_name not in names:
            print "Error: The attr's name does not exist."
            return
        old_list = data_dict.dict[node.table_name]
        data_dict.dict[node.table_name] = [
            attr for attr in old_list if attr.attr_name != attr_name
        ]
        idx_remove = names.index(attr_name)
        for idx in range(len(data)):
            del data[idx][idx_remove]
        index_dict.drop_index(node.table_name, attr_name)
    table.data_list = data
    table.write_back()
    data_dict.write_back()
    print "Alert table successful."
示例#16
0
def execute_update(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    names = data_dict.table_attr_names()
    table = TableFile(data_dict, node.table_name)
    table_data = table.load_data()
    update_line = 0
    try:
        for line in table_data:
            # 如果line符合where的条件,则返回True
            if check_where(node.where_list, names, line):
                set_value(line, names, node.set_list)
                print("node.where_list: ", node.where_list)
                update_line += 1
    except Exception as ex:
        print("Error: %s." % ex)
        return
    table.write_back()
    index_dict.load_index()  # 表中记录更新后,索引文件也要相应的发生改变
    print("%d line(s) are updated." % update_line)
示例#17
0
def execute_create_index(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print "Error: The table does not exist."
        return
    attr_names = data_dict.table_attr_names(node.table_name)
    if node.attr_name not in attr_names:
        print "Error: The table_attr does not exist."
        return
    if index_dict.has_index(node.table_name, node.attr_name):
        print "Error: The index already exist."
        return
    data = TableFile(data_dict, node.table_name).load_data()
    index_dict.create_index(node.table_name, node.attr_name, attr_names, data)
    index_dict.write_back()
示例#18
0
    def load_index(self):
        self.index_dict = {}
        data_dict = DataDict(DATA_DICT_PATH)
        f = open(self.file_path, "r")
        attr_names = None
        table_name = None
        table_data = None
        for line in f.readlines():
            items = line.split()
            if len(items) < 1: continue

            if items[0][0] == '[':
                table_name = items[0][1:-1]
                attr_names = data_dict.table_attr_names(table_name)
                table_data = TableFile(data_dict, table_name).load_data()
            else:
                self.create_index(table_name, items[0], attr_names, table_data)
示例#19
0
def execute_create_index(node):
    if not __check_power(node.type, [node.table_name]):
        return
    print("node.table_name: ", node.table_name)
    if not __check_table([node.table_name]):
        print("Error: This table isn't exists.")
        return
    data_dict = DataDict(node.table_name)
    attr_names = data_dict.table_attr_names()
    if node.attr_name not in attr_names:
        print("Error: The table_attr does not exist.")
        return
    if index_dict.has_index(node.table_name, node.attr_name):
        print("Error: The index already exist.")
        return
    data = TableFile(data_dict, node.table_name).load_data()
    index_dict.create_index(node.table_name, node.attr_name, attr_names, data)
    index_dict.write_back()
示例#20
0
    def load_index(self):
        self.index_dict = {}
        # data_dict = DataDict(self.table_name)
        f = open(self.file_path, "r")
        attr_names = None
        table_name = None
        table_data = None
        lines = f.readlines()
        # print("f.readlines: ", lines)
        for line in lines:
            # print("line: ", line)
            items = line.split()
            if len(items) < 1:
                continue

            if items[0][0] == '[':
                table_name = items[0][1:-1]
                # print("table_name: ", table_name)
                data_dict = DataDict(table_name)
                attr_names = data_dict.table_attr_names()
                table_data = TableFile(data_dict, table_name).load_data()
            else:
                self.create_index(table_name, items[0], attr_names, table_data)
示例#21
0
def execute_delete(node):
    if not __check_power(node.type, [node.table_name]):
        return
    if not data_dict.has_table(node.table_name):
        print("Error: The table does not exist.")
        return
    names = data_dict.table_attr_names(node.table_name)
    table = TableFile(data_dict, node.table_name)
    data = table.load_data()
    old_len = len(data)
    try:
        table.data_list = [line for line in data if not check_where(node.where_list, names, line)]
    except Exception as e:
        print("Error: %s." % e)
        # print traceback.format_exc()
        return
    new_len = len(table.data_list)
    table.write_back()
    index_dict.load_index()
    print("%d line(s) are deleted." % (old_len - new_len))
示例#22
0
def execute_select(node):
    if not __check_power(node.type, node.from_list):
        return
    if not __check_table(node.from_list):
        print("Error: This table isn't exists.")
        return
    __dur()
    print("\n" + "node.select_list: ") if VISIBLE is True else None
    for it in node.select_list:
        print(it, end=", ") if VISIBLE is True else None
    print("\n" + "node.from_list: ") if VISIBLE is True else None
    for it in node.from_list:
        print(it, end=", ") if VISIBLE is True else None
    print("\n" + "node.where_list: ") if VISIBLE is True else None
    print(node.where_list) if VISIBLE is True else None
    part_name = []  # 只存属性名
    full_name = [
    ]  # 表名和属性名都存,格式: [table_name.attr_name1, table_name.attr_name2,...]
    # table_data = []
    table_datas = dict()  # 表的数据
    for table_name in node.from_list:
        data_dict = DataDict(table_name)
        names = data_dict.table_attr_names()
        part_name += names
        full_name += [table_name + '.' + attr_name for attr_name in names]
        # table_data += [TableFile(data_dict, table_name).load_data()]

    name_dict = {}
    for idx in range(len(full_name)):
        name_dict[full_name[idx]] = idx
        name_dict[part_name[idx]] = idx

    if node.select_list[0] == "*":
        node.select_list = full_name
    try:
        # 先进行选择
        if len(node.from_list) >= 2 and node.where_list:
            node_value = __get_where_value([node.where_list])
            if len(node_value) > 0:
                for it in node_value:
                    # print("node.left.attr_name: ", it.left.table_name)
                    table_name = str(it.left.table_name)
                    table_datas[table_name] = []
                    data_dict = DataDict(table_name)
                    names = data_dict.table_attr_names()
                    data = TableFile(data_dict, table_name).load_data()
                    for line in data:
                        if check_where(it, names, line):
                            table_datas[table_name].append(line)

        for it in node.from_list:
            if it in table_datas.keys():
                continue
            data_dict = DataDict(it)
            data = TableFile(data_dict, it).load_data()
            table_datas[it] = data
            # print("data: ", data)

        table_data = list(table_datas.values())
        # print("table_data; ", table_data)
        # 找出要投影属性的位置
        select_col_nums = [
            name_dict[str(attr_name)] for attr_name in node.select_list
        ]
        # 判断是否可以用索引来做连接,两个表的
        if node.where_list and len(node.from_list) == 2 and \
                __can_use_index_joint(node.from_list[0], node.where_list):
            print("This query used index 1.")
            res = query.joint_by_index(table_data, node.from_list[0],
                                       node.where_list.left.attr_name,
                                       index_dict)
            if len(res) == 0:
                res = query.joint(table_data)
        else:
            # 直接做笛卡尔积,返回做完笛卡尔积的结果
            res = query.joint(table_data)

        # 判断是否可以利用索引来做选择,对于第一个表
        if node.where_list and __can_use_index_select(node.from_list[0],
                                                      node.where_list):
            print("This query used index 2.")
            val = node.where_list.right.value
            num = index_dict.query(node.from_list[0],
                                   node.where_list.left.attr_name, [val])[0]
            res = [res[num]] if num is not None else []

        selected_data = []
        for line in res:

            # print(node.where_list, "\n", part_name, "\n", line, "\n", full_name)
            if check_where(node.where_list, part_name, line, full_name):
                selected_data.append(line)
        projection_data = query.projection(selected_data, select_col_nums)
        print_table(node.select_list, projection_data)

        __dur("Select")
    # except ZeroDivisionError:
    #     print("Error.")
    except Exception as ex:
        print("Error: %s." % ex)