def drop_table(tname): catalog.init_catalog() catalog.exist_table(tname, False) index.delete_table(tname) catalog.delete_table(tname) record.delete_table(tname) catalog.finalize()
def delete_tuple(tname, condition): catalog.init_catalog() catalog.exist_table(tname, False) clause = extract_condition(condition) length = catalog.get_length(tname) index_name = catalog.get_column_with_index(tname) where = record.delete_record(tname, clause, length) index.delete_entries(where, tname, index_name) catalog.finalize()
def drop_table(tname): '''record.init() catalog.init_catalog() index.init_index()''' catalog.exist_table(tname, False) index.delete_table(tname) catalog.delete_table(tname) record.delete_table(tname) '''index.finalize_index()
def select(table, condition): catalog.init_catalog() catalog.exist_table(table, False) clause = extract_condition(condition) attr_list = catalog.get_column_name(table) length = catalog.get_length(table) index_name = catalog.get_column_with_index(table) where = index.select_from_table(table, clause, index_name) list_res = record.select_record(table, attr_list, clause, where, length) catalog.finalize() return list_res
def insert(tname, values): catalog.init_catalog() catalog.exist_table(tname, False) catalog.check_type(tname, values) index_name = catalog.get_column_with_index(tname) idx = catalog.get_index_of_attribute(tname, index_name) key = [] for dxtemp in idx: key.append(values[dxtemp]) index.insert_entry(tname, index_name, key, values) record.insert(tname, values) catalog.finalize()
def create_table(name, attribute, PK): catalog.init_catalog() catalog.exist_table(name, True) pidx = [x[0] for x in attribute].index(PK) if len(attribute[pidx]) != 5 or attribute[pidx][-1] != 1: raise Exception('Primary key is not a unique attribute!') catalog.create_table(name, attribute, PK) record.create_table(name) index.create_table(name, PK) for x in attribute: if PK not in x and len(x) == 5 and x[-1] == 1: index.create_index(name, 'Uni_' + x[0], x[0]) catalog.finalize()
def select(table, condition): '''record.init() catalog.init_catalog() index.init_index()''' catalog.exist_table(table, False) clauses = [] if len(condition) == 1 and condition[0] == '*': record.select_record(table, catalog.get_column_name(table), clauses, None, catalog.get_length(table)) else: condition = add_space(condition) cnt = 0 tran = condition.split() #print(tran) while (1): if cnt + 3 > len(tran): raise Exception('The query condition is illegal') if tran[cnt + 1] not in ['<', '>', '=', '<>', '>=', '<=']: raise Exception('The operator ' + tran[cnt + 1] + ' in query is illegal') if tran[cnt + 1] == '<>': tran[cnt + 1] = '!=' if tran[cnt + 1] == '=': tran[cnt + 1] = '==' clauses.append([ tran[cnt], tran[cnt + 1], tran[cnt + 2], catalog.get_type_of_attribute(table, tran[cnt]), catalog.get_index_of_attribute(table, tran[cnt]) ]) if cnt + 3 == len(tran): indexname = catalog.get_column_with_index(table) index_clause = None for clause in clauses: if clause[0] in indexname: index_clause = clause break res = None if index_clause != None: clauses.remove(index_clause) indexname = catalog.get_index_name(table, index_clause[0]) res = index.select_from_table(table, index_clause, indexname[0]) record.select_record(table, catalog.get_column_name(table), clauses, res, catalog.get_length(table)) break if tran[cnt + 3] != 'and': raise Exception('and expected but ' + tran[cnt + 3] + ' found.') cnt += 4 '''index.finalize_index()
def delete_tuple(tname, condition): '''record.init() catalog.init_catalog() index.init_index()''' catalog.exist_table(tname, False) clauses = [] if len(condition) == 1 and condition[0] == '*': record.delete_record(tname, clauses, catalog.get_length(tname)) for index_name in catalog.get_index_list(tname): index.delete_table_index(tname, index_name) else: condition = add_space(condition) cnt = 0 tran = condition.split() # print(tran) while (1): if cnt + 3 > len(tran): raise Exception('The query condition is illegal') if tran[cnt + 1] not in ['<', '>', '=', '<>', '>=', '<=']: raise Exception('The operator ' + tran[cnt + 1] + ' in query is illegal') if tran[cnt + 1] == '<>': tran[cnt + 1] = '!=' if tran[cnt + 1] == '=': tran[cnt + 1] = '==' clauses.append([ tran[cnt], tran[cnt + 1], tran[cnt + 2], catalog.get_type_of_attribute(tname, tran[cnt]), catalog.get_index_of_attribute(tname, tran[cnt]) ]) if cnt + 3 == len(tran): indexname = catalog.get_column_with_index(tname) res = record.delete_record(tname, clauses, catalog.get_length(tname)) for cnt, i in enumerate(catalog.get_type_list(tname)): if i != 'char': for j in res: j[cnt] = eval(j[cnt]) for attribute in catalog.get_column_with_index(tname): attr_id = catalog.get_index_of_attribute(tname, attribute) for indexname in catalog.get_index_name_by_seq( tname, attr_id): index.delete_entries([x[attr_id] for x in res], tname, indexname) break if tran[cnt + 3] != 'and': raise Exception('and expected but ' + tran[cnt + 3] + ' found.') cnt += 4 '''index.finalize_index()
def create_table(name, attribute, PK): '''record.init() catalog.init_catalog() index.init_index()''' catalog.exist_table(name, True) pidx = [x[0] for x in attribute].index(PK) if len(attribute[pidx]) != 5 or attribute[pidx][-1] != 1: raise Exception('Primary key is not a unique attribute!') catalog.create_table(name, attribute, PK) record.create_table(name) for x in attribute: if len(x) == 5 and x[-1] == 1: #print(name,x[0]) index.create_table(name, x[0]) catalog.create_index(name, x[0], x[0]) '''index.finalize_index()
def insert(tname, values): '''record.init() catalog.init_catalog() index.init_index()''' catalog.exist_table(tname, False) values = catalog.check_type(tname, values) where = record.insert(tname, values) - catalog.get_length(tname) for idx, key in enumerate(values): if catalog.get_index_name_by_seq(tname, idx) != []: for indexname in catalog.get_index_name_by_seq(tname, idx): try: index.insert_entry(tname, indexname, key, where) except Exception as err: temp_list = catalog.get_index_name_by_seq(tname, idx) for index_del in temp_list[:temp_list.index(indexname)]: index.delete_entries([err], tname, index_del) record.truncate(tname, where) raise Exception('Insertion fails. Data with key: ' + str(key) + ' already exists.') '''index.finalize_index()