Co-authored-by: José Henrique <jose.henrique.ivan@gmail.com>

This commit is contained in:
Gabriel Amaral 2023-11-30 22:05:39 -03:00
parent a08fd5f98b
commit ccc961e5d0
3 changed files with 73 additions and 3 deletions

29
main.py
View File

@ -42,6 +42,8 @@ def main():
elif escolha == "BD": elif escolha == "BD":
try: try:
tables = {}
connection = mysql.connector.connect(host='localhost', connection = mysql.connector.connect(host='localhost',
database='employees', database='employees',
user='employeesdb', user='employeesdb',
@ -51,6 +53,33 @@ def main():
print("Conectado ao employees-db") print("Conectado ao employees-db")
cursor = connection.cursor() 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: except Error as e:
print("Error while connecting to MySQL", e) print("Error while connecting to MySQL", e)
finally: finally:

10
sql.py
View File

@ -3,6 +3,12 @@ class SQL:
self.tables = tables self.tables = tables
self.query = query.replace(";", "") self.query = query.replace(";", "")
def _try_parse_int(self, value: str) -> int:
try:
return int(value)
except ValueError:
return value
# ISCOLHE * DE tabela # ISCOLHE * DE tabela
def execute(self): def execute(self):
query_parts = self.query.split(" ") query_parts = self.query.split(" ")
@ -29,7 +35,7 @@ class SQL:
index = query_parts.index("DONDE") index = query_parts.index("DONDE")
where_filter_column = query_parts[index + 1] where_filter_column = query_parts[index + 1]
where_filter_operator = query_parts[index + 2] 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] where_filter = [where_filter_column, where_filter_operator, where_filter_value]
# parse TABLE # parse TABLE
@ -84,6 +90,6 @@ class SQL:
row_values = [] row_values = []
for column in column_list: for column in column_list:
column_index = tables_columns.index(column) column_index = tables_columns.index(column)
row_values.append(row[column_index]) row_values.append(str(row[column_index]))
print(', '.join(row_values)) print(', '.join(row_values))

View File

@ -10,6 +10,12 @@ class Table:
def __repr__(self): def __repr__(self):
return self.name 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): def load_from_csv(self, csv_file):
with open(csv_file, 'r') as f: with open(csv_file, 'r') as f:
reader = f.read().splitlines() reader = f.read().splitlines()
@ -20,7 +26,36 @@ class Table:
for row in reader: for row in reader:
row = row.split(',') 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): def print_columns(self, column_list: list, where_filter: list = None, join_stmt: list = None):
if column_list[0] == '*': if column_list[0] == '*':