Skip to content

Commit

Permalink
Merge pull request #5 from LMOlivera/dev
Browse files Browse the repository at this point in the history
Actualización de base de datos, refactorización completa
  • Loading branch information
LMOlivera authored Mar 9, 2019
2 parents 4dba257 + b60bc36 commit 6465228
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 184 deletions.
29 changes: 29 additions & 0 deletions app/logic/clsSqlDelete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pymysql.cursors

class SqlDelete:
def __init__(self):
self.conexion = pymysql.connect(host='localhost',
user='root',
password='',
db='uruguia_bd_test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

def borrarUsuario(self, id_usuario, tipo):
with self.conexion.cursor() as cursor:
query = """
DELETE FROM usuario WHERE id_usuario=%s
"""
cursor.execute(query,(id_usuario))
if tipo=='turista':
query = """
DELETE FROM turista WHERE id=%s
"""
cursor.execute(query,(id_usuario))
else:
query = """
DELETE FROM empresa WHERE id=%s
"""
cursor.execute(query,(id_usuario))
self.conexion.commit()
pass
77 changes: 77 additions & 0 deletions app/logic/clsSqlInsert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pymysql.cursors

class SqlInsert:
def __init__(self):
self.conexion = pymysql.connect(host='localhost',
user='root',
password='',
db='uruguia_bd_test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.userdata = ""

def insertarLugar(self, nombre, descripcion, ubicacion, tipo, horario, fecha):
with self.conexion.cursor() as cursor:
query = """
INSERT INTO lugar
(nombre, descripcion, ubicacion, tipo, horario, fecha)
VALUES
(%s,%s,%s,%s,%s, %s)
"""
cursor.execute(query,(nombre, descripcion, ubicacion, tipo, horario, fecha))
self.conexion.commit()
pass

def insertar_pertenece_a(self, ide, idc):
with self.conexion.cursor() as cursor:
query = """
INSERT INTO pertenece_a
(ide, idc)
VALUES
(%s,%s)
"""
cursor.execute(query,(ide, idc))
self.conexion.commit()
pass

def insertar_tiene(self, ide, id):
with self.conexion.cursor() as cursor:
query = """
INSERT INTO tiene
(ide, id)
VALUES
(%s,%s)
"""
cursor.execute(query,(ide, id))
self.conexion.commit()
pass

def crearUsuario(self, email, nombre, password, tipo, id_nuevoUsuario, edad, pais, nombreEmpresa):
with self.conexion.cursor() as cursor:
query = """
INSERT INTO usuario
(id_usuario,email,nombre,contrasena,tipo)
VALUES
(%s,%s,%s,%s,%s)
"""
cursor.execute(query,(id_nuevoUsuario,email,nombre,password,tipo))

if tipo=='turista':
query = """
INSERT INTO turista
(id,edad,pais_origen)
VALUES
(%s,%s,%s)
"""
cursor.execute(query,(id_nuevoUsuario,edad,pais))
else:
query = """
INSERT INTO empresa
(id,nombre)
VALUES
(%s,%s)
"""
cursor.execute(query,(id_nuevoUsuario,nombreEmpresa))
self.conexion.commit()
pass

101 changes: 101 additions & 0 deletions app/logic/clsSqlSelect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import pymysql.cursors

class SqlSelect:
def __init__(self):
self.conexion = pymysql.connect(host='localhost',
user='root',
password='',
db='uruguia_bd_test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.userdata = ""

def login(self, email, password):
with self.conexion.cursor() as cursor:
query = "SELECT id_usuario, email, nombre, tipo FROM usuario WHERE email=%s AND contrasena=%s"
cursor.execute(query,(email,password))
self.userdata = cursor.fetchone()
results = cursor.rowcount
if results==0:
return False
else:
return True
pass

def listarLugares(self, id):
with self.conexion.cursor() as cursor:
query = """
SELECT l.nombre,
l.descripcion,
l.ubicacion,
l.tipo,
l.horario,
l.fecha
FROM tiene t
INNER JOIN lugar l
ON t.ide=l.ide
WHERE id=%s
"""
cursor.execute(query,(id))
listaLugares = cursor.fetchall()
return listaLugares
self.conexion.commit()
pass

def conseguir_ide(self, nombre):
with self.conexion.cursor() as cursor:
query = """
SELECT ide FROM lugar
WHERE nombre=%s
"""
cursor.execute(query,(nombre))
ide = cursor.fetchone()
return ide['ide']
pass

def listarDatosUsuario(self, id, tipo):
with self.conexion.cursor() as cursor:
query="SELECT nombre, contrasena FROM usuario WHERE id_usuario=" + str(id)
cursor.execute(query)
usuario=cursor.fetchone()
empresa={}
turista={}
if tipo=='turista':
query="SELECT edad, pais_origen FROM turista WHERE id=" + str(id)
cursor.execute(query)
turista=cursor.fetchone()
empresa={"nombre":"Ninguno"}
else:
query="SELECT nombre FROM empresa WHERE id=" + str(id)
cursor.execute(query)
empresa=cursor.fetchone()
turista={"edad":"0", "pais_origen":"Ninguno"}
userdata={"contrasena": usuario['contrasena'],"edad": turista['edad'],"pais_origen": turista['pais_origen'],"nombre": empresa['nombre']}
return userdata
pass

def conseguir_id(self, email):
with self.conexion.cursor() as cursor:
query = """
SELECT id_usuario FROM usuario
WHERE email=%s
"""
cursor.execute(query,(email))
idU = cursor.fetchone()
return idU
self.conexion.commit()
pass

def conseguir_ultimo_idMasUno(self):
with self.conexion.cursor() as cursor:
query = """
SELECT id_usuario
FROM usuario
ORDER BY id_usuario
DESC LIMIT 1
"""
cursor.execute(query)
idU = cursor.fetchone()
return (idU['id_usuario']+1)
self.conexion.commit()
pass
37 changes: 37 additions & 0 deletions app/logic/clsSqlUpdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pymysql.cursors

class SqlUpdate:
def __init__(self):
self.conexion = pymysql.connect(host='localhost',
user='root',
password='',
db='uruguia_bd_test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)


def actualizarUsuario(self, nombre, password, id, tipo, edad, pais, nombreEmpresa):
with self.conexion.cursor() as cursor:
query="""
UPDATE usuario
SET nombre=%s,
contrasena=%s
WHERE id_usuario=""" + str(id)
cursor.execute(query,(nombre,password))
if tipo=='turista':
query="""
UPDATE turista
SET edad=%s,
pais_origen=%s
WHERE id=""" + str(id)
cursor.execute(query,(edad,pais))
pass
else:
query="""
UPDATE empresa
SET nombre=%s
WHERE id=""" + str(id)
cursor.execute(query,(nombreEmpresa))
pass
self.conexion.commit()
pass
Loading

0 comments on commit 6465228

Please sign in to comment.