def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: print("=== create_table ===") with open(DB_ROOT / "DataBase.json", "r") as DB_file: db_data = json.load(DB_file) # if table_name in db_data.keys(): # raise FileExistsError("The table is exists") with open(DB_ROOT / f"{table_name}.csv", "w") as table_file: table = csv.writer(table_file) table.writerow([f.name for f in fields]) db_data[table_name] = { "fields": [{ f.name: str(f.type) } for f in fields], "key": key_field_name } # db_data["num_tables"] += 1 with open(DB_ROOT / "DataBase.json", "w") as DB_file: # db_data = json.load(DB_file) json.dump(db_data, DB_file) return DBTable(table_name, fields, key_field_name)
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: if not os.path.isdir(f"{DB_ROOT}/{table_name}"): try: os.makedirs(f"{DB_ROOT}/{table_name}") new_table = DBTable(table_name, fields, key_field_name) with open(f"{DB_ROOT}/{table_name}/{table_name}.json", "w") as tables: json.dump( { "len": 0, "name": table_name, "fields": [{ field.name: str(field.type) } for field in fields], "key_field_name": key_field_name }, tables) except ValueError: self.delete_table(table_name) raise ValueError self.my_tables.update({table_name: new_table}) return new_table raise FileExistsError
def __init__(self): with shelve.open(os.path.join(db_api.DB_ROOT, "DB.db"), writeback=True) as db: for key in db: db_table = DBTable(key, db[key][0], db[key][1]) db_table.list_index = db[key][2] DataBase.__dict_tables__[str(key)] = db_table
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: new_table = DBTable(table_name, fields, key_field_name) self.db_table[table_name] = new_table return new_table
def get_table(self, table_name: str) -> DBTable: print("=== get_table ===") with open(DB_ROOT / "DataBase.json", "r+") as DB_file: db_data = json.load(DB_file) # fields_list = [DBField(f.key(), f[f.key()]) for f in db_data[table_name]["fields"]] fields_list = [] for dict_ in db_data[table_name]["fields"]: print(dict_) fields_list.append(dict_) return DBTable(table_name, fields_list, db_data[table_name]["key"])
def get_table(self, table_name: str) -> DBTable: if table_name in self.my_tables.keys(): return self.my_tables[table_name] if os.path.isdir(f"{DB_ROOT}/{table_name}"): with open(f"{DB_ROOT}/{table_name}/{table_name}.json") as tables: table_data = json.load(tables) new_table = DBTable(table_data["name"], table_data["filed"], table_data["key_field_name"]) self.my_tables[table_name] = new_table return new_table raise NameError
def get_table(self, table_name: str) -> DBTable: if table_name in self.my_tables.keys(): return self.my_tables[table_name] if os.path.isdir(f"{DB_ROOT}/{table_name}"): with open(f"{DB_ROOT}/{table_name}/{table_name}.json") as tables: table_data = json.load(tables) new_table = DBTable(table_data["name"], [DBField(list(item.keys())[0], list(item.values())[0]) for item in table_data["fields"]], table_data["key_field_name"]) self.my_tables[table_name] = new_table return new_table raise NameError
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: self.num_of_tables += 1 self.tables_array.append(DBTable(table_name, fields, key_field_name)) with open(f"{table_name}.csv", "w") as new_file: csv_writer = csv.writer(new_file) data_in_line = [] for field in fields: data_in_line.append(field.name) csv_writer.writerow(data_in_line) return self.tables_array[self.num_of_tables - 1]
def get_table(self, table_name: str) -> DBTable: print("=== get_table ===") with open(DB_ROOT / "DataBase.json", "r+") as DB_file: db_data = json.load(DB_file) print(db_data.keys()) if table_name not in db_data.keys(): raise FileNotFoundError fields_list = [] for dic in db_data[table_name]["fields"]: fields_list.append(dic) return DBTable(table_name, fields_list, db_data[table_name]["key"])
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: dictionary = read_tables() if table_name in dictionary: raise TableExistError with open(DB_ROOT / 'tables.json', 'w+') as tables_file: db_table = DBTable(table_name, fields, key_field_name) create_table_file(db_table) dictionary[table_name] = { 'fields': [field.name for field in fields], 'key field name': key_field_name } self.num += 1 json.dump(dictionary, tables_file) return db_table
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: if not os.path.isdir(f"{DB_ROOT}/{table_name}"): os.makedirs(f"{DB_ROOT}/{table_name}") new_table = DBTable(table_name, fields, key_field_name) with open(f"{DB_ROOT}/{table_name}/{table_name}.json", "w") as tables: json.dump( { "name": table_name, # "filed": [{field.name: field.type} for field in fields], "key_field_name": key_field_name }, tables) self.my_tables.update({table_name: new_table}) return new_table raise FileExistsError
def create_table(self, table_name: str, fields: List[DBField], key_field_name: str) -> DBTable: if DataBase.__dict_tables__.get(table_name): return DataBase.__dict_tables__[table_name] flag = 0 for field in fields: if key_field_name == field.name: flag = 1 if flag == 0: raise ValueError data_table = shelve.open(os.path.join(db_api.DB_ROOT, table_name + ".db"), writeback=True) data_table.close() db_table = DBTable(table_name, fields, key_field_name) db_table.list_index = [] DataBase.__dict_tables__[table_name] = db_table with shelve.open(os.path.join(db_api.DB_ROOT, "DB.db"), writeback=True) as db: db[table_name] = [fields, key_field_name, list()] return db_table
def get_table(self, table_name: str) -> DBTable: dictionary = read_tables() return DBTable(table_name, dictionary[table_name]['fields'], dictionary[table_name]['key field name'])