def left_join(current_database_name,infos,left_table_name,right_table_name,condition): global data left_table = data.joinpath(current_database_name,left_table_name+".csv") right_table = data.joinpath(current_database_name,right_table_name+".csv") if (not left_table.exists()) or (not right_table.exists()): print("Error : Unknown table") return left_reader = csv.DictReader(left_table.open("r",newline=""),delimiter="|") right_reader = csv.DictReader(right_table.open("r",newline=""),delimiter="|") is_single_condition = re.compile(r"([\w\s\.]+)=([\w\s\.]+)",re.IGNORECASE).match(condition) if not is_single_condition: print("ERROR!") return left = is_single_condition.group(1).strip() right = is_single_condition.group(2).strip() try: tableA,parmA = left.split(".") tableB,parmB = right.split(".") if tableA == left_table_name: parm_left = parmA parm_right = parmB else: parm_left = parmB parm_right = parmA # infos left_info = [] right_info = [] infos = infos.split(",") for info in infos: table,parm = info.split(".") if table == left_table_name: left_info.append(parm) else: right_info.append(parm) left_info = ",".join(left_info) right_info = ",".join(right_info) except: left_info = "*" right_info = "*" l = [] for i in left_reader: l.append(i) r = [] for j in right_reader: r.append(j) for left_row in l: print_infos(left_info,left_row) for right_row in r: if left_row[parm_left] == right_row[parm_right]: print_infos(right_info,right_row) print("OPERATOR SUCCESS")
def select_from_group_by_having(current_database_name, infos, table_name, parms, condition): global data table = data.joinpath(current_database_name, table_name + ".csv") if not table.exists(): print("ERROR : Unknown table '%s.%s'" % (current_database_name, table_name)) return parms = parms.split(",") # 所有不同的种类 cate = [] with open(table, "r", newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter="|") for item in reader: tup = tuple(item[parm] for parm in parms) if tup not in cate: cate.append(tup) # 对每一类,用一个列表装起来 l = [[] for i in range(len(cate))] with open(table, "r", newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter="|") for item in reader: for i in range(len(cate)): if tuple(item[parm] for parm in parms) == cate[i]: l[i].append(item) for i in l: if check_item_condition(i[0], condition): print_infos(infos, i[0]) print("OPERATOR SUCCESS")
def select_from_limit(current_database_name, infos, table_name, limit): global data table = data.joinpath(current_database_name, table_name + ".csv") if not table.exists(): print("ERROR : Unknown table '%s.%s'" % (current_database_name, table_name)) return if len(limit.split(",")) == 1: limit_offset = 0 limit_count = int(limit) else: limit_offset = int(limit.split(",")[0]) limit_count = int(limit.split(",")[1]) with open(table, "r", newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter="|") for i in range(limit_offset): next(reader) for i in range(limit_count): try: item = next(reader) print_infos(infos, item) except: break print("OPERATOR SUCCESS")
def select_from_order_by(current_database_name, infos, table_name, order_key): global data table = data.joinpath(current_database_name, table_name + ".csv") if not table.exists(): print("ERROR : Unknown table '%s.%s'" % (current_database_name, table_name)) return def order_by_key(item): return item[order_key] l = None with open(table, "r", newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter="|") l = sorted(reader, key=order_by_key) for item in l: print_infos(infos, item) print("OPERATOR SUCCESS")
def select_from_where_order_by(current_database_name, infos, table_name, condition, order_key): global data table = data.joinpath(current_database_name, table_name + ".csv") if not table.exists(): print("ERROR : Unknown table '%s.%s'" % (current_database_name, table_name)) return def order_by_key(item): return item[order_key] l = [] with open(table, "r", newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter="|") for i in reader: if check_item_condition(i, condition): l.append(i) l.sort(key=order_by_key) for i in l: print_infos(infos, i) print("OPERATOR SUCCESS")
def select_from(current_database_name, infos, table_name, condition): global data table = data.joinpath(current_database_name, table_name + ".csv") if not table.exists(): print("ERROR : Unknown table '%s.%s'" % (current_database_name, table_name)) return # print(condition) # and or between and in # id between 1 and 3 is_between_and = re.compile(r"(\w+) between (\w+) and (\w+)", re.IGNORECASE).match(condition) # id = 1 is_single_condition = re.compile(r"([\w\s]+)([=<>]+)([\w\s]+)", re.IGNORECASE).match(condition) if is_between_and: parm = is_between_and.group(1) begin = int(is_between_and.group(2)) end = int(is_between_and.group(3)) # print(begin) # print(end) firstrow = None with open(table, "r", newline="") as csvfile: reader = csv.reader(csvfile, delimiter="|") firstrow = next(reader) if parm not in firstrow: print("Error parm!") return reader = csv.DictReader(table.open("r", newline=""), delimiter="|") read_list = [] for i in reader: read_list.append(i) for i in read_list: # print(i) if int(i[parm]) <= end and int(i[parm]) > begin: print_infos(infos, i) if is_single_condition: left = is_single_condition.group(1).strip() right = is_single_condition.group(3).strip() operator = is_single_condition.group(2).strip() firstrow = None with open(table, "r", newline="") as csvfile: reader = csv.reader(csvfile, delimiter="|") firstrow = next(reader) if left not in firstrow: print("Error parm!") return reader = csv.DictReader(table.open("r", newline=""), delimiter="|") for item in reader: if operator == "=" and item[left] == right: print_infos(infos, item) elif operator == ">" and item[left] > right: print_infos(infos, item) elif operator == "<" and item[left] < right: print_infos(infos, item) elif operator == ">=" and item[left] >= right: print_infos(infos, item) elif operator == "<=" and item[left] <= right: print_infos(infos, item) else: pass print("OPERATOR SUCCESS")