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))
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."
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."
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))
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.")
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 as e: print("Error: %s." % e) # print traceback.format_exc() return table.write_back() print("%d line(s) are updated." % updated_lines)
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)