示例#1
0
文件: alter.py 项目: jossiebk/tytus
 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
示例#2
0
文件: alter.py 项目: jossiebk/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_table_name = self.table_name
     result_field_name = self.field_name
     result_field_type = self.field_type.val
     result_field_length = self.field_length
     result = alterAddColumn(table.get_current_db().name, result_table_name,
                             None)
     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,
                     '42P04: table_does_not_exists')
     else:
         total_fields = len(table.get_fields_from_table(result_table_name))
         column_symbol = FieldSymbol(table.get_current_db().name,
                                     result_table_name, total_fields + 1,
                                     result_field_name, result_field_type,
                                     result_field_length, self.allows_null,
                                     False, None, None)
         table.add(column_symbol)
         return True
示例#3
0
文件: alter.py 项目: jossiebk/tytus
 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
示例#4
0
文件: create.py 项目: edgarJ91/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_values = []
     for val in self.value_list:          
         result_values.append(val.execute(table, tree))
     symbol = TypeSymbol(self.name, result_values)
     table.add(symbol)
     print(f'[AST] ENUM {self.name} created.')
     return f'[AST] ENUM {self.name} created.'
示例#5
0
文件: insert.py 项目: edgarJ91/tytus
 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
示例#6
0
文件: alter.py 项目: jossiebk/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_field_name = self.field_name
     result_table_name = self.table_name
     # 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_field_name), None)
     column_symbol.allows_null = self.allows_null
     table.update(column_symbol)
     return True
示例#7
0
文件: alter.py 项目: vkslax/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_field_name = self.old_name
     result_new_name = self.new_name
     result_table_name = self.table_name
     # 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_field_name), None)
     column_symbol.field_name = result_new_name
     column_symbol.name = result_new_name
     table.update(column_symbol)
     return f'Column {self.old_name} renamed to {self.new_name}'
示例#8
0
文件: drop.py 项目: jossiebk/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_name = self.name.execute(table, tree)
     result = dropTable(table.get_current_db().name, result_name)
     if result == 0:  # successful operation
         table.drop_table(result_name)
         return "Table " + str(result_name) + " has been dropped."
     elif result == 1:  # operation error
         raise Error(0, 0, ErrorType.RUNTIME, '58000: system_error')
     elif result == 2:  # database does not exist.
         raise Error(0, 0, ErrorType.RUNTIME, '42P04: database_does_not_exists')
     elif result == 3:  # table does not exist
         raise Error(0, 0, ErrorType.RUNTIME, '42P01: table_does_not_exists')
示例#9
0
文件: create.py 项目: edgarJ91/tytus
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        #result_name = self.name.execute(table, tree)
        #result_owner = self.owner.execute(table, tree) if self.owner else None  # Owner seems to be stored only to ST
        #result_mode = self.owner.mode(table, tree) if self.mode else 6  # Change to 1 when default mode from EDD available
        result_name = self.name.execute(table, tree)
        result_owner = self.owner
        result_mode = self.mode
        result = 0
        if self.replace:
            dropDatabase(result_name)
        
        #if result_mode == 6:  # add more ifs when modes from EDD available
        result = createDatabase(result_name)

        if result == 1:
            # log error on operation
            raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error')
            return False
        elif result == 2 and self.exists == False:
            # log error because db already exists
            raise Error(0, 0, ErrorType.RUNTIME, '42P04: duplicate_database')
            return False
        else:            
            #return table.add(DatabaseSymbol(result_name, result_owner, result_mode)) #chaged by loadDatabases
            table.LoadDataBases()
            return ['Database \'' + result_name + '\' was created successfully!']
示例#10
0
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        # result_name = self.name.execute(table, tree)
        result_name = self.name
        result_owner = self.owner.execute(
            table, tree
        ) if self.owner else None  # Owner seems to be stored only to ST
        result_mode = self.mode(
            table, tree
        ) if self.mode is not None else 6  # Change to 1 when default mode available
        if self.replace:
            dropDatabase(result_name)
        result = 0
        if result_mode == 6:  # add more ifs when modes from EDD available
            result = createDatabase(result_name)

        if result == 1:
            # log error on operation
            raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error')
            return False
        elif result == 2:
            # log error because db already exists
            raise Error(0, 0, ErrorType.RUNTIME, '42P04: duplicate_database')
            return False
        else:
            return table.add(
                DatabaseSymbol(result_name, result_owner, result_mode))
示例#11
0
文件: drop.py 项目: jossiebk/tytus
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        result_name = self.name.execute(table, tree)

        result = 0
        if self.if_exists:
            dropDatabase(result_name)
            return "Database " + str(result_name) + " has been dropped"
        else:
            result = dropDatabase(result_name)
            if result == 0:  # successful operation
                table.drop_data_base(result_name)
                return "Database " + str(result_name) + " has been dropped."
            elif result == 1:  # operation error
                raise Error(0, 0, ErrorType.RUNTIME, '58000: system_error')
            elif result == 2:  # database does not exist.
                raise Error(0, 0, ErrorType.RUNTIME, '42P04: database_does_not_exists')
示例#12
0
文件: alter.py 项目: jossiebk/tytus
 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
示例#13
0
文件: alter.py 项目: jossiebk/tytus
 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)
示例#14
0
文件: create.py 项目: vkslax/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_name = self.name.execute(table, tree)
     result_field_type = self.field_type.execute(table, tree)
     result_length = self.length.execute(table, tree)
     return FieldSymbol(table.get_current_db().name, None, 0, result_name,
                        result_field_type, result_length, self.allows_null,
                        self.is_pk, None, None)
示例#15
0
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        result_name = self.name.execute(table, tree)
        result_inherits_from = self.inherits_from.execute(
            table, tree) if self.inherits_from else None
        result_fields = []
        if result_inherits_from:
            # get inheritance table, if doesn't exists throws semantic error, else append result
            result_fields.append(
                table.get_fields_from_table(result_inherits_from))

        result = createTable('db_from_st', result_name, len(result_fields))
        if result == 1:
            raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error')
            return False
        elif result == 2:
            raise Error(0, 0, ErrorType.RUNTIME,
                        '42P04: database_does_not_exists')
            return False
        elif result == 3:
            raise Error(0, 0, ErrorType.RUNTIME, '42P07: duplicate_table')
            return False
        else:
            table.add(TableSymbol(table.get_current_db().name, result_name))

        result_fields = self.fields.execute(
            table, tree)  # A list of TableField assumed
        for field in result_fields:
            field.table_name = result_name
            table.add(field)
        return
示例#16
0
文件: create.py 项目: vkslax/tytus
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        result_name = self.name
        result_inherits_from = self.inherits_from.val if self.inherits_from else None
        result_fields = self.fields
        if result_inherits_from:
            # get inheritance table, if doesn't exists throws semantic error, else append result
            result_fields += table.get_fields_from_table(result_inherits_from)

        result = createTable(table.get_current_db().name, result_name,
                             len(result_fields))

        if result == 1:
            raise Error(self.line, self.column, ErrorType.RUNTIME,
                        '5800: system_error')
        elif result == 2:
            raise Error(self.line, self.column, ErrorType.RUNTIME,
                        '42P04: database_does_not_exists')
        elif result == 3:
            raise Error(self.line, self.column, ErrorType.RUNTIME,
                        '42P07: duplicate_table')
        else:
            # add primary keys, jsonMode needs the number of the column to set it to primarykey
            keys = list(
                map(lambda x: result_fields.index(x),
                    filter(lambda key: key.is_pk is True, result_fields)))
            if len(keys) > 0:
                result = alterAddPK(table.get_current_db().name, result_name,
                                    keys)

            table.add(
                TableSymbol(table.get_current_db().id, result_name,
                            self.check_exp))

        field_index = 0
        for field in result_fields:
            nuevo = FieldSymbol(table.get_current_db().name, result_name,
                                field_index, field.name, field.field_type,
                                field.length, field.allows_null, field.is_pk,
                                None, None)
            field_index += 1
            table.add(nuevo)

        return "Table: " + str(result_name) + " created."
示例#17
0
文件: create.py 项目: edgarJ91/tytus
    def execute(self, table: SymbolTable, tree):
        super().execute(table, tree)
        #result_name = self.name.execute(table, tree)
        result_name = self.name
        result_inherits_from = self.inherits_from.execute(table, tree) if self.inherits_from else None
        result_fields = self.fields
        if result_inherits_from:
            # get inheritance table, if doesn't exists throws semantic error, else append result
            result_fields.append(table.get_fields_from_table(result_inherits_from))


        result = createTable(table.get_current_db().name, result_name, len(result_fields))    

        if result == 1:
            raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error')
            return False
        elif result == 2:
            raise Error(0, 0, ErrorType.RUNTIME, '42P04: database_does_not_exists')
            return False
        elif result == 3:
            raise Error(0, 0, ErrorType.RUNTIME, '42P07: duplicate_table')
            return False
        else:
            #add primary keys, jsonMode needs the number of the column to set it to primarykey            
            keys = list( 
                map(
                    lambda x: result_fields.index(x),
                    filter(lambda key: key.is_pk == True, result_fields)                           
                    )  
            )
            if(len(keys)>0):
                result = alterAddPK(table.get_current_db().name, result_name, keys)

            table.add(TableSymbol(table.get_current_db().id, result_name, self.check_exp))

        ##result_fields = self.fields.execute(table, tree)  # A list of TableField assumed

        field_index = 0
        for field in result_fields:
            field.table_name = result_name
            field.field_index = field_index
            field.type = SymbolType.FIELD
            field_index += 1
            table.add(field)
        return "Table: " +str(result_name) +" created."
示例#18
0
文件: delete.py 项目: edgarJ91/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_table_name = self.table_name.execute(table, tree)
     result_where = self.where.execute(
         table, tree)  # Select is a must, check result from where
     # This array must be filled with pks to delete
     result = delete(table.get_current_db().name, result_table_name, ['1'])
     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: PK_does_not_exists')
     else:
         return True
示例#19
0
文件: alter.py 项目: edgarJ91/tytus
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_table_name = self.table_name.execute(table, tree)
     result_field_name = self.field_name.execute(table, tree)
     # Obtaining all fields because are gonna be needed to get correct field and update indexes later
     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_field_name), None)
     result = alterDropColumn(table.get_current_db(), result_field_name,
                              column_symbol.field_index)
     if result == 1:
         raise Error(0, 0, ErrorType.RUNTIME, '5800: system_error')
         return False
     elif result == 2:
         # log error, old database name does not exists
         raise Error(0, 0, ErrorType.RUNTIME,
                     '42P04: database_does_not_exists')
         return False
     elif result == 3:
         # log error, table does not exists
         raise Error(0, 0, ErrorType.RUNTIME,
                     '42P04: table_does_not_exists')
         return False
     elif result == 4:
         # log error, PK cannot be deleted or table to be empty
         raise Error(0, 0, ErrorType.RUNTIME,
                     '2300: integrity_constraint_violation')
         return False
     elif result == 4:
         # log error, column out of index
         raise Error(0, 0, ErrorType.RUNTIME, '2300: column_out_of_index')
         return False
     else:
         for field in all_fields_symbol:
             # Update indexes for higher fields
             if field.field_index > column_symbol:
                 field.field_index -= 1
                 table.update(field)
         # TODO just realized it's needed to check for FKs in other tables
         # finally delete symbol of column removed
         table.delete(column_symbol.id)
         return True
示例#20
0
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_values = self.value_list.execute(table, tree)
     symbol = TypeSymbol(self.name, result_values)
     return table.add(symbol)
示例#21
0
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_name = self.name.execute(table, tree)
     table.set_current_db(result_name)
     return True
示例#22
0
 def execute(self, table: SymbolTable, tree):
     super().execute(table, tree)
     result_name = self.name.execute(table, tree)
     table.set_current_db(result_name)
     return "You are using \'" + str(result_name) + "\' DB"