def dropTable(database: str, table: str) -> int: try: if type(database) != str or type(table) != str: return 1 checkData() # Get the databases tree dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) root = tablesTree.getRoot() if not tablesTree.search(root, table): return 3 #table no existente else: tablesTree.delete(root, table) serializable.delete(f"./Data/BPlusMode/{database}/{table}") serializable.update(f"./Data/BPlusMode/{database}/", database, tablesTree) return 0 else: return 2 except: return 1
def createTable(database, table, numberColumns): if type(database) != str or type(table) != str or type( numberColumns) != int: return 1 # Validates identifier before searching if validateIdentifier(database) and validateIdentifier( table) and numberColumns > 0: checkData() # Get the databases tree dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) if tablesTree.search(tablesTree.getRoot(), table): return 3 else: # Creates new table node tablesTree.add(tablesTree.getRoot(), table) serializable.update(f"./Data/BPlusMode/{database}/", database, tablesTree) # Creates bin file for the new table serializable.write(f"./Data/BPlusMode/{database}/", table, BplusTree.BPlusTree(5, numberColumns)) return 0 else: return 2 else: return 1
def alterDropColumn(database: str, table: str, columnNumber: int) -> int: try: if type(database) != str or type(table) != str or type( columnNumber) != int: return 1 checkData() if validateIdentifier(database) and validateIdentifier(table): # Get the databases tree dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table): return 3 # table no existente else: tuplaTree = serializable.Read( f"./Data/BPlusMode/{database}/{table}/", table) if columnNumber < 0 or columnNumber >= tuplaTree.columns: return 5 #out of limit else: res = tuplaTree.dropColumn(columnNumber) if res: return res else: serializable.update( f"./Data/BPlusMode/{database}/{table}/", table, tuplaTree) return 0 else: return 2 # database no existente else: return 1 except: return 1
def alterAddPK(database: str, table: str, columns: list) -> int: try: if type(database) != str or type(table) != str or type( columns) != list: return 1 checkData() # Get the databases tree if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table): return 3 # table no existente else: tuplaTree = serializable.Read( f"./Data/BPlusMode/{database}/{table}/", table) try: res = tuplaTree.CreatePK(columns) except: return 1 if res: return res else: serializable.update( f"./Data/BPlusMode/{database}/{table}/", table, tuplaTree) return 0 else: return 2 # database no else: return 1 except: return 1
def extractTable(database, table): if type(database) != str or type(table) != str: return None checkData() if validateIdentifier(database) and validateIdentifier(table): # Get the databases tree dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) if tablesTree.search(tablesTree.getRoot(), table): table = serializable.Read( f'./Data/BPlusMode/{database}/{table}/', table) return list(table.lista().values()) else: return None else: return None else: return None
def insert(database, table, register): if type(database) != str or type(table) != str or type(register) != list: return 1 checkData() if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/BPlusMode/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table): return 3 # table no existente PKsTree = serializable.Read( f'./Data/BPlusMode/{database}/{table}/', table) res = PKsTree.register(register) if res: return res serializable.update(f'./Data/BPlusMode/{database}/{table}/', table, PKsTree) return 0 # exito else: return 1
def showDatabases(): checkData() dataBaseTree = serializable.Read('./Data/BPlusMode/', "Databases") root = dataBaseTree.getRoot() dbKeys = dataBaseTree.postOrder(root) return [] if len(dbKeys) == 0 else dbKeys[:-1].split("-")