def delete(database, table, columns): if type(database) != str or type(table) != str or type(columns) != list: return 1 checkData() if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) if len(PKsTree.search(columns)): try: PKsTree.delete(columns) serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return 0 except: return 1 else: return 4 else: return 1
def loadCSV(filepath, database, table): if type(database) != str or type(table) != str or type(filepath) != str: return [] checkData() if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return [] tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return [] try: res = [] import csv PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) with open(filepath, 'r') as file: reader = csv.reader(file, delimiter=',') for row in reader: res.append(PKsTree.register(row)) serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return res except: return [] else: return []
def alterDropPK(database: str, table: str) -> int: try: if type(database) != str or type(table) != str: return 1 checkData() if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) res = PKsTree.DeletePk() if res: return res else: serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return 0 # exito else: return 1 except: return 1
def alterAddPK(self, database, table, columns): if self.searchDB(database): if self.searchTB(database, table): try: columns[0] = columns[0] range = True for i in columns: if range: if i >= self.dicDB[database][table][1]: range = False if range: if not self.dicDB[database][table][2]: if self.verifyPk(database, table, columns): self.dicDB[database][table][2] = columns self.updateTree(database, table) serializar.commit( self.dicDB[database][table][0], database + "-" + table + "-B") return 0 return 4 return 4 return 5 except: return 1 return 3 return 2
def alterDropColumn(database: str, table: str, columnNumber: int) -> int: try: checkData() # Get the databases tree dataBaseTree = serializable.Read('./Data/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente else: tuplaTree = serializable.Read(f"./Data/{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/{database}/{table}/", table, tuplaTree) return 0 else: return 2 # database no existente except: return 1
def alterAddPK(database: str, table: str, columns: list) -> int: try: checkData() # Get the databases tree dataBaseTree = serializable.Read('./Data/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente else: tuplaTree = serializable.Read(f"./Data/{database}/{table}/", table) maximun = max(columns) minimun = min(columns) numberColumnsA = tuplaTree.columns # actual amount from column if not (minimun >= 0 and maximun < numberColumnsA): return 5 try: res = tuplaTree.CreatePK(columns) except: return 1 if res: return res else: serializable.update(f"./Data/{database}/{table}/", table, tuplaTree) return 0 else: return 2 # database no existente except: return 1
def alterTable(database: str, tableOld: str, tableNew: str) -> int: if type(database) != str or type(tableOld) != str or type(tableNew) != str: return 1 checkData() if validateIdentifier(tableOld) and validateIdentifier(tableNew): dataBaseTree = serializable.Read('./Data/', "Databases") databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) rootT = tablesTree.getRoot() if not tablesTree.search(rootT, tableOld.upper()): return 3 #tableOLD no existente elif tablesTree.search(rootT, tableNew.upper()): return 4 #tableNEW existente tablesTree.delete(rootT, tableOld.upper()) serializable.Rename(f'./Data/{database}/', tableOld, tableNew) tablesTree.add(rootT, tableNew.upper()) serializable.update(f"./Data/{database}/", database, tablesTree) return 0 else: return 2 #db no existente else: return 1
def alterAddColumn(database: str, table: str, default: any) -> int: try: if type(database) != str or type(table) != str: return 1 checkData() if validateIdentifier(database) and validateIdentifier(table): # Get the databases tree dataBaseTree = serializable.Read('./Data/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente else: tuplaTree = serializable.Read( f"./Data/{database}/{table}/", table) res = tuplaTree.addColumn(default) if res: return res else: serializable.update(f"./Data/{database}/{table}/", table, tuplaTree) return 0 else: return 2 # database no existente else: return 1 except: return 1
def showTables(database): checkData() dataBaseTree = serializable.Read('./Data/', "Databases") if dataBaseTree.search(dataBaseTree.getRoot(), database.upper()): db = serializable.Read(f"./Data/{database}/", database) dbKeys = db.postOrder(db.getRoot()) return [] if len(dbKeys) == 0 else dbKeys[:-1].split("-") else: return None
def alterAddColumn(database: str, table: str, default: any) -> int: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.alterAddColumn(database, table, default) b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") return r
def alterDropPK(database: str, table: str) -> int: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.alterDropPK(database, table) b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") return r
def extractRow(database, table, columns): checkData() dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return [] # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return [] # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) return PKsTree.search(columns) # exito
def alterAddColumn(self, database, table, default): if self.searchDB(database): if self.searchTB(database, table): try: self.dicDB[database][table][1] += 1 self.dicDB[database][table][0].agregarValor(default) serializar.commit(self.dicDB[database][table][0], database + "-" + table + "-B") return 0 except: return 1 return 3 return 2
def dropDatabase(database): checkData() if validateIdentifier(database): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 dataBaseTree.delete(root, database.upper()) serializable.delete('./Data/' + database) serializable.update('./Data/', 'Databases', dataBaseTree) return 0 else: return 1
def extractRangeTable(database, table, columnNumber, lower, upper): if type(database) != str or type(table) != str or type( columnNumber) != int: return None checkData() if validateIdentifier(database) and validateIdentifier(table): # Get the databases tree dataBaseTree = serializable.Read('./Data/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) if tablesTree.search(tablesTree.getRoot(), table.upper()): table = serializable.Read(f'./Data/{database}/{table}/', table) tableList = list(table.lista().values()) validList = [] if columnNumber < 0 or columnNumber >= table.columns: return None try: for i in tableList: if type(i[columnNumber]) == str: if str(i[columnNumber]) <= str(upper) and str( i[columnNumber]) >= str(lower): validList.append(i) elif type(i[columnNumber]) == float: if float( i[columnNumber]) <= float(upper) and float( i[columnNumber]) >= float(lower): validList.append(i) elif type(i[columnNumber]) == int: if int(i[columnNumber]) <= int(upper) and int( i[columnNumber]) >= int(lower): validList.append(i) elif type(i[columnNumber]) == bool: if bool(i[columnNumber]) <= bool(upper) and bool( i[columnNumber]) >= bool(lower): validList.append(i) except: return None return validList else: return None else: return None else: return None
def createDatabase(database): checkData() if database and validateIdentifier(database): dataBaseTree = serializable.Read('./Data/', 'Databases') root = dataBaseTree.getRoot() if dataBaseTree.search(root, database.upper()): return 2 else: dataBaseTree.add(root, database.upper()) serializable.write('./Data/', database, AVLTree.AVLTree()) serializable.update('./Data/', 'Databases', dataBaseTree) return 0 else: return 1
def delete(database: str, table: str, columns: list) -> int: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.delete(database, table, columns) try: serializar.commit(b.dicDB[database][table][0], database + "-" + table + "-B") b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") except: pass return r
def insert(database: str, table: str, register: list) -> int: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.insert(database, table, register) try: serializar.commit(b.dicDB[database][table][0], database + "-" + table + "-B") b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") except: pass return r
def truncate(database: str, table: str) -> int: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.truncate(database, table) try: serializar.commit(b.dicDB[database][table][0], database + "-" + table + "-B") b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") except: pass return r
def loadCSV(file: str, database: str, table: str) -> list: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass r = b.loadCSV(file, database, table) try: serializar.commit(b.dicDB[database][table][0], database + "-" + table + "-B") b.dicDB[database][table][0] = arbolB(b.grade) serializar.commit(b, "BDD") except: pass return r
def alterDatabase(dataBaseOld, dataBaseNew) -> int: checkData() if validateIdentifier(dataBaseOld) and validateIdentifier(dataBaseNew): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, dataBaseOld.upper()): return 2 if dataBaseTree.search(root, dataBaseNew.upper()): return 3 dataBaseTree.delete(root, dataBaseOld.upper()) serializable.Rename('./Data/', dataBaseOld, dataBaseNew) dataBaseTree.add(root, dataBaseNew.upper()) serializable.update('./Data/', 'Databases', dataBaseTree) return 0 else: return 1
def extractTable(database: str, table: str) -> list: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass return b.extractTable(database, table)
def extractRow(database, table, columns): if type(database) != str or type(table) != str or type(columns) != list: return [] checkData() if validateIdentifier(database) and validateIdentifier(table): dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return [] # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return [] # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) return PKsTree.search(columns) # exito else: return []
def extractRangeTable(database: str, table: str, columnNumber: int, lower: any, upper: any) -> list: try: b.dicDB[database][table][0] = serializar.rollback(database + "-" + table + "-B") except: pass return b.extractRangeTable(database, table, columnNumber, lower, upper)
def extractTable(database, table): checkData() # Get the databases tree dataBaseTree = serializable.Read('./Data/', "Databases") # Get the dbNode databaseNode = dataBaseTree.search(dataBaseTree.getRoot(), database.upper()) # If DB exist if databaseNode: tablesTree = serializable.Read(f"./Data/{database}/", database) if tablesTree.search(tablesTree.getRoot(), table.upper()): table = serializable.Read(f'./Data/{database}/{table}/', table) return list(table.lista().values()) else: return None else: return None
def truncate(database, table): checkData() dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) try: PKsTree.truncate() serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return 0 except: return 1
def insert(database, table, register): checkData() dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) if PKsTree.buscar(register): return 4 res = PKsTree.register(register) if res: return res serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return 0 # exito
def loadCSV(filepath, database, table): checkData() dataBaseTree = serializable.Read('./Data/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return [] tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return [] try: res = [] import csv with open(filepath, 'r') as file: reader = csv.reader(file, delimiter=',') for row in reader: res.append(insert(database, table, row)) return res except: return []
def update(self, database, table, register, columns): if self.searchDB(database): if self.searchTB(database, table): try: columns[0] = columns[0] if self.dicDB[database][table][2] != None: pk = "" for i in columns: pk += str(i) + "_" pk = pk[:-1] if pk in self.dicDB[database][table][0].Keys(): tupla = self.extractRow( database, table, columns) self.dicDB[database][table][0]._del(pk) for key, value in register.items(): tupla[key] = value self.insert(database, table, tupla) serializar.commit( self.dicDB[database][table][0], database + "-" + table + "-B") return 0 return 4 else: pk = "" for i in columns: pk += str(i) + "_" pk = pk[:-1] if pk in self.dicDB[database][table][0].Keys(): tupla = self.extractRow( database, table, columns) self.dicDB[database][table][0]._del(pk) for key, value in register.items(): tupla[key] = value self.insert(database, table, tupla) serializar.commit( self.dicDB[database][table][0], database + "-" + table + "-B") return 0 return 4 except: return 1 return 3 return 2
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/', "Databases") root = dataBaseTree.getRoot() if not dataBaseTree.search(root, database.upper()): return 2 # database no existente else: tablesTree = serializable.Read(f"./Data/{database}/", database) if not tablesTree.search(tablesTree.getRoot(), table.upper()): return 3 # table no existente PKsTree = serializable.Read(f'./Data/{database}/{table}/', table) res = PKsTree.register(register) if res: return res serializable.update(f'./Data/{database}/{table}/', table, PKsTree) return 0 # exito else: return 1