def extractRow(self, name: str, columns: list, noLine, noColumn) -> list: """ Method to extract a record from a table :param name: The name of table :param columns: List with number of columns of primary keys :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None if not TypeChecker().searchTable(database, name): desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return None data = data_mode(database.mode).extractRow(database.name.lower(), name.lower(), columns) if data == None: ErrorController().add(34, 'Execution', '', noLine, noColumn) return None return data
def extractTable(self, name: str, noLine, noColumn) -> list: """ Method to get a list of records from a table :param name: The name of table :param noLine: The instruction line :param noColumn: The instruction column :return: Returns a list of records """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None if not TypeChecker().searchTable(database, name): desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return None # TODO Revisar Esto didier, que con lower me da problema al buscar tablas y base de datos data = data_mode(database.mode).extractTable(database.name.lower(), name.lower()) if data == None: ErrorController().add(34, 'Execution', '', noLine, noColumn) return None return data
def deleteDatabase(self, name: str, line, column): """ Method to remove a database in type checker :param name: The name of the database :param line: The instruction line :param column: The instruction column :return: Returns nothing """ database = self.searchDatabase(name) if not database: desc = f": Database {name} does not exist" ErrorController().add(35, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).dropDatabase(name.lower()) if dbStatement == 0: self._typeCheckerList.remove(database) self.writeFile() SymbolTable().delete(database) DataWindow().consoleText( 'Query returned successfully: Database deleted') elif dbStatement == 1: desc = f": Can't drop database {name}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {name} does not exist" ErrorController().add(35, 'Execution', desc, line, column)
def createDatabase(self, database: Database, line, column): """ Method to create a database in type checker :param database: Database object :param line: The instruction line :param column: The instruction column :return: Returns nothing """ if self.searchDatabase(database.name): desc = f": Database {database.name} already exists" ErrorController().add(30, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).createDatabase( database.name.lower()) if dbStatement == 0: self._typeCheckerList.append(database) self.writeFile() SymbolTable().add(database, 'New Database', 'Database', 'Global', None, line, column) DataWindow().consoleText( 'Query returned successfully: Database created') elif dbStatement == 1: desc = f": Can't create database {database.name}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {database.name} already exists" ErrorController().add(30, 'Execution', desc, line, column)
def showDatabases(self) -> list: """ Method to show all databases :return: Returns list of databases """ databases = [] for i in range(1, 7): mode = data_mode(i) if mode: databases.extend(mode.showDatabases()) return databases
def alterAddPK(self, name: str, columns: list, noLine, noColumn): """ Method to define primary keys to a database :param name: The name of table :param columns: List with number of columns :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return table = TypeChecker().searchTable(database, name) if not table: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return dbStatement = data_mode(database.mode).alterAddPK( database.name.lower(), name.lower(), columns) if dbStatement == 0: for col in table.columns: if col.number in columns: col.primaryKey = True DataWindow().consoleText( 'Query returned successfully: Alter Table add PK') elif dbStatement == 1: ErrorController().add(34, 'Execution', '', noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) elif dbStatement == 4: desc = f": Multiple primary keys for table {database.name} are not allowed" ErrorController().add(36, 'Execution', desc, noLine, noColumn) elif dbStatement == 5: desc = f": Column of relation {name} does not exist" ErrorController().add(26, 'Execution', desc, noLine, noColumn)
def createColumnTable(self, table: Table, column: Column, noLine, noColumn): """ Method to create a column in table :param table: The name of table :param column: Number of columns :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return if self.searchColumn(table, column.name): desc = f": Column {column.name} already exists" ErrorController().add(29, 'Execution', desc, noLine, noColumn) return dbStatement = data_mode(database.mode).alterAddColumn( database.name.lower(), table.name.lower(), column.default) if dbStatement == 0: if len(table.columns) > 0: column.number = table.columns[-1].number + 1 table.columns.append(column) self.writeFile() DataWindow().consoleText( 'Query returned successfully: Table updated') return True elif dbStatement == 1: desc = f": Can't update table {table.name}" ErrorController().add(34, 'Execution', desc, noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {table.name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn)
def deleteColumn(self, table: Table, column: Column, noLine, noColumn): """ Method to remove a column in table :param table: The name of table :param column: Number of columns :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return dbStatement = data_mode(database.mode).alterDropColumn( database.name.lower(), table.name.lower(), column.number) if dbStatement == 0: if column: table.remove(column) self.updateColumnIndex(table) self.writeFile() DataWindow().consoleText( 'Query returned successfully: Column deleted') return desc = f": Column {column.name} does not exist" ErrorController().add(26, 'Execution', desc, noLine, noColumn) elif dbStatement == 1: desc = f": Can't update Table {table.name}" ErrorController().add(34, 'Execution', desc, noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {table.name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) elif dbStatement == 4: desc = f": Column of relation {column.name} does not exist" ErrorController().add(26, 'Execution', desc, noLine, noColumn)
def update(self, name: str, register: dict, columns: list, noLine, noColumn): """ Method to update a record from a table :param name: The name of table :param register: Dictionary where the key is the column number and the value the data :param columns: List with number of columns of primary keys :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None if not TypeChecker().searchTable(database, name): desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return None dbStatement = data_mode(database.mode).update(database.name.lower(), name.lower(), register, columns) if dbStatement == 0: DataWindow().consoleText('Query returned successfully: UPDATE') elif dbStatement == 1: ErrorController().add(34, 'Execution', '', noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) elif dbStatement == 4: desc = f": Primary key does not exist" ErrorController().add(34, 'Execution', desc, noLine, noColumn)
def updateTable(self, tableOld: str, tableNew: str, line, column): """ Method to update the name of a table in database :param tableOld: The old name of the table :param tableNew: The new name of the table :param line: The instruction line :param column: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).alterTable( database.name.lower(), tableOld.lower(), tableNew.lower()) if dbStatement == 0: table = self.searchTable(database, tableOld) table.name = tableNew self.writeFile() DataWindow().consoleText( 'Query returned successfully: Table updated') elif dbStatement == 1: desc = f": Can't update Table {tableOld}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, line, column) elif dbStatement == 3: desc = f": Table {tableOld} does not exist" ErrorController().add(27, 'Execution', desc, line, column) elif dbStatement == 4: desc = f": Table {tableNew} already exists" ErrorController().add(31, 'Execution', desc, line, column)
def insert(self, name: str, data: list, noLine, noColumn): """ Method to insert data to a column :param name: The name of table :param data: Data list :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None dbStatement = data_mode(database.mode).insert(database.name.lower(), name.lower(), data) if dbStatement == 0: DataWindow().consoleText( 'Query returned successfully: INSERT INTO') elif dbStatement == 1: ErrorController().add(34, 'Execution', '', noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) elif dbStatement == 4: desc = f": Duplicate key value violates unique" ErrorController().add(24, 'Execution', desc, noLine, noColumn) elif dbStatement == 5: desc = f": Column of relation {name} does not exist" ErrorController().add(26, 'Execution', desc, noLine, noColumn)
def alterDropPK(self, name: str, noLine, noColumn): """ Method to remove primary key :param name: The name of table :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return if not TypeChecker().searchTable(database, name): desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return dbStatement = data_mode(database.mode).alterDropPK( database.name.lower(), name.lower()) if dbStatement == 0: DataWindow().consoleText( 'Query returned successfully: Alter Table drop PK') elif dbStatement == 1: ErrorController().add(34, 'Execution', '', noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) elif dbStatement == 4: desc = f": Column of relation {name} does not exist" ErrorController().add(26, 'Execution', desc, noLine, noColumn)
def showTables(self, noLine, noColumn) -> list: """ Method to show all tables in a database :param noLine: The instruction line :param noColumn: The instruction column :return: Returns list of tables """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None tables = data_mode(database.mode).showTables(database.name.lower()) if tables == None: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) return None return tables
def createTable(self, name: str, columns: int, line, column): """ Method to create a table in database :param name: The name of table :param columns: Number of columns :param line: The instruction line :param column: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).createTable( database.name.lower(), name.lower(), 0) if dbStatement == 0: table = Table(name) database.tables.append(table) self.writeFile() DataWindow().consoleText( 'Query returned successfully: Table created') return table elif dbStatement == 1: desc = f": Can't create table {name}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, line, column) elif dbStatement == 3: desc = f": Table {name} already exists" ErrorController().add(31, 'Execution', desc, line, column)
def deleteTable(self, name: str, line, column): """ Method to remove a table in database :param database: Table database :param name: The name of table :param line: The instruction line :param column: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).dropTable( database.name.lower(), name.lower()) if dbStatement == 0: table = self.searchTable(database, name) database.tables.remove(table) self.writeFile() DataWindow().consoleText( 'Query returned successfully: Table deleted') elif dbStatement == 1: desc = f": Can't drop table {name}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, line, column) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, line, column)
def updateDatabase(self, databaseOld: str, databaseNew: str, line, column): """ Method to update the name of a database in type checker :param databaseOld: The old name of the database :param databaseNew: The new name of the database :param line: The instruction line :param column: The instruction column :return: Returns nothing """ database = self.searchDatabase(databaseOld) if not database: desc = f": Database {databaseOld} does not exist" ErrorController().add(35, 'Execution', desc, line, column) return dbStatement = data_mode(database.mode).alterDatabase( databaseOld.lower(), databaseNew.lower()) if dbStatement == 0: database.name = databaseNew self.writeFile() DataWindow().consoleText( 'Query returned successfully: Database updated') elif dbStatement == 1: desc = f": Can't update database {databaseOld}" ErrorController().add(34, 'Execution', desc, line, column) elif dbStatement == 2: desc = f": Database {databaseOld} does not exist" ErrorController().add(35, 'Execution', desc, line, column) elif dbStatement == 3: desc = f": Database {databaseNew} already exists" ErrorController().add(30, 'Execution', desc, line, column)
def truncate(self, name: str, noLine, noColumn): """ Method to delete a record from a table :param name: The name of table :param noLine: The instruction line :param noColumn: The instruction column :return: Returns nothing """ database = SymbolTable().useDatabase if not database: desc = f": Database not selected" ErrorController().add(4, 'Execution', desc, noLine, noColumn) return None if not TypeChecker().searchTable(database, name): desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn) return None dbStatement = data_mode(database.mode).truncate( database.name.lower(), name.lower()) if dbStatement == 0: DataWindow().consoleText('Query returned successfully: TRUNCATE') elif dbStatement == 1: ErrorController().add(34, 'Execution', '', noLine, noColumn) elif dbStatement == 2: desc = f": Database {database.name} does not exist" ErrorController().add(35, 'Execution', desc, noLine, noColumn) elif dbStatement == 3: desc = f": Table {name} does not exist" ErrorController().add(27, 'Execution', desc, noLine, noColumn)