|
| 1 | +from ConnectionFactory import * |
| 2 | +from Contato import * |
| 3 | + |
| 4 | +class ContatoDAO: |
| 5 | + def __init__(self): |
| 6 | + self.__cf = ConnectionFactory() |
| 7 | + |
| 8 | + def add(self, contato): |
| 9 | + try: |
| 10 | + con = self.__cf.getconnection() |
| 11 | + if con.is_connected(): |
| 12 | + cursor = con.cursor() |
| 13 | + cursor.execute("INSERT INTO contato(nome, telefone, email) VALUES(%s, %s, %s)", (contato.getnome(), contato.gettelefone(), contato.getemail())) |
| 14 | + con.commit() |
| 15 | + if cursor.rowcount > 0: |
| 16 | + print('Contato adicionado.') |
| 17 | + else: |
| 18 | + print('Contato não adicionado.') |
| 19 | + except: |
| 20 | + print('Não foi possível adicionar o contato.') |
| 21 | + con.rollback() |
| 22 | + finally: |
| 23 | + self.__cf.closeconnection(con) |
| 24 | + |
| 25 | + def getcontato(self, id): |
| 26 | + try: |
| 27 | + con = self.__cf.getconnection() |
| 28 | + if con.is_connected(): |
| 29 | + cursor = con.cursor() |
| 30 | + cursor.execute("SELECT * FROM contato WHERE id = %s", (id,)) |
| 31 | + result = cursor.fetchone() |
| 32 | + if result is not None: |
| 33 | + contato = Contato(result[0], result[1], result[2], result[3]) |
| 34 | + return contato |
| 35 | + else: |
| 36 | + print('Não existe contato com esse ID.') |
| 37 | + except: |
| 38 | + print('Não foi possível buscar o contato.') |
| 39 | + finally: |
| 40 | + self.__cf.closeconnection(con) |
| 41 | + |
| 42 | + def getcontatospornome(self, nome): |
| 43 | + try: |
| 44 | + con = self.__cf.getconnection() |
| 45 | + if con.is_connected(): |
| 46 | + cursor = con.cursor() |
| 47 | + cursor.execute("SELECT * FROM contato WHERE nome LIKE %s", ('%' + nome + '%',)) |
| 48 | + result = cursor.fetchall() |
| 49 | + length = len(result) |
| 50 | + if length > 0: |
| 51 | + contatos = [] |
| 52 | + for i in range(length): |
| 53 | + contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3])) |
| 54 | + return contatos; |
| 55 | + else: |
| 56 | + print('Não existem contatos com esse nome ou parte dele.') |
| 57 | + except: |
| 58 | + print('Não foi possível buscar os contatos.') |
| 59 | + finally: |
| 60 | + self.__cf.closeconnection(con) |
| 61 | + |
| 62 | + def getcontatosportelefone(self, telefone): |
| 63 | + try: |
| 64 | + con = self.__cf.getconnection() |
| 65 | + if con.is_connected(): |
| 66 | + cursor = con.cursor() |
| 67 | + cursor.execute("SELECT * FROM contato WHERE telefone LIKE %s", ('%' + telefone + '%',)) |
| 68 | + result = cursor.fetchall() |
| 69 | + length = len(result) |
| 70 | + if length > 0: |
| 71 | + contatos = [] |
| 72 | + for i in range(length): |
| 73 | + contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3])) |
| 74 | + return contatos; |
| 75 | + else: |
| 76 | + print('Não existem contatos com esse telefone ou parte dele.') |
| 77 | + except: |
| 78 | + print('Não foi possível buscar os contatos.') |
| 79 | + finally: |
| 80 | + self.__cf.closeconnection(con) |
| 81 | + |
| 82 | + def getcontatosporemail(self, email): |
| 83 | + try: |
| 84 | + con = self.__cf.getconnection() |
| 85 | + if con.is_connected(): |
| 86 | + cursor = con.cursor() |
| 87 | + cursor.execute("SELECT * FROM contato WHERE email LIKE %s", ('%' + email + '%',)) |
| 88 | + result = cursor.fetchall() |
| 89 | + length = len(result) |
| 90 | + if length > 0: |
| 91 | + contatos = [] |
| 92 | + for i in range(length): |
| 93 | + contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3])) |
| 94 | + return contatos; |
| 95 | + else: |
| 96 | + print('Não existem contatos com esse email ou parte dele.') |
| 97 | + except: |
| 98 | + print('Não foi possível buscar os contatos.') |
| 99 | + finally: |
| 100 | + self.__cf.closeconnection(con) |
| 101 | + |
| 102 | + def getcontatos(self): |
| 103 | + try: |
| 104 | + con = self.__cf.getconnection() |
| 105 | + if con.is_connected(): |
| 106 | + cursor = con.cursor() |
| 107 | + cursor.execute("SELECT * FROM contato") |
| 108 | + result = cursor.fetchall() |
| 109 | + length = len(result) |
| 110 | + if len(result) > 0: |
| 111 | + contatos = [] |
| 112 | + for i in range(length): |
| 113 | + contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3])) |
| 114 | + return contatos; |
| 115 | + else: |
| 116 | + print('Não existem contatos.') |
| 117 | + except: |
| 118 | + print('Não foi possível buscar os contatos.') |
| 119 | + finally: |
| 120 | + self.__cf.closeconnection(con) |
| 121 | + |
| 122 | + def update(self, contato): |
| 123 | + try: |
| 124 | + con = self.__cf.getconnection() |
| 125 | + if con.is_connected(): |
| 126 | + cursor = con.cursor() |
| 127 | + cursor.execute("UPDATE contato SET nome = %s, telefone = %s, email = %s WHERE id = %s", (contato.getnome(), contato.gettelefone(), contato.getemail(), contato.getid())) |
| 128 | + con.commit() |
| 129 | + if cursor.rowcount > 0: |
| 130 | + print('Contato alterado com sucesso.') |
| 131 | + else: |
| 132 | + print('Não existe contato com esse ID.') |
| 133 | + except: |
| 134 | + print('Não foi possível alterar/atualizar o contato.') |
| 135 | + finally: |
| 136 | + self.__cf.closeconnection(con) |
| 137 | + |
| 138 | + def delete(self, id): |
| 139 | + try: |
| 140 | + con = self.__cf.getconnection() |
| 141 | + if con.is_connected(): |
| 142 | + cursor = con.cursor() |
| 143 | + cursor.execute("DELETE FROM contato WHERE id = %s", (id,)) |
| 144 | + con.commit() |
| 145 | + if cursor.rowcount > 0: |
| 146 | + print('Contato deletado com sucesso.') |
| 147 | + else: |
| 148 | + print('Não existe contato com esse ID.') |
| 149 | + except: |
| 150 | + print('Não foi possível excluir/remover o contato.') |
| 151 | + finally: |
| 152 | + self.__cf.closeconnection(con) |
0 commit comments