def execute(self, table: SymbolTable, tree): super().execute(table, tree) by_pass = False if isinstance(self.insert_list, Select): self.insert_list = self.insert_list.execute(table, tree)[1] by_pass = True # Table symbol so we can run checks and validations table_symbol = table.get(self.table_name, SymbolType.TABLE) all_fields_declared = table.get_fields_from_table(self.table_name) # Sorting list, so we know order to insert is correct all_fields_declared.sort(key=lambda x: x.field_index) # Mapping values to insert, to actual structure on data structure to_insert = [] if not by_pass: if self.column_list is not None: for field_symbol in all_fields_declared: # looking in column list if declared field appears or None match = next((col for col in self.column_list if col.val == field_symbol.name), None) # Run validations only if result is not None value_related_to_match = self.column_list.index(match) if match is not None: #to ENUM StmENUM = None try: StmENUM = table.get(field_symbol.field_type.upper(), SymbolType.TYPE) except: pass if StmENUM and self.insert_list[value_related_to_match].val not in StmENUM.value_list : raise Error(0, 0, ErrorType.SEMANTIC, f'Field {field_symbol.name} must be a take any of the follow: {str(StmENUM.value_list)}') # TODO ADD HERE TYPE VALIDATIONS PER FIELD, JUST ONE ADDED BY NOW TO GIVE EXAMPLE if field_symbol.field_type.upper() == 'INTEGER' and type(self.insert_list[value_related_to_match].val) != int: raise Error(0, 0, ErrorType.SEMANTIC, f'Field {field_symbol.name} must be an integer type') to_insert.append(self.insert_list[value_related_to_match].val) # TODO ADD HERE CHECK VALIDATION else: to_insert = list(map(lambda x: x.val, self.insert_list)) result = insert(table.get_current_db().name, self.table_name, to_insert) if result == 1: raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error') elif result == 2: raise Error(0, 0, ErrorType.RUNTIME, '42P04: database_does_not_exists') elif result == 3: raise Error(0, 0, ErrorType.RUNTIME, '42P07: table_does_not_exists') elif result == 4: raise Error(0, 0, ErrorType.RUNTIME, '42P10: duplicated_primary_key') else: for insert_reg in self.insert_list: result = insert(table.get_current_db().name, self.table_name, insert_reg) if result == 1: raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error') elif result == 2: raise Error(0, 0, ErrorType.RUNTIME, '42P04: database_does_not_exists') elif result == 3: raise Error(0, 0, ErrorType.RUNTIME, '42P07: table_does_not_exists') elif result == 4: raise Error(0, 0, ErrorType.RUNTIME, '42P10: duplicated_primary_key') return True
def execute(self, table: SymbolTable, tree): super().execute(table, tree) # result_owner = self.owner.execute(table, tree) old_symbol = table.get(self.name, SymbolType.DATABASE) old_symbol.owner = self.owner.val table.update(old_symbol) return True
def execute(self, table: SymbolTable, tree): super().execute(table, tree) result_table_name = self.table_name.execute(table, tree) old_symbol = table.get(result_table_name, SymbolType.TABLE) old_symbol.check_exp = self.validation # Change for append if needed to handle multiple ones table.update(old_symbol) return True
def execute(self, table: SymbolTable, tree): super().execute(table, tree) result_table_name = self.table_name.execute(table, tree) result_table_column = self.table_column.execute(table, tree) result_table_reference = self.table_reference.execute(table, tree) result_column_reference = self.column_reference.execute(table, tree) # Obtaining all fields because are gonna be needed to get correct field all_fields_symbol = table.get_fields_from_table(result_table_name) column_symbol = next((sym for sym in all_fields_symbol if sym.field_name == result_table_column), None) # Obtaining table and column itself, since we need to store id table_reference = table.get(result_table_reference, SymbolType.TABLE) column_reference = table.get(result_column_reference, SymbolType.FIELD) column_symbol.fk_table = table_reference.id column_symbol.fk_field = column_reference.id table.update(column_symbol) return True
def execute(self, table: SymbolTable, tree): super().execute(table, tree) result = alterDatabase(self.name, self.new_name) if result == 1: raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error') elif result == 2: raise Error(0, 0, ErrorType.RUNTIME, '42P04: old_database_does_not_exists') elif result == 3: raise Error(0, 0, ErrorType.RUNTIME, '42P04: new_database_already_exists') else: old_symbol = table.get(self.name, SymbolType.DATABASE) old_symbol.name = self.new_name table.update(old_symbol) return "You renamed table " + str(self.name) + " to " + str( self.new_name)