From ccc961e5d07a2512132e2ebc31ffbc946871b41c Mon Sep 17 00:00:00 2001 From: Gabriel Amaral Date: Thu, 30 Nov 2023 22:05:39 -0300 Subject: [PATCH] =?UTF-8?q?Co-authored-by:=20Jos=C3=A9=20Henrique=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 29 +++++++++++++++++++++++++++++ sql.py | 10 ++++++++-- table.py | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index c86e894..d1f83ee 100644 --- a/main.py +++ b/main.py @@ -42,6 +42,8 @@ def main(): elif escolha == "BD": try: + tables = {} + connection = mysql.connector.connect(host='localhost', database='employees', user='employeesdb', @@ -51,6 +53,33 @@ def main(): print("Conectado ao employees-db") cursor = connection.cursor() + cursor.execute("SHOW TABLES") + tables_names = cursor.fetchall() + + for table_name in tables_names: + # table_name = ('departments', ) + table_name = table_name[0] + # table_name = 'departments' + + print('Carregando', table_name) + tables[table_name] = Table(table_name) + tables[table_name].load_from_mysql(table_name,cursor) + print('Carregado', table_name) + + print("\n") + + while True: + print("Digite a query [0 para sair]:") + query = input() + + if query == "0": + break + + sql = SQL(tables, query) + sql.execute() + + print() + except Error as e: print("Error while connecting to MySQL", e) finally: diff --git a/sql.py b/sql.py index 5bce18c..e25a3f4 100644 --- a/sql.py +++ b/sql.py @@ -3,6 +3,12 @@ class SQL: self.tables = tables self.query = query.replace(";", "") + def _try_parse_int(self, value: str) -> int: + try: + return int(value) + except ValueError: + return value + # ISCOLHE * DE tabela def execute(self): query_parts = self.query.split(" ") @@ -29,7 +35,7 @@ class SQL: index = query_parts.index("DONDE") where_filter_column = query_parts[index + 1] where_filter_operator = query_parts[index + 2] - where_filter_value = query_parts[index + 3] + where_filter_value = self._try_parse_int(query_parts[index + 3]) where_filter = [where_filter_column, where_filter_operator, where_filter_value] # parse TABLE @@ -84,6 +90,6 @@ class SQL: row_values = [] for column in column_list: column_index = tables_columns.index(column) - row_values.append(row[column_index]) + row_values.append(str(row[column_index])) print(', '.join(row_values)) diff --git a/table.py b/table.py index 0320fd5..1ccd076 100644 --- a/table.py +++ b/table.py @@ -10,6 +10,12 @@ class Table: def __repr__(self): return self.name + def _try_parse_int(self, value: str) -> int: + try: + return int(value) + except ValueError: + return value + def load_from_csv(self, csv_file): with open(csv_file, 'r') as f: reader = f.read().splitlines() @@ -20,7 +26,36 @@ class Table: for row in reader: row = row.split(',') - self.rows.append(row) + row_list = [] + + for column in row: + row_list.append(self._try_parse_int(column)) + + self.rows.append(row_list) + + def load_from_mysql(self, table_name, cursor): + cursor.execute("SELECT * FROM " + table_name) + rows = cursor.fetchall() + + for row in rows: + row_list = [] + + for col in row: + row_list.append(col) + + self.rows.append(row_list) + + ''' + self.rows = [ + ["dp001", "Customer Service"] + ] + ''' + + cursor.execute("SHOW COLUMNS FROM " + table_name) + columns = cursor.fetchall() + + for row in columns: + self.columns.append(row[0]) def print_columns(self, column_list: list, where_filter: list = None, join_stmt: list = None): if column_list[0] == '*':