Beispiel #1
0
    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
Beispiel #2
0
    def compile(self, environment):
        database_id = SymbolTable().useDatabase
        # ejecutando si hay llamada a alguna funcion
        temps_array = []
        for value in self.arr_values:
            if isinstance(value, Funcion):
                temps_array.append(value.compile(environment))
        new_val = putVarValues(self._tac, temps_array, environment)

        temp = ''

        if new_val == self._tac:  # Es un temporal --- quitar comillas

            temp = ThreeAddressCode().newTemp()

            if database_id is not None:
                ThreeAddressCode().addCode(
                    f"{temp} = \"USE {database_id}; {new_val}\"")
            else:
                ThreeAddressCode().addCode(f"{temp} = \"{new_val}\"")
        else:
            temp = new_val

            # LLAMANDO A FUNCION PARA ANALIZAR ESTA COCHINADA
        temp1 = ThreeAddressCode().newTemp()
        ThreeAddressCode().addCode(f"{temp1} = parse({temp})")
        return temp1
Beispiel #3
0
    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)
Beispiel #4
0
    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
Beispiel #5
0
    def generateColumns(self, nombreTabla, typeChecker):
        for columna in self._column_list:
            if isinstance(columna, CreateCol):
                # columna._paramOne
                # columna._paraTwos
                # columna._tipoColumna
                tipoFinal = {
                    '_tipoColumna': str(columna._type_column._tipoColumna),
                    '_paramOne': columna._type_column._paramOne,
                    '_paramTwo': columna._type_column._paramTwo
                }
                columnaFinal = Column(columna._column_name, tipoFinal)
                if columna._properties != None:
                    for prop in columna._properties:
                        columnaFinal = self.addPropertyes(prop, columnaFinal)

                tableToInsert = typeChecker.searchTable(
                    SymbolTable().useDatabase, nombreTabla)
                validateCol = typeChecker.createColumnTable(
                    tableToInsert, columnaFinal, 0, 0)

                # if return None an error ocurrio
                if validateCol == None:
                    self._can_create_flag = False
                    return
Beispiel #6
0
    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)
Beispiel #7
0
 def compile(self, instrucction):
     temp = ThreeAddressCode().newTemp()
     database_id = SymbolTable().useDatabase
     if database_id is not None:
         ThreeAddressCode().addCode(
             f"{temp} = \"USE {database_id}; {self._tac}\"")
     else:
         ThreeAddressCode().addCode(f"{temp} = \"{self._tac}\"")
Beispiel #8
0
    def getParams(self, name):
        self.loadFile()
        db = SymbolTable().useDatabase
        key = f"{name}{db}"

        if key in self.__storedProcedure:
            return self.__storedProcedure[key]['tac'].params

        return []
Beispiel #9
0
    def process(self, instrucction):
        typeNew = Type(self._name.alias)
        for valor in self._values:
            typeNew._values.append(valor.alias)

        SymbolTable().add(typeNew._name, str(typeNew._values), 'TYPE', 'DB',
                          None, '0', '0')
        print(typeNew)
        # CREANDO C3D
        temp = ThreeAddressCode().newTemp()
        database_id = SymbolTable().useDatabase
        if database_id is not None:
            ThreeAddressCode().addCode(
                f"{temp} = \"USE {database_id}; {self._tac}\"")
        else:
            ThreeAddressCode().addCode(f"{temp} = \"{self._tac}\"")
        # LLAMANDO A FUNCION PARA ANALIZAR ESTA COCHINADA
        temp1 = ThreeAddressCode().newTemp()
        ThreeAddressCode().addCode(f"{temp1} = parse({temp})")
Beispiel #10
0
    def process(self, instrucction):
        typeChecker = TypeChecker()
        database = typeChecker.searchDatabase(self._dbActual)

        if not database:
            desc = f": Database {self._dbActual} does not exist"
            ErrorController().add(35, 'Execution', desc,
                                  self._noLine, self._noColumn)
            return

        SymbolTable().useDatabase = database
        DataWindow().consoleText('Query returned successfully: USE DATABASE')
Beispiel #11
0
    def addInherits(self, nameChildTable, nameParentTable):
        typeChecker = TypeChecker()
        tablaPadre = typeChecker.searchTable(SymbolTable().useDatabase,
                                             nameParentTable)
        tablaHija = typeChecker.searchTable(SymbolTable().useDatabase,
                                            nameChildTable)

        # La tabla de la que hereda no existe
        if tablaPadre == None:
            desc = f": parent table dont exists"
            ErrorController().add(27, 'Execution', desc, 0, 0)
            self._can_create_flag = False
            return

        for colPar in tablaPadre._colums:
            # Vamos a insertar en la hija
            validarCol = typeChecker.createColumnTable(tablaHija, colPar, 0, 0)
            # Si es una columna repetida entonces no puede crear la tabla
            if validarCol == None:
                self._can_create_flag = False
                return
Beispiel #12
0
 def addVar(self, id, _type, value, pos, line, col):
     # id = id.lower()
     if self.variables.get(id) == None:
         SymbolTable().add(id, value, _type, self, None, line, col)
         newVar = Variable(pos, _type, value, line, col)
         self.variables[id] = newVar
         return newVar
     else:
         print("VARIABLE DECLARADA ------ ERROR", id)
         ErrorController().add(33, 'Execution',
                               f"VARIABLE {id} YA DECLARADA", line, col)
     return self.getVar(id)
Beispiel #13
0
    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)
Beispiel #14
0
class SymbolTableReport(object):
    def __init__(self):
        self._symbolTable = SymbolTable()
        self._data = ''

    def generateReport(self):
        self._data = 'digraph {\n\ttbl [\n\tshape=plaintext\n\tlabel=<'
        self._data += '\n\t\t<table border=\'0\' cellborder=\'1\' color=\'#324960\' cellspacing=\'0\'>'
        self._data += '\n\t\t\t<tr>\n\t\t\t\t<td bgcolor="#324960" colspan=\'8\' SIDES=\"TB\">'
        self._data += f"\n\t\t\t\t\t<font color=\"white\">SYMBOL TABLE</font>"
        self._data += '\n\t\t\t\t</td>\n\t\t\t</tr>'

        self.generateHeaders()

        self._data += '\n\t\t</table>\n\t>];\n}'

        return self._data

    def generateHeaders(self):
        self._data += '\n\t\t\t<tr>'
        self.dataHeaderCol('ID')
        self.dataHeaderCol('Name')
        self.dataHeaderCol('Value')
        self.dataHeaderCol('Type')
        self.dataHeaderCol('Environment')
        self.dataHeaderCol('References')
        self.dataHeaderCol('Line')
        self.dataHeaderCol('Column')
        self._data += '\n\t\t\t</tr>'

        for col in self._symbolTable.getList():
            self._data += '\n\t\t\t<tr>'
            self.dataCol(col.idSymbol)
            self.dataCol(col.name)
            self.dataCol(col.value)
            self.dataCol(col.dataType)
            self.dataCol(col.environment)
            self.dataCol(col.references)
            self.dataCol(col.row)
            self.dataCol(col.column)
            self._data += '\n\t\t\t</tr>'

    def dataHeaderCol(self, name):
        self._data += f"\n\t\t\t\t<td bgcolor=\"#4fc3a1\" SIDES=\"B\"><b><font color=\"white\"> {name} </font></b></td>"

    def dataCol(self, data):
        self._data += f"\n\t\t\t\t<td SIDES=\"B\"> {data} </td>"
Beispiel #15
0
    def addForeignKey(self, listaCols, nombreTabla, listaTablasCols):

        # the len of the cols must be de same
        if len(listaCols) != len(listaTablasCols):
            desc = f": cant of params in foreign() != "
            ErrorController().add(36, 'Execution', desc, 0, 0)
            self._can_create_flag = False
            return

        typeChecker = TypeChecker()
        existForeingTable = typeChecker.searchTable(SymbolTable().useDatabase,
                                                    nombreTabla)

        # validate if the foreign table exists
        if existForeingTable == None:
            desc = f": Undefined table in foreign key ()"
            ErrorController().add(27, 'Execution', desc, 0, 0)
            self._can_create_flag = False
            return

        # validate if the columns exists in the foreign table
        for coli in listaTablasCols:
            if typeChecker.searchColumn(existForeingTable, coli) == None:
                desc = f": Undefined col in table in foreign key ()"
                ErrorController().add(26, 'Execution', desc, 0, 0)
                self._can_create_flag = False
                return

        bandera = False
        for x in range(0, len(listaCols)):
            for columna in self._column_list:
                if isinstance(columna, CreateCol):
                    if (columna._column_name == listaCols[x]):
                        if columna._properties != None:
                            bandera = True
                            columna._properties[0]['fk_references_to'] = {
                                '_refTable': nombreTabla,
                                '_refColumn': listaTablasCols[x]
                            }
                            break
            if not bandera:
                desc = f": Undefined column in foreign key ()"
                ErrorController().add(26, 'Execution', desc, 0, 0)
                self._can_create_flag = False
                return
            bandera = False
Beispiel #16
0
    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)
Beispiel #17
0
    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)
Beispiel #18
0
    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)