def execute(self, environment): Struct.load() try: if self.structure == "TABLE": if instruction.dbtemp != "": valor = jsonMode.dropTable(instruction.dbtemp, self.name) if valor == 2: instruction.semanticErrors.append([ "La base de datos " + str(instruction.dbtemp) + " no existe", self.row, ]) instruction.syntaxPostgreSQL.append( "Error: 42000: La base de datos " + str(instruction.dbtemp) + " no existe") return "La base de datos no existe" if valor == 3: instruction.semanticErrors.append([ "La tabla " + str(self.name) + " no existe", self.row ]) instruction.syntaxPostgreSQL.append( "Error: 42P01: La tabla " + str(self.name) + " no existe") return "La tabla no existe en la base de datos" if valor == 1: instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Hubo un problema en la ejecucion de la sentencia DROP" if valor == 0: Struct.dropTable(instruction.dbtemp, self.name) return "DROP TABLE Se elimino la tabla: " + self.name instruction.syntaxPostgreSQL.append( "Error: 42000: Base de datos no especificada ") return "El nombre de la base de datos no esta especificado operacion no realizada" else: valor = jsonMode.dropDatabase(self.name) if valor == 1: instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Hubo un problema en la ejecucion de la sentencia" if valor == 2: instruction.semanticErrors.append([ "La base de datos " + str(self.name) + " no existe", self.row, ]) instruction.syntaxPostgreSQL.append( "Error: 42000: La base de datos " + str(self.name) + " no existe") return "La base de datos no existe" if valor == 0: Struct.dropDatabase(self.name) return "Instruccion ejecutada con exito DROP DATABASE" instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno DROPTABLE") return "Fatal Error: DROP TABLE" except: instruction.syntaxPostgreSQL.append( "Error: P0001: Error en la instruccion DROP")
def execute(self, environment): Struct.load() # insert = [posiblesErrores,noColumnas] insert = Struct.insertTable(instruction.dbtemp, self.name, self.columns, self.inherits) error = insert[0] nCol = insert[1] if not error: error = Checker.checkValue(instruction.dbtemp, self.name) """ Result 0: insert 1: error 2: not found database 3: exists table """ if not error: result = jsonMode.createTable(instruction.dbtemp, self.name, nCol) if result == 0: pass elif result == 1: instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Error: No se puede crear la tabla: " + self.name elif result == 2: instruction.semanticErrors.append("La base de datos " + instruction.dbtemp + " no existe") instruction.syntaxPostgreSQL.append( "Error: 3F000: base de datos" + instruction.dbtemp + " no existe") return "Error: Base de datos no encontrada: " + instruction.dbtemp elif result == 3 and self.exists: instruction.semanticErrors.append( ["La tabla " + str(self.name) + " ya existe", self.row]) instruction.syntaxPostgreSQL.append( "Error: 42P07: La tabla " + str(self.name) + " ya existe") return "La tabla ya existe en la base de datos" else: instruction.semanticErrors.append( ["La tabla " + str(self.name) + " ya existe", self.row]) instruction.syntaxPostgreSQL.append( "Error: 42P07: tabla duplicada") return "Error: ya existe la tabla " + self.name pk = Struct.extractPKIndexColumns(instruction.dbtemp, self.name) addPK = 0 if pk: addPK = jsonMode.alterAddPK(instruction.dbtemp, self.name, pk) if addPK != 0: instruction.syntaxPostgreSQL.append( "Error: 23505: Error en llaves primarias de la instruccion CREATE TABLE de la tabla " + str(self.name)) return "Tabla " + self.name + " creada" else: Struct.dropTable(instruction.dbtemp, self.name) return error
def execute(self, environment): lista = [] if self.like != None: for l in jsonMode.showDatabases(): if self.like in l: lista.append(l) else: lista = jsonMode.showDatabases() if len(lista) == 0: return "No hay bases de datos" return lista
def execute(self, environment): Struct.load() result = jsonMode.createDatabase(self.name) """ 0: insert 1: error 2: exists """ if self.mode == None: self.mode = 1 if result == 0: Struct.createDatabase(self.name, self.mode, self.owner) report = "Base de datos: " + self.name + " insertada." elif result == 1: instruction.syntaxPostgreSQL.append("Error: XX000: Error interno") report = "Error al insertar la base de datos: " + self.name elif result == 2 and self.replace: Struct.replaceDatabase(self.name, self.mode, self.owner) report = "Base de datos '" + self.name + " ' reemplazada." elif result == 2 and self.exists: report = "Base de datos no insertada, " + self.name + " ya existe." else: instruction.semanticErrors.append([ "La base de datos " + str(self.name) + " ya existe", self.row ]) instruction.syntaxPostgreSQL.append( "Error: 42P04: La base de datos " + str(self.name) + " ya existe") report = "Error: La base de datos ya existe" return report
def execute(self, environment): try: valor = jsonMode.truncate(instruction.dbtemp, self.name) if valor == 2: instruction.semanticErrors.append([ "La base de datos " + str(instruction.dbtemp) + " no existe ", self.row, ]) instruction.syntaxPostgreSQL.append( "Error: 42000: La base de datos " + str(instruction.dbtemp) + " no existe") return "La base de datos no existe" if valor == 3: instruction.semanticErrors.append( ["La tabla " + str(self.name) + " no existe ", self.row]) instruction.syntaxPostgreSQL.append("Error: 42P01: La tabla " + str(self.name) + " no existe") return "El nombre de la tabla no existe" if valor == 1: instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Hubo un problema en la ejecucion de la sentencia" if valor == 0: return "Truncate de la tabla: " + self.name except: instruction.syntaxPostgreSQL.append( "Error: P0001: Error en la instruccion TRUNCATE")
def execute(self, environment): dbs = jsonMode.showDatabases() if self.db in dbs: global dbtemp dbtemp = self.db return "Se cambio la base de datos a: " + dbtemp syntaxPostgreSQL.append("Error: 42000: La base de datos " + self.db + " no existe") semanticErrors.append( ["La base de datos " + str(self.db) + " no existe", self.row]) return "La base de datos: " + self.db + " no existe."
def validateUnique(database, table, value, index): records = jsonMode.extractTable(database, table) if records == []: return for record in records: if value == record[index]: lstErr.append("El Valor " + str(value) + " ya existe dentro de la tabla") syntaxPostgreErrors.append("Error: 23505: El valor " + str(value) + " ya existe dentro de la tabla") break
def validatePrimary(database, table, values, index): records = jsonMode.extractTable(database, table) if records == []: return for record in records: lst1 = [] lst2 = [] for j in index: lst1.append(record[j]) lst2.append(values[j].value) if lst1 == lst2: lstErr.append("Llaves primarias existentes dentro de la tabla") syntaxPostgreErrors.append( "Error: 23505: llaves primarias duplicadas ") break
def validateForeign(database, values, value): # values = [references,column] references = values[0] column = values[1] records = jsonMode.extractTable(database, references) if records == []: syntaxPostgreErrors.append("Error: 23503: El valor " + str(value) + " no es una llave foranea ") lstErr.append("El Valor " + str(value) + " no es una llave foranea") return index = S.getIndex(database, references, column) for record in records: if value == record[index]: return lstErr.append("El Valor " + str(value) + " no es una llave primaria") syntaxPostgreErrors.append("Error: 23505: El valor " + str(value) + " no es una llave primaria ")
def execute(self, environment): Struct.load() try: if self.option == "RENAME": valor = jsonMode.alterDatabase(self.name, self.newname) if valor == 2: instruction.semanticErrors.append([ "La base de datos " + str(self.name) + " no existe", self.row ]) instruction.syntaxPostgreSQL.append( "Error: 42000: La base de datos " + str(self.name) + " no existe") return "La base de datos no existe: '" + self.name + "'." if valor == 3: instruction.semanticErrors.append([ "La base de datos " + str(self.newname) + " ya existe", self.row, ]) instruction.syntaxPostgreSQL.append( "Error: 42P04: La base de datos " + str(self.newname) + " ya existe") return "El nuevo nombre para la base de datos existe" if valor == 1: instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Hubo un problema en la ejecucion de la sentencia" if valor == 0: Struct.alterDatabaseRename(self.name, self.newname) return ("Base de datos renombrada: " + self.name + " - " + self.newname) return "Error ALTER DATABASE RENAME: " + self.newname elif self.option == "OWNER": valor = Struct.alterDatabaseOwner(self.name, self.newname) if valor == 0: return "Instruccion ejecutada con exito ALTER DATABASE OWNER" instruction.syntaxPostgreSQL.append( "Error: XX000: Error interno") return "Error ALTER DATABASE OWNER" instruction.syntaxPostgreSQL.append("Error: XX000: Error interno") return "Fatal Error ALTER DATABASE: " + self.newname except: instruction.syntaxPostgreSQL.append( "Error: P0001: Error en la instruccion ALTER DATABASE")
def execute(self, environment): result = jsonMode.extractTable(instruction.dbtemp, self.name) if result == None: instruction.semanticErrors.append([ "La tabla " + str(self.name) + " no pertenece a la base de datos " + instruction.dbtemp, self.row, ]) instruction.syntaxPostgreSQL.append("Error: 42P01: la relacion " + instruction.dbtemp + "." + str(self.name) + " no existe") return "FATAL ERROR TABLE ID" # Almacena una lista con con el nombre y tipo de cada columna lst = Struct.extractColumns(instruction.dbtemp, self.name) columns = [l.name for l in lst] newColumns = [self.name + "." + col for col in columns] df = pd.DataFrame(result, columns=newColumns) environment.addTable(self.name) tempTypes = {} for i in range(len(newColumns)): tempTypes[newColumns[i]] = lst[i].type return [df, tempTypes]
def execute(self, environment): try: Struct.load() lista = [] params = [] tab = self.tabla for p in self.parametros: params.append(p.execute(environment)) result = Checker.checkInsert( instruction.dbtemp, self.tabla, self.columns, params ) if result[0] == None: for p in result[1]: if p == None: lista.append(p) else: lista.append(p.value) res = jsonMode.insert(instruction.dbtemp, tab, lista) if res == 2: instruction.semanticErrors.append( [ "La base de datos " + instruction.dbtemp + " no existe", self.row, ] ) instruction.syntaxPostgreSQL.append( "Error: 42000: La base de datos " + str(instruction.dbtemp) + " no existe" ) return "La base de datos no existe" elif res == 3: instruction.semanticErrors.append( ["La tabla " + str(tab) + " no existe", self.row] ) instruction.syntaxPostgreSQL.append( "Error: 42P01: La tabla " + str(tab) + " no existe" ) return "No existe la tabla" elif res == 5: instruction.semanticErrors.append( [ "La instruccion INSERT tiene mas o menos registros que columnas", self.row, ] ) instruction.syntaxPostgreSQL.append( "Error: 42611: INSERT tiene mas o menos registros que columnas " ) return "Columnas fuera de los limites" elif res == 4: instruction.semanticErrors.append( [ "El valor de la clave esta duplicada, viola la restriccion unica", self.row, ] ) instruction.syntaxPostgreSQL.append( "Error: 23505: el valor de clave esta duplicada, viola la restricción única " ) return "Llaves primarias duplicadas" elif res == 1: instruction.syntaxPostgreSQL.append("Error: XX000: Error interno") return "Error en la operacion" elif res == 0: return "Fila Insertada correctamente" else: return result[0] except: instruction.syntaxPostgreSQL.append( "Error: P0001: Error en la instruccion INSERT" ) pass
def execute(self, environment): try: Struct.load() # Verificamos que no pueden venir mas de 1 tabla en el clausula FROM if len(self.fromcl.tables) > 1: instruction.semanticErrors.append( ["Error sintactico cerco e en ','", self.row] ) instruction.syntaxPostgreSQL.append( "Error: 42601: Error sintactico cerca de , en la linea " + str(self.row) ) return "Error: syntax error at or near ','" newEnv = Environment(environment, instruction.dbtemp) instruction.envVariables.append(newEnv) self.fromcl.execute(newEnv) value = [newEnv.dataFrame[p] for p in newEnv.dataFrame] labels = [p for p in newEnv.dataFrame] for i in range(len(labels)): newEnv.dataFrame[labels[i]] = value[i] if self.wherecl == None: w2 = newEnv.dataFrame.filter(labels) else: wh = self.wherecl.execute(newEnv) w2 = wh.filter(labels) # Si la clausula WHERE devuelve un dataframe vacio if w2.empty: return "Operacion UPDATE completada" # Logica para realizar el update table = self.fromcl.tables[0].name pk = Struct.extractPKIndexColumns(instruction.dbtemp, table) # Se obtienen las parametros de las llaves primarias para proceder a eliminar rows = [] if pk: for row in w2.values: rows.append([row[p] for p in pk]) else: rows.append([i for i in w2.index]) print(rows) # Obtenemos las variables a cambiar su valor ids = [p.id for p in self.values] values = [p.execute(newEnv).value for p in self.values] ids = Struct.getListIndex(instruction.dbtemp, table, ids) if len(ids) != len(values): return "Error: Columnas no encontradas" temp = {} for i in range(len(ids)): temp[ids[i]] = values[i] print(temp, rows) # TODO: La funcion del STORAGE esta bugueada bug = False for row in rows: result = jsonMode.update(instruction.dbtemp, table, temp, rows) if result != 0: bug = True break if bug: return ["Error: Funcion UPDATE del Storage", temp, rows] return "Operacion UPDATE completada" except: instruction.syntaxPostgreSQL.append( "Error: P0001: Error en la instruccion UPDATE" )