def delete_table_from_db(table_name): db_data = read_from_json("db_files/db.json") num_of_files = db_data[table_name]['num of files'] del (db_data[table_name]) db_data['num of tables'] -= 1 write_to_json("db_files/db.json", db_data) return num_of_files
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: if os.path.isfile(f"db_files/{table_name}_1.json"): raise ValueError if key_field_name not in [field.name for field in fields]: raise ValueError db_data = read_from_json("db_files/db.json") if table_name in db_data.keys(): raise ValueError write_to_json(f"db_files/{table_name}_1.json", {}) db_data['num of tables'] += 1 db_data[table_name] = \ {'num of files': 1, 'num of lines': 0, 'fields': convert_from_DBfield_to_dict(fields), 'primary key': key_field_name, 'indexes': [] } write_to_json("db_files/db.json", db_data) table = DBTable(table_name, fields, key_field_name) table.create_index(key_field_name) return table
def insert_record(self, values: Dict[str, Any]) -> None: db_data = read_from_json("db_files/db.json") flag = None primary_key = db_data[self.name]['primary key'] try: flag = self.get_record(values[primary_key]) except: db_data = read_from_json("db_files/db.json") primary_key = get_primary_key(db_data, self.name) record_to_insert = { values[primary_key]: {k: str(v) for k, v in values.items() if k != primary_key} } if end_place_in_file( db_data, self.name) and db_data[self.name]['num of lines'] != 0: db_data[self.name]['num of files'] += 1 write_to_json( f"db_files/{self.name}_{db_data[self.name]['num of files']}.json", record_to_insert) else: add_to_json( f"db_files/{self.name}_{db_data[self.name]['num of files']}.json", record_to_insert) if flag is not None: raise ValueError db_data[self.name]['num of lines'] += 1 write_to_json("db_files/db.json", db_data)
def update_record(self, key: Any, values: Dict[str, Any]) -> None: db_data = read_from_json("db_files/db.json") num_of_files = db_data[self.name]['num of files'] for file_num in range(num_of_files): file_data = read_from_json( f"db_files/{self.name}_{file_num + 1}.json") for k in file_data.keys(): if k == str(key): for field in values.keys(): file_data[k][field] = values[field] write_to_json(f"db_files/{self.name}_{file_num + 1}.json", file_data) return raise ValueError
def delete_record(self, key: Any) -> None: db_data = read_from_json("db_files/db.json") num_of_files = db_data[self.name]['num of files'] flag = False for file_num in range(num_of_files): file_data = read_from_json( f"db_files/{self.name}_{file_num + 1}.json") for k in file_data.keys(): if k == str(key): flag = True del_key = k break if flag: del file_data[del_key] write_to_json(f"db_files/{self.name}_{file_num + 1}.json", file_data) if not flag: raise ValueError db_data[self.name]['num of lines'] -= 1 write_to_json("db_files/db.json", db_data)
def update_data(table_name, num, file_data, file_num): db_data = read_from_json("db_files/db.json") db_data[table_name]['num of lines'] -= num write_to_json(f"db_files/{table_name}_{file_num + 1}.json", file_data) write_to_json("db_files/db.json", db_data)
def __init__(self): if not os.path.isfile("db_files/db.json"): write_to_json("db_files/db.json", {'num of tables': 0})